Page 1 of 1

Clicking a Button on a Pop Up Dialog if it Exists

Posted: Fri Feb 06, 2015 4:54 pm
by Fergal
(Ranorex Version 5.2.2)

I'm testing a web page, which occasionally has a pop up asking if the user wants to complete a survey. I've been trying to create a module that will click the "No Thanks" button if the dialog pops up.

The following if statement works great when the pop up dialog appears, i.e. the module successfully clicks the "No Thanks" button, if the pop up is displayed.

Code: Select all

if(repo.No_Thank_You_Button_on_Survey_DialogInfo != null)
{
     // code to click the "No Thanks" button
}
However, the module still runs the code in that if statement, when the dialog is not displayed. Hence the module fails, because it cannot click on the button.

Is there a way to edit that if statement, so that the code within it, will only execute if the "No Thanks" button is visible in the pop up.

Thanks!

Re: Clicking a Button on a Pop Up Dialog if it Exists

Posted: Fri Feb 06, 2015 4:59 pm
by krstcs
The problem is that the evaluation will always return true because you are checking if the repo object is not null, not if the element it represents is visible. Remember, the repo object represents the object's Ranorex's understanding of the object, not the object itself.

Try using this:

Code: Select all

if (repo.No_Thank_You_Button_on_Survey_Dialog.Visible) {
    // code to click the "No Thanks" button
}
Edit: Note the use of the base repo object, not the INFO object.

Re: Clicking a Button on a Pop Up Dialog if it Exists

Posted: Fri Feb 06, 2015 5:59 pm
by Fergal
Thanks very much krstcs.
krstcs wrote:The problem is that the evaluation will always return true because you are checking if the repo object is not null, not if the element it represents is visible....
The xPath for the object includes "@visible='True'" so I had assumed that if the item was not visible it would be null. That assumption appears to be incorrect, but I'm not sure why?

Code: Select all

if (repo.No_Thank_You_Button_on_Survey_Dialog.Visible)
Seems to have the same result as my original if statement, but I will spend more time on it to try and get it working. The workaround I've started using, is to set the User Code item to "Enable Continue on Fail".

Thanks again!

Re: Clicking a Button on a Pop Up Dialog if it Exists

Posted: Tue Feb 10, 2015 10:58 am
by Support Team
Hi Fergal,

I would use the explicit exists method of the RepoItemInfo object to check if the pop up exists:
if(repo.No_Thank_You_Button_on_Survey_DialogInfo.Exists(new Duration(7000))
{
     // code to click the "No Thanks" button
}
Each item and each folder in a repository provides an additional object item declared with '<ObjectName>Info'. It is used to access item related attributes without accessing the UI element directly in order to prevent Ranorex from throwing exceptions. The info object is mainly used to check whether an item or a folder path is valid or not.
Please have a look at our code examples chapter to learn more about using the info objects: Wait for UI Elements Using Repository
So the Info objects are part of the repository structure and will be created when you create the repository the first time.

Regards,
Markus

Re: Clicking a Button on a Pop Up Dialog if it Exists

Posted: Tue Feb 17, 2015 9:58 am
by Fergal
Thanks for your help Markus, that's working for me now.

Re: Clicking a Button on a Pop Up Dialog if it Exists

Posted: Wed Sep 07, 2016 11:53 am
by Andrew Simpson
Hi

Though I would add to the end to this as I am trying to achieve a similar thing, basically want to click a repository item if it exists, if not then just report a success and move on with the test. Here is the code I have

Code: Select all

if(repo.AccountsSuiteReportCriteriaScreen.AccountsSuiteReportCriteriaPageDown.Exists(5000))
       		
        	{
        		Mouse.Click(repo.AccountsSuiteReportCriteriaScreen.AccountsSuiteReportCriteriaPageDown);
        	}
        	else
        	{
        		Ranorex.Report.Success("Whole Criteria screen is visable");
        	}
However it doesnt seem to like the Exist command throwing this error
'Ranorex.Button' does not contain a definition for 'Exists' and no extension method 'Exists' accepting a first argument of type 'Ranorex.Button' could be found (are you missing a using directive or an assembly reference?) (CS1061)
I have loaded system.io so know that the exist command is there, any help would be appreciated

Thanks

Re: Clicking a Button on a Pop Up Dialog if it Exists

Posted: Wed Sep 07, 2016 2:34 pm
by krstcs
You have to use the button's INFO object to get the Exists() method.

If you button is named "MyButton", then the info object would be "MyButtonInfo", assuming the button is a standard repo object and not a repo folder.

Code: Select all

if (repo.MyApplication.MyButtonInfo.Exists(5000)) {
  //do stuff
}