Ranorex

English|Deutsch

RanoreXPath

RanoreXPath allows easy identification and filtering of GUI elements.

The following RanoreXPath represents the unique identifier of the '7' button in the calculator application:

How to use RanoreXPath within code

Within Ranorex Studio simple drag&drop elements from the Element Browser into code.

C#

Form formCalculator = "/form[@title='Calculator']";
formCalculator.Activate();
			
Button button1 = "/form[@title='Calculator']/button[@text='1']";
button1.Click();
						
List listListBox1 = "/form[@controlname='TestedApp']/list[@controlname='listBox1']";
foreach (ListItem item in listListBox1.Items)
{
  Mouse.MoveTo(item);
}

VB.NET

Dim formCalculator As Form = "/form[@title='Calculator']"
formCalculator.Activate()

Dim button1 As Button = "/form[@title='Calculator']/button[@text='1']"
button1.Click()

Dim listListBox1 As List = "/form[@controlname='TestedApp']/list[@controlname='listBox1']"
For Each item As ListItem In listListBox1.Items
	Mouse.MoveTo(item)
Next

Python

formCalculator = Form("/form[@title='Calculator']")
formCalculator.Activate()

button1 = Button("/form[@title='Calculator']/button[@text='1']")
button1.Click()

listListBox1 = List("/form[@controlname='TestedApp']/list[@controlname='listBox1']")
for item in settingsTree.Items:
	Mouse.MoveTo(item)

RanoreXPath syntax examples

Each RanoreXPath can return multiple GUI elements which match the path query.  RanoreXPath is modeled on W3C XPath.
Axes  
/form/button
absolute path
./button
relative path
//button
identifies all buttons beginning from root
.//button
identifies all buttons from current element
../button
identifies all buttons from parent
Attributes  
/form
identifies a top level application
/form[@title='Calculator']
identifies a top level application with the title 'Calculator'
/form[
@title='Calculator' and @instance='2']
identifies a top level application with the title 'Calculator' and an attribute of instance with value two
/form[
@title='Calculator' or @class='SciCalc']
identifies a top level application with the title 'Calculator' or by its process name
/form/button
identifies a button in the application
/form/button[2]
identifies the second button in the application
/form/button[@text='7']
identifies a button with a text attribute value of '7'
/form/button[@text~'^7']
identifies a button using a regular expression

Advanced RanoreXPath example

The following example describes how to use RanoreXPath to identify a GUI element having no unique attributes. The example shows how to access a HTML checkbox using a relative RanoreXPath expression.

Each row in the table represents a user. The users attributes are mapped into separate cells.

  1. Identify a user from a list by its name (green highlighted)

    /table/*/tr/td/a[@InnerText='Username']

  2. Select the corresponding checkbox with a relative path from the name (red highlighted)

    /../../td/input[@type='checkbox']

  3. Get the full path to the checkbox by combining the two paths.

RanoreXPath with regular expression

button[@text~'sample[0-9]']
matches the following button elements: sample0, sample1, ... sample9
listitem[@text~'^sample.*']
matches all elements starting with text value sample
listitem[@text~'gr(a|e)y']
matches text value gray or grey


The following are special characters that need to be escaped when used in a regular expression by preceding them with a backslash '\'. E.g. when you want to match the text 'Sample.' (with a dot at the end), the dot needs to be escaped: 'Sample\.'. 

CharacterDescription
.The dot will match any single character. For example 'Sample.' matches 'Sample1', 'Samplex', etc.
$The dollar sign will match the end of the string. The expression 'abc$' will match the sub-string 'abc' only if it is at the end of the string.
|The alternation character allows either expression on its side to match the target string. The expression 'gr(a|e)y' can match 'gray' or 'grey'.
*The asterisk indicates that the character to the left of the asterisk in the expression should match zero or more times. For example 'g*gle' matches ggle, gogle, google, gooogle, etc.
+The plus is similar to asterisk but there must be at least one match of the character to the left of the + sign in the expression. For example 'go+gle' matches gogle, google, gooogle, etc.
?The question mark (?) matches the character to its left 0 or 1 times. For example, 'colou?r' matches both color and colour.
^Beginning of the string. The expression '^A' will match an A only at the beginning of the string.
()The parenthesis affects the order of pattern evaluation.
[]Brackets enclosing a set of characters indicate that any of the enclosed characters may match the target character.
[^0-9]The caret immediately following the left-bracket  has a different meaning. It is used to exclude the remaining characters within brackets from matching the target string. The expression '[^0-9]' indicates that the target character must not be a digit.