A RanoreXPath expression is primarily used to uniquely identify UI elements within a desktop or web application. Generally, a RanoreXPath expression consists of:
The adapter type specifies the type or classification of a UI element to search for (button, form, text field, listbox, etc.).
Looking at the first part of the RanoreXPath expression '/form[@title='Form 1']' the
As an example the first part of the RanoreXPath expression shown in the pictures will look for a UI element which is of type form and has an attribute called 'title' with the value 'Form 1'. This form holds a container witch caption 'Container 1', which holds a button with text 'Button 1'.
Use the Ranorex Spy tool to get the RanoreXPath for a particular UI object. To edit a RanoreXPath use the text box in Ranorex Spy or change the path value of a repository item directly in the repository view. A more comfortable way to edit RanoreXPath is provided by the Ranorex Path Editor.
Note: Use the <CTRL> key in combination with the arrow keys to navigate to the previous or the next token in RanoreXPath. Use the <CTRL> key and the <SHIFT> key in combination with the arrow keys for selecting specific parts of the RanoreXPath. Use the <SHIFT> key in combination with the <UP> arrow key to select the current token of the RanoreXPath (tokens can be a values, identifiers, operators ...).In this section you will learn more about RanoreXPath:
| Axes | |
/form/button |
absolute path identifying all buttons that are children of a form |
./button |
relative path identifying all buttons that are children of the current element |
//button |
identifies all buttons that are descendants of the root element, i.e. all buttons in all levels of the element tree |
.//button |
identifies all buttons that are descendants of the current element, i.e. all buttons in the subtree of the current element |
../button |
identifies all buttons that are descendants of the parent of the current element |
/form/container?/button |
optional location step fitting a container adapter between form and button; identifies both, '/form/container/button' and '/form/button' |
/form/?/button |
optional location step fitting every adapter between form and button |
| Attributes | |
/form |
identifies a top level application |
/form[@title='Calculator'] |
identifies a top level application with the title 'Calculator' |
/form[ |
identifies a top level application with the title 'Calculator' and an attribute of instance with value two |
/form[@title='Calculator' |
identifies a top level application with the title 'Calculator' or by its class |
/form/button |
identifies a button in the application |
/form/button[2] |
identifies the second button in the application |
/form/button[-2] |
identifies the second-to-last 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 with a text attribute value that is not '7' |
/form/button[@text~'^7'] |
identifies a button using a regular expression |
/form/button[@text!~'^7'] |
identifies a button not matching a regular expression |
/form/control[@items.count=100] |
identifies a WinForms control using cascaded attribute names. The identified control has an "Items" property that has a "Count" property which returns the value 100 |
/form/*[@text='7'] |
identifies any element with a text attribute value of '7' |
/form/container? [@caption='Container 1']/button |
optional location step fitting a container adapter between form and button; identifies both, '/form/container[@caption='Container 1']/button' and '/form/button' |
/form/button[?'add'] |
identifies button with any attribute containing the substring 'add' (case-insensitive) |
/form/button[@text!=null()] |
identifies a button where the attribute text is not null |
/form/progressbar[@Value>=13.5] |
identifies a progress bar with value greater than or equal 13.5 (following comparison operators are also available: >, <=, <) |
/form[@title=$var] |
identifies a top level application with title attribute value set to value stored in variable $var |
/form/button[$var] |
identifies the button with index equals the value stored in the variable $var |
/dom//input[#'search'] |
identifies an input element on a web document with unique id 'search' (supported for web testing) |
| Functions | |
/form/table/row/cell[first()='True'] |
identifies the first cell of a row |
/form/table/row/cell[last()='True'] |
identifies the last cell of a row |
/form/table/row/cell[index()=2] |
identifies the second cell of a row |
/form[x()>100 and y()>100] |
identifies a top level application with screen coordinates greater than 100 |
/form/button[cx()>10 and cy()>10] |
identifies a button with client coordinates (coordinates of an element relative to its parent) greater than 10 |
/form[width()>100 and height()>100] |
identifies a top level application with width and height greater than 100. |
The following example describes how to use RanoreXPath to identify a GUI element not having 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.
/table/*/tr/td/a[@InnerText='Username']
/../../td/input[@type='checkbox']
button[@text~'sample[0-9]'] | matches the following button elements: 'sample0', 'sample1', ... 'sample9', 'My sample26' |
listitem[@text~'^sample.*'] | matches all elements starting with text value sample |
listitem[@text~'.*sample$'] | matches all elements ending with text value sample |
listitem[@text~'gr(a|e)y'] | matches text value gray or grey |
listitem[@text~'^sample\ 123$'] | matches 'sample 123' (use backslash to escape special characters like space) |
listitem[@text~'(?i:MyTeXt)'] | matches the regular expression case-insensitive, e.g. 'mytext', 'MYTEXT', 'mYTeXT', ... |
The following are special characters that need to be escaped when used in a regular expression by prefixing 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\.'.
| Character | Description |
. | 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 'go*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, 'colo?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. |
For additional information on regular expressions please consult the corresponding MSDN web site: http://msdn.microsoft.com/en-us/library/az24scfc.aspx
Download Test Automation Guide
(PDF file, 20MB)