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

How to check if a control exists without an exception thrown

Best practices, code snippets for common functionality, examples, and guidelines.

How to check if a control exists without an exception thrown

Postby jack_r_daniels » Tue Apr 06, 2010 12:04 am

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.
jack_r_daniels
 
Posts: 17
Joined: Fri Feb 12, 2010 6:33 pm

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

Postby Ciege » Tue Apr 06, 2010 12:45 am

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...
User avatar
Ciege
 
Posts: 965
Joined: Thu Oct 16, 2008 7:46 pm
Location: Arizona, USA

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

Postby Marianne Jacobsen » Tue Apr 06, 2010 8: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
Marianne Jacobsen
 
Posts: 44
Joined: Fri Oct 26, 2007 2:18 pm

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

Postby jack_r_daniels » Tue Apr 06, 2010 4: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.
jack_r_daniels
 
Posts: 17
Joined: Fri Feb 12, 2010 6:33 pm

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

Postby Support Team » Tue Apr 06, 2010 5: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
User avatar
Support Team
Site Admin
 
Posts: 4845
Joined: Fri Jul 07, 2006 5:30 pm
Location: Graz, Austria


Return to How To …

Who is online

Users browsing this forum: No registered users and 0 guests