Clicking a Button on a Pop Up Dialog if it Exists

Ask general questions here.
Fergal
Certified Professional
Certified Professional
Posts: 298
Joined: Tue Feb 18, 2014 2:14 pm
Location: Co Louth, Ireland
Contact:

Clicking a Button on a Pop Up Dialog if it Exists

Post by Fergal » Fri Feb 06, 2015 4:54 pm

(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!

krstcs
Ranorex Guru
Posts: 2683
Joined: Tue Feb 07, 2012 4:14 pm
Location: Austin, Texas, USA

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

Post by krstcs » Fri Feb 06, 2015 4:59 pm

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.
Shortcuts usually aren't...

Fergal
Certified Professional
Certified Professional
Posts: 298
Joined: Tue Feb 18, 2014 2:14 pm
Location: Co Louth, Ireland
Contact:

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

Post by Fergal » Fri Feb 06, 2015 5:59 pm

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!

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

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

Post by Support Team » Tue Feb 10, 2015 10:58 am

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
.
Image

Fergal
Certified Professional
Certified Professional
Posts: 298
Joined: Tue Feb 18, 2014 2:14 pm
Location: Co Louth, Ireland
Contact:

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

Post by Fergal » Tue Feb 17, 2015 9:58 am

Thanks for your help Markus, that's working for me now.

Andrew Simpson
Posts: 22
Joined: Thu Dec 03, 2015 11:58 am

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

Post by Andrew Simpson » Wed Sep 07, 2016 11:53 am

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

krstcs
Ranorex Guru
Posts: 2683
Joined: Tue Feb 07, 2012 4:14 pm
Location: Austin, Texas, USA

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

Post by krstcs » Wed Sep 07, 2016 2:34 pm

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
}
Shortcuts usually aren't...