Page tree
Skip to end of metadata
Go to start of metadata

XPath Operators

  • Boolean operatorsandornot() - Simple logic that can be integrated with the different properties used.

    ExampleUsing the query xpath=//*[@hint='Username' or @hint='Password'] will give us username text field and password text field.

    If we surround the properties with not() - xpath=//*[not(@hint='Username' or @hint='Password')] we will get all the elements in this page except for the text fields.

    b
  • A union operator: I - forms the union of two XPath expressions.

  • Arithmetic operators+-*div (divided), mod (Returns the remainder of a division)

  • Comparison operators: =, !=, >, <, <=, >=

    ExampleIf we want to get only the elements whose width is at least 8 times their height we can use this query - xpath=//*[@width div @height > 8]


XPath Functions

  • Contains(string, string) - Use this function if you are looking for a part of a value, very useful in cases of partial text. 

    ExampleYou can use the query xpath=//*[contains(@text,'Your balance is:')] in order to identify the element that holds the balance value, regardless of the value itself which can change from test to test.
  • starts-with(string, string) - For finding elements that start with a specific string.

    Examplexpath=//*[starts-with(@text, "M")] This query will return all elements with text property that start with "M":

  • concat(string, string, string*) - Used to connect two strings or more.

  • substring(stringstartlength- Returns a part of a string, for example substring('string', 2, 4) returns "trin".

  • substring-after(string, string) - Returns the part of the string that exists after some specific characters in the string.

  • substring-before(string, string) -  Returns the part of the string that exists before some specific characters in the string.

    Example: In order to get all apps that use more then 20 MB we will use substring-before function to extract the text before the decimal point - xpath=//*[substring-before(@text, '.')>20]/../*[@id='title']


  • string-length(string) - Returns the string length.

  • normalize-space(string) - Remove all the whitespace from a string.

    Example: In this query we get all app names with 19 or more characters with no consideration of whitespace - xpath=//*[string-length(normalize-space(@text))>=19]


  • translate(string, string, string) - You can use this function if you want to replace some specific characters in a string with other specific characters. For example "translate('string', 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')" will return the string "STRING" and "translate('We want pizza', 'pizza', 'burger')" will return the string "We want burger".

    Example: We want to get all elements that have a text property that contains a numeric value bigger than 1000000. You can notice that the numbers appear in this form: 10,00,000, In order for the query to work we use the translate function like this:  xpath=//*[translate(@text, ',' ,'')>1000000] the function replaces ',' with '' (In fact - deletes them).
     
  • cmd:matches(string, string) - This is a special addition to XPath in SeeTest Automation. It is used in order to integrate regular expression with the XPath query. 

    Example: The query xpath=//*[cmd:matches(@text, "^[^\s]+\s+R.*")] will identify all elements with a text property whose second word starts with 'R'


  • No labels