We're here to help
Creating an XPath
Provar’s XPath Editor is designed to create a stable and optimized XPath for when this type of locator is needed. This is useful for mapping dynamic UI fields through the Test Builder, when standard Salesforce or Visualforce locators are not available.
This feature is especially useful for creating or modifying an XPath to make it independent of randomly changing nodes or attributes in Salesforce, such as IDs.
Note that Provar has introduced a new XPath Editor in version 1.9.7 to make it easier to generate a reliable XPath for any field. This editor replaces Provar’s earlier XPath Editor. Steps for using the new XPath Editor are below. If you are on a Provar version earlier than 1.9.7, refer to the section at the bottom of this page, Creating an XPath (1.9.6 and earlier).
Creating an XPath (1.9.7+)
Provar has introduced a new XPath Editor in Provar version 1.9.7 to make it easier to generate a reliable XPath for any field. This editor helps create an optimized XPath by autopopulating options for the user, offering alternatives where available and giving instantaneous feedback in the browser window.
This editor is available in the Test Builder whenever a field is being mapped and an XPath option is available.
Opening the XPath Editor
Start by mapping the field as normal. The Test Builder will present a draft test step with the Field Locator section set to the default option, which is By SLDS Locator in the example below:
Click into the By SLDS Label and change it to By XPath. This will open the XPath editor:
Note that the element the XPath matches is highlighted in the browser in red.
Using the XPath Editor
Within the XPath editor itself, there is a list of qualifier attributes that correspond to the available entry:
These qualifier attributes contain all the attributes for the node that the cursor is focused on:
We can add more attributes to the populated Xpath from the Add another dropdown in the editor, as below:
Once selected, the attribute will appear in the editor’s list of attributes so that a value can be set.
In general, these XPath locators will follow a sequence of ID, Name, Text, Label. This means that, if a field has these attributes present in the HTML structure, the locators will always show in this order.
In addition, for any field that has a label available in the HTML structure, a label locator will always be shown as an alternative locator:
Provar also provides an autocomplete option for the XPath by providing a list of available child nodes to the node that the focus is currently on. Pressing Ctrl + Spacebar will open the autocomplete list:
If the cursor focus is after ‘/’ and in front of a tag name, using autocomplete will replace the tag name with the newly chosen option.
Provar also supports the use of grouping and unions within the XPath:
Please note that the XPath used post-Union character will not reflect its qualifier attributes.
If the XPath doesn’t match any elements, an error is shown, as below. The qualifiers remain editable so that the XPath can be updated to match an element.
If the XPath is invalid, an invalid error will be shown and the qualifier attributes will be hidden from editing until the XPath is fixed:
In some cases the XPath editor will present a warning, for example if the XPath matches more than one element on the page. In this case the warning is shown in the editor but the step can still be added to the test if wanted.
In some cases a Failed to parse XPath warning may appear. If this happens the qualifier attributes are hidden until the issue is resolved. If you are not sure how to resolve the issue, file a case via the support portal.
Creating an XPath (1.9.6 and earlier)
Note that Provar has introduced a new XPath Editor in version 1.9.7 to make it easier to generate a reliable XPath for any field. This editor replaces Provar’s earlier XPath Editor. However, if you are on a Provar version earlier than 1.9.7, the instructions below describe how to use this editor.
In addition, if you should need to switch back to the old XPath editor for any reason, this can be done by making the following entry in the provar.ini file:
The example below describes how to create an XPath.
The field being mapped here is ‘Opportunity Name’, a custom Visualforce field which has a random HTML ‘ID’ assigned to it.
Although the Test Builder may accurately map this field using an ID (see Step 1), it is likely that this ID will change when running the test case in a different Org and so the mapped field will not be identified successfully. We will therefore use the XPath Generator to map a more stable and optimized XPath.
Step 1: Note the ID locator being used in the current mapping. Click on the By ID dropdown to see the other locator options.
Step 2: Choose the XPath option from the list:
Note that the XPath contains a Name attribute which is random like an ID. This needs to be optimized.
Step 3: Click on the Edit XPath icon as highlighted below:
Step 4: Note the tags which have appeared under the locator field, e.g. HTML, DIV, INPUT. Colors are used on the tags to identify those which are currently being used in the XPath.
In this case, the INPUT tag is being used along with the ‘Name’ attribute.
A green tag indicates that you have selected this tag for editing and attribute selection.
Other colors will also appear to indicate the following:
- Grey: Tag is not being used in the current XPath
- Pink: Tag is being used in the current XPath
- Blue: This is the last or the focussed tag of the XPath
Step 5: Uncheck the ‘Name’ attribute and note the change in XPath. Note also that multiple fields are now highlighted in the browser, indicating that our XPath is not yet optimized:
Step 6: Now click on the DIV tag, going one step up the hierarchy in the HTML. Note that the DIV tag is now highlighted in green. The focus has also narrowed to highlighting fewer fields.
Step 7: Check the Class attribute. Note that the XPath now includes the Class name and only Opportunity Name is highlighted. This indicates that our XPath is now optimized as well as stable, being independent of Field ID and Name.
Finally, click the Close XPath Edit icon as highlighted below:
Step 8: Confirm the XPath to map this field by clicking Add & Do.