1

I'm new to Ranorex and trying to use Ranorex's C# API for a Windows desktop application. How to find an element only by automation id/text in C#?

Looking for something like,

mainWindow.Get(SearchCriteria.ByAutomationId("<automationId>"))
mainWindow.Get<Button>("<automationId>");

or

mainWindow.Button.GetElement("<automationId>");

But in the API docs, all I can see is XPath (RanoreXPath) based object identification.

Pedro
  • 12,032
  • 4
  • 32
  • 45
Suresh Raja
  • 411
  • 6
  • 23

3 Answers3

2

You don't have to give the whole XPath, but use something like this:

Host.Local.FindSingle("//button[@automationid='autoidname']");
Sae1962
  • 1,122
  • 15
  • 31
mosaad
  • 2,276
  • 5
  • 27
  • 49
  • As Suresh has added a reference to the AUT it's good idea to add the targeted form to the xpath as well. This will result in quick identification of the element. Eg. form specified xpath would be: "/form[@title='TheTitle']//div[@innertext='Test data']". The "//" means that the div is nested somewhere in the form and is not a direct descendant. Also to use some actions on it you should create an instance of an object of it. So for example for html link: "ATag aLink = Host.Local.FindSingle("dom[@domain='www.google.com']//a[@innertext='Pictures']);" and then use it as "aLink.PerformClick();" – Martin Aug 05 '16 at 10:19
1

To help find the XPath syntax to find an element, it is highly recommended to use the Ranorex Spy (would save you a lot of time).

I would also recommend using the recorder instead of directly user code, as it greatly simplifies automated tests maintenance… (and facilitates learning Ranorex). BTW, if you create a recording using the recorder and open its corresponding source file, you will see the equivalent code required to do whatever action you recorded. Saves lots of time when developing user code…

Hope this points you in the right direction.

Sae1962
  • 1,122
  • 15
  • 31
Sup3rHugh
  • 677
  • 7
  • 15
0

Do you know where it's located (approximately)? If you do, open SPY and click "Track", then click on that area where your element is located. In SPY, navigate to tab "BROWSER & RESULTS" and look through the tree. On the right side, you can see tabs "Overview/Advanced", while in Advanced tab scroll to bottom and look for Name of your element.

Also, you can try this: Edit the path to your element and hit "Apply". For example, as suggested above – use .//text[@automationid='DealerNameText'] or .//button[@automationid='ButtonPause' and @visible='true'] that depends on your element. Check this out, helped me a lot.

Sae1962
  • 1,122
  • 15
  • 31
Sunguresat
  • 483
  • 2
  • 6
  • 11
  • Forgot to add, if you know type of your element you can do this: /form[@processname='MyApp']//container[@automationid='MyElementName'] /form[@processname='MyApp'] = root process // = any descendants. this is very important! if you are not sure where your element lies, but you know type and name, just use this example. container = type [@automationid='MyElementName'] = name of my element – Sunguresat Jun 21 '17 at 07:57