Best way to find an element on a loading form?

Ask general questions here.
jjorgens
Posts: 14
Joined: Wed Apr 01, 2009 7:32 pm

Best way to find an element on a loading form?

Post by jjorgens » Thu Aug 06, 2009 8:17 pm

Platform: Windows XP SP3
Ranorex Build: 2.1.0.6243

I am currently working on automating our Windows side installer. I am trying to figure out a good reliable way to find buttons and check boxes. Currently I get the form first, then I am using findSingle to find the adapters on the form. This sometimes works but, I am having problems with forms that reload. For example, in our msi installer, it will come up with no buttons at first. When the form comes up it initializes. Once that is done then buttons appear. As soon as the form pops up I get it but because the buttons are not on the form yet I get a form with no buttons. What I did originally was put a long time out on the button hoping it would find it, but that did not seem to work. So what I have done now is put it in a loop and I keep looking for the button. This works better.

What I really want to know is:
1. We are storing the form in a variable. Does the variable refresh to contain the latest elements? (Like what I described above)
2. Should a long time out find the element on the form? (I guess this depends on question 1)

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Re: Best way to find an element on a loading form?

Post by Support Team » Fri Aug 07, 2009 11:53 am

jjorgens wrote:1. We are storing the form in a variable. Does the variable refresh to contain the latest elements? (Like what I described above)
All the attributes of the form element will always get you the up-to-date information. E.g. every time you get the Children property, the form GUI element is asked again for its children. So, if you're invoking a Find method on your variable, the search will return the most recent elements.
jjorgens wrote:2. Should a long time out find the element on the form? (I guess this depends on question 1)
A Find/FindSingle call on the form variable or on Host.Local with a longer timeout will find the element when it appears, i.e. when the form is shown and the element inside that form exists. When you use a timeout with Find/FindSingle, Ranorex will search for the corresponding element until it found the element or the timeout is reached. So, using those methods with an adequate timeout should work for you.

Regards,
Alex
Ranorex Support Team
.
Image

jjorgens
Posts: 14
Joined: Wed Apr 01, 2009 7:32 pm

Re: Best way to find an element on a loading form?

Post by jjorgens » Fri Aug 07, 2009 10:46 pm

I tried doing this like what you mentioned but I cant seem to get it to work. I am using cmdlets to wrap around Ranorex. In my powershell script I call my cmdlets. I can get this to work if I use while loops. When its working i do something like this:

Method 1 - Powershell script with while loop

Code: Select all

$times = 0
$max_loop = 20
$installForm = $null
Do 
{
	$installForm = Get-Form "My Installer" -TimeOut 30000
	$times++
}
While ($installForm -ne $null -and $times -le $max_loop)
if ($installForm -eq $null) 
{
	throw "Could not find form"
}

$times = 0
$nextButton = $null
Do 
{
	$installForm = Get-Form "My Installer" -TimeOut 20000
	$nextButton = Get-Button Next $installForm -TimeOut 30000
	$times++
}
While ($nextButton -ne $null -and $times -le $max_loop)
if ($nextButton -eq $null) 
{
	throw "Could not find button"
}
$nextButton.Click()
From what I understand from your reply, I should not have to do this loop, but I should be able to do something like this:

Method 2 - Powershell script without loop

Code: Select all

$installForm = Get-Form "My Installer" -TimeOut 30000
if ($installForm -eq $null) 
{
	throw "Could not find install form"
}

$nextButton = Get-Button "Next" $installForm -TimeOut 30000
if ($nextButton -eq $null) 
{
	throw "Could not find next button"
}
$nextButton.Click()
Method 1 works and method 2 does not. Like I mentioned above, I am using cmdlets to wrap around Ranorex. For example I have cmdlets called get-button and get-form. For get-form, I give a string of the form name I am looking for. For get-button i give it the button name and the form that is returned from get-form.

Section of get-button cmdlet

Code: Select all

            Ranorex.Button buttonItem = null;
            try
            {
                container.EnsureVisible();
                buttonItem = container.FindSingle<Ranorex.Button>(new RxPath(".//button[@text~'" + name + "']"),
                    new Duration(timeOut));
            }
            catch (ElementNotFoundException ex)
            {
                exceptionInformation = ex.ToString();
            }
            return buttonItem;
Section of get-form cmdlet

Code: Select all

            Ranorex.Form newForm = null;
            try
            {
                newForm = Ranorex.Host.Local.FindSingle<Ranorex.Form>(new RxPath("/form[@title~'" + name + "']"),
                    new Duration(timeOut));
            }
            catch (ElementNotFoundException ex)
            {
                exceptionInformation = ex.ToString();
            }
            return newForm;
I am not sure why Method 2 is not finding the button. I have tried it with longer timeouts and I still can not find it. I have checked to make sure that I have a valid form before trying to get the button and I still cannot get the button object.

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Re: Best way to find an element on a loading form?

Post by Support Team » Mon Aug 10, 2009 12:05 pm

Hmmm, there should not be much of a difference between method 1 and 2. The only thing I could think of is that the wrong form is found, one that does not contain the button. Could you try method 2 and search the button using an absolute path? I.e. change your get-button cmdlet so that it takes an absolute path, that is the combination of the form path and the relative path of the button.

Regards,
Alex
Ranorex Support Team
.
Image