Existence of an Adapter

Ask general questions here.
tgagel
Posts: 33
Joined: Tue Jun 04, 2013 7:50 pm

Existence of an Adapter

Post by tgagel » Tue Apr 19, 2016 8:31 pm

In our application we have some versions of the application where an element may not be present in all instances of the application. To speed up our scripts I've created one off user code snippits for these elements where it checks for the existence of the element and then only populates the element if it is present. For example:

Code: Select all

            bool isPresent = repo.Element1Info.Exists(1000);
        	
            if (isPresent)
            {
            	repo.Element1.PressKeys("1");
            }
I'm trying to create a generic user code method where I pass in a repo item (adapter) and string. The method will check for the existence of the element (ideally in a way such that I can override the typical repo timeouts since I know the page is already loaded) and then enter some info if the item exists. I have tried various ways of validating the existence of an Adapter but can't get it to work. I even tried a try / catch but, when I pass an element that is not on the page, it errors out after the search timeout expires saying that it can't find the element.

Here is my code (and some of the things I've tried that are commented out):

Code: Select all

       	public void SetValueWhenPresent(Adapter repoElement, string valueToEnter)
        {
            bool itemPresent = true;
            
            // Check to see if the item exists
// The following didn't work and failed when passing in an element which didn't exist on the page
            try {
            	Validate.Exists(repoElement, null, false);
            } catch(Exception ex) { itemPresent = false;}

// The following didn't work and failed when passing in an element which didn't exist on the page
//            try {
//            	itemPresent = repoElement.Valid;
//            } catch(Exception ex) { itemPresent = false;}

// The following didn't work and failed when passing in an element which didn't exist on the page
//            itemPresent = repoElement.Valid;

			Report.Info("itemPresent = " + itemPresent.ToString());
            
            if (itemPresent)
            {
            	Report.Log(ReportLevel.Info, "Keyboard", "Press keys '" + valueToEnter + "' with focus on '" + repoElement.Element);
            	repoElement.PressKeys("{Home}{LShiftKey down}{End}{LShiftKey up}{Delete}" + valueToEnter);
            }
        }
I know about changing the property of a Key Sequence to Continue on Fail = True (this is how cases like this have been handled so far) but that uses the repository search timeout and slows down the scripts. Note that my 2nd code snippit above wouldn't improve the search timeout issue like the timeout of 1s in the first code snippit. Also the 1st snippit uses the "Info" of the element and, from what I've seen, I can't find a way to pass that (or convert the adapter to the element's "Info"). I suspect if there is a way to do that I could solve this as well (so I can use Exists(searchTimeout), which the Adapter doesn't support).

Any suggestions on getting a generic method to do what I'm able to do in the first code snippit (capture the existence of the element and set a max of amount of time to search for it)?

Thanks,

Todd

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 3944
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: Existence of an Adapter

Post by odklizec » Wed Apr 20, 2016 7:27 am

Hi Todd,

Just in case you missed it, you can pass also repo item info element to your method. Of course, you will then have to create an adapter, to access some of the adapter-only available methods. So then your method can look like this:

Code: Select all

public void SetValueWhenPresent(Core.Repository.RepoItemInfo repoInfoElement, string valueToEnter)
{
            bool isPresent = repoInfoElement.Exists(1000);
           
            if (isPresent)
            {
               repoInfoElement.CreateAdapter<Unknown>(false).PressKeys(valueToEnter);
            }
}
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration

tgagel
Posts: 33
Joined: Tue Jun 04, 2013 7:50 pm

Re: Existence of an Adapter

Post by tgagel » Wed Apr 20, 2016 11:58 am

I did miss that and that is exactly what I needed. Thanks Pavel!