Page 1 of 1

if statement causing failure

Posted: Mon May 15, 2017 8:29 pm
by BrotherCarson
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.

Re: if statement causing failure

Posted: Mon May 15, 2017 9:08 pm
by Support Team
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

Re: if statement causing failure

Posted: Mon May 15, 2017 9:23 pm
by krstcs
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
}

Re: if statement causing failure

Posted: Tue May 16, 2017 8:44 am
by Stub
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.

Re: if statement causing failure

Posted: Tue May 16, 2017 3:18 pm
by BrotherCarson
Awesome!

Using Exists() on the Info worked.

Thank you!