Tips for strong XPath - native & web objects

Use proper object attributes to make your XPath specification for an object as strong as necessary to find the right object.

XPath is your way of identifying the UI Elements that a test script wants to automate. As its name implies the XPath specification is designed to specify the traversal path through the UI object tree to arrive at the particular UI Element.

Specifying the full traversal path through the tree has the disadvantage that it is very sensitive to any change to the applications UI. For example, if new options are added to a table this may cause your path traversal to go to a different element.

A strong XPath is one that will withstand most changes to the application UI. The following tips will guide you to find the simple and strong specification for the application UI elements.

Do's and Don'ts

Do:

  1. Use anchors in your XPath. Base the identification on Strong attributes (see below) for elements in your path . For example: //div[@id="example]/div[@class="class"]/button
  2. Use functions like "contains" and "starts-with". For example: "//button[contains(text(),'hello')]".
  3. Use axes to find anchors. For example: "//button[@id='goodID']/parent::*/following-sibling::*[1]/button".
  4. Use strong attributes in your XPath. The attributes are written below the tips.
  5. Write the tag instead of using a wildcard "*". Example: "//button" is stronger than "//*".
  6. If you are writing XPath for multiple platforms, you can use the "or" operator with a strong attribute from each platform. For example : "//*[@class='Android.widget.button' or @class='UIAButton']".
  7. Use the text of the elements with functions. For example : '//button[contains(text(),"OK")]' – is better than just noting the button or the text OK, since the expression explicitly states a button with the text OK.

Do not:

  1. Avoid using absolute and long XPaths. "//view/../…/…/…/…./…./…./button" as they tend to be fragile and likely to break whenever the application changes.
  2. Do not use the operator "=" with text attributes. Use "contains" and "starts-with". For example: "//button[contains(text(),'hello')]"
  3. Do not use attributes that depend on the device – x, y, width, height etc.
  4. Do not rely on an index number that may change For example : "(//button)[17]"

Strong attributes

Recommended to use the following attributes in the XPath specifications:

Web:

  1. Id. Example : "//*[@id='perfecto']".
  2. Name. Example : "//button[@name='mobile']".
  3. Class. Example : "//div/a[@class='perfectomobile']".

iOS without instrumentation:

  • Label. Example : "//*[@label='ios']".
  • Name. Example : "//*[@name='ios8']".
  • Value. Example : "//*[@value='example-value']".
  • Class. Example: "//*[@class='Class.Ios' or @class='Class.Android']".

iOS with instrumentation:

  • Label. Example : "//*[@label='ios']".

Android with/without instrumentation:

  • ResourceID. Example: "//*[@resourceid='com.perfectomobile.multiple:id/editText1']".
  • Content Description. Example: "//text[@contentDesc='HELLO']".