if statement causing failure

Class library usage, coding and language questions.
User avatar
BrotherCarson
Posts: 5
Joined: Mon May 15, 2017 8:21 pm
Location: Dallas, TX

if statement causing failure

Post by BrotherCarson » Mon May 15, 2017 8:29 pm

Hey all,

*zips up flame suit*

I'm fairly new to using this tool in it's vanilla form and running into an issue with an if statement.

Under various circumstances the site I'm testing against will "sometimes" present a page where additional selections are available. So I though an if would be perfect here:

Code: Select all

            if (repo.URL.SelectOffer.Visible){
            	            
            Report.Log(ReportLevel.Info, "Selcting Offer");
            repo.URL.SelectOffer.Click();
            Delay.Milliseconds(0);
            
            }
However, the test fails when it can't find it and doesn't just move on if it can't find it.

If anyone could help me in the right direction I would greatly appreciate it.

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Re: if statement causing failure

Post by Support Team » Mon May 15, 2017 9:08 pm

Hello BrotherCarson,

For events which are truly random, I recommend using a Popup watcher. More specifically for your case, use the WatchAndClick method since you only need to click an element if the element exist.

Also note, Ranorex throws an exception in most cases, instead of returning a bool. To handle this, you need to use try/catch block instead.

Code: Select all

try 
{
	Validate.Exists(myElement);
	Report.Success(myElement.Name + " found");
		
} catch (ValidationException) 
{
	Report.Warn(myElement.Name + " not found!");
}
I hope this helps!

Kind Regards,
-Ned

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

Re: if statement causing failure

Post by krstcs » Mon May 15, 2017 9:23 pm

In addition to what Ned mentioned, you can also you the Exists() method of the RepoItemInfo object for the element you are looking for. This will not throw an exception at all, but will return bool (true if found withing the timeout, false if otherwise). I use it quite a bit.

Code: Select all

if (repo.MyForm.MyButtonInfo.Exists()) {  //notice MyButtonInfo instead of MyButton
  //do stuff
}
Shortcuts usually aren't...

User avatar
Stub
Posts: 515
Joined: Fri Jul 15, 2016 1:35 pm

Re: if statement causing failure

Post by Stub » Tue May 16, 2017 8:44 am

And, IIRC, you can stick a Duration onto the Exists call to do a very quick check. I've been doing that rather than using PopupWatchers of late.

User avatar
BrotherCarson
Posts: 5
Joined: Mon May 15, 2017 8:21 pm
Location: Dallas, TX

Re: if statement causing failure

Post by BrotherCarson » Tue May 16, 2017 3:18 pm

Awesome!

Using Exists() on the Info worked.

Thank you!