How to check if a control exists without an exception thrown

Best practices, code snippets for common functionality, examples, and guidelines.
jack_r_daniels
Posts: 17
Joined: Fri Feb 12, 2010 5:33 pm

How to check if a control exists without an exception thrown

Post by jack_r_daniels » Mon Apr 05, 2010 11:04 pm

Hi all...

I have a UI control that might exist or might not... if it does exist, I want to be able to click it. And if it does not exist, I want to be able to click on a different control.

How can I check if the control exists or not without an exception being thrown? Thanks.

Jack.

User avatar
Ciege
Ranorex Guru
Posts: 1335
Joined: Thu Oct 16, 2008 6:46 pm
Location: Arizona, USA

Re: How to check if a control exists without an exception thrown

Post by Ciege » Mon Apr 05, 2010 11:45 pm

Put your Find statement in a try/catch block. If an exception is thrown that the control is not found, the catch will catch it and you can continue on with your testing.

Pseudo code:

Code: Select all

try
{ 
myobject = findsingle(...my object to find...);
boolMyobjectFound = true;
}
catch (RanorexException e)
{
boolMyObjectFound = false;
}

if (boolMyobjectFound == true)
{
do this
}
else
{
do that
}

If this or any response has helped you, please reply to the thread stating that it worked so other people with a similar issue will know how you fixed your issue!

Ciege...

Marianne Jacobsen
Posts: 44
Joined: Fri Oct 26, 2007 1:18 pm

Re: How to check if a control exists without an exception thrown

Post by Marianne Jacobsen » Tue Apr 06, 2010 7:53 am

Or use TryFindSingle to get the control. Will return true if control is found and false otherwise. out Element parameter will contain your control if it exists

jack_r_daniels
Posts: 17
Joined: Fri Feb 12, 2010 5:33 pm

Re: How to check if a control exists without an exception thrown

Post by jack_r_daniels » Tue Apr 06, 2010 3:29 pm

Thanks Marianne... I opted to go with your suggestion.
I initially tried that approach but it kept failing.
To share a lesson learned, here are some details...
to call the Host.Local.TryFindSingle you have to pass an RxPath. I don;t want to hard code the path in call. so, I initially used repo.object.GetPath() to try to get the path, but that would throw an exception because it actually tries to find the object.
The proper thing to do is to call repo.objectInfo.AbsolutePath which returns the path to the object as it exists in the repository (which is what I wanted to do all along).

So, use Host.Local.FindSingle(repo.objectInfo.absolutepath, out test)

Hope this helps someone else who runs into this problem.

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

Re: How to check if a control exists without an exception thrown

Post by Support Team » Tue Apr 06, 2010 4:30 pm

For Ranorex repositories, you can also use the RepoItemInfo.Exists() method of the info object corresponding to your repository items:
bool buttonExists = myRepo.MyForm.MyButtonInfo.Exists<Button>();
That way, the timeout values and the path set to the repository item will be used. You can even get the item using the out parameter of the Exists method:
Button myButton;
bool buttonExists = myRepo.MyForm.MyButtonInfo.Exists<Button>(out myButton);
Regards,
Alex
Ranorex Support Team
.
Image