Use an object variable as starting point for TryFindSingle

Class library usage, coding and language questions.
jackrabbit
Posts: 47
Joined: Wed Mar 18, 2015 10:06 pm

Use an object variable as starting point for TryFindSingle

Post by jackrabbit » Tue Mar 24, 2015 4:23 pm

In my UserCode, I already found a TabPage, now I need to quickly find a button inside that tab page:

Code: Select all

String MyPath = "/form[@title='My app']/element/tabpagelist/tabpage[@name='General']";
Ranorex.TabPage MyTabPage = null;
if (! Host.Local.TryFindSingle(MyPath, 5000, out MyTabPage) 
   throw new ValidationException("Tab not found");

MyPath = "Button[@name='button1']";
Ranorex.Button MyButton = null;
if (! MyTabPage.TryFindSingle(MyPath, 5000, out MyButton)   // Gives an error
   throw new ValidationException("Button not found");

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 3921
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: Use an object variable as starting point for TryFindSingle

Post by odklizec » Tue Mar 24, 2015 4:37 pm

Hi,

What error are you getting exactly? There seems to be missing bracket at the end of failing line?

Code: Select all

if (! MyTabPage.TryFindSingle(MyPath, 5000, out MyButton))
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration

jackrabbit
Posts: 47
Joined: Wed Mar 18, 2015 10:06 pm

Re: Use an object variable as starting point for TryFindSingle

Post by jackrabbit » Tue Mar 24, 2015 5:58 pm

No, I had the missing bracket, it was just a transcription problem.

Here is the error I get when I build:

An object reference is required for the non-static field, method, or property 'Ranorex.Adapter.FindSingle(Ranorex.Core.RxPath, Ranorex.Duration)' (CS0120) - C:\Temp\Ranorex\Ranorex\Accès NovA\Ruban\Ruban\GestionFormes.cs:89,28

The only workaround I found, is to copy my variable into a new variable (Element Object = MyTabPage) and then specify Object.FindSingle(...), however I would rather use TryFindSIngle to avoid an automatic exception is the object does not exists.

Is there a way to use a variable as a starting point to the TryFindSingle function?

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

Re: Use an object variable as starting point for TryFindSingle

Post by Support Team » Thu Mar 26, 2015 7:11 pm

Hi jackrabbit,

Unfortunately I was not able to reproduce this error on my side.
The following code works as expected with the Windows Calculator application
String MyPath ="/form[@title='Calculator']/element[@class='CalcFrame']";
Ranorex.Unknown myElement = null;

if (! Host.Local.TryFindSingle(MyPath, 5000, out myElement))
    throw new ValidationException("Element not found");
			
MyPath = "/?/?/container[@instance='1']/button[@controlid='122']";
Ranorex.Button MyButton = null;

if (! myElement.TryFindSingle(MyPath, 5000, out MyButton))
    throw new ValidationException("Button not found");
May I ask you to provide more information about your system under test?
Thank you.

Regards,
Markus (S)
.
Image