Skip to content

Instantly share code, notes, and snippets.

@sauceaaron
Created January 10, 2020 01:37
Show Gist options
  • Save sauceaaron/ab74c4ebaeeddc73bf9205667f9707c4 to your computer and use it in GitHub Desktop.
Save sauceaaron/ab74c4ebaeeddc73bf9205667f9707c4 to your computer and use it in GitHub Desktop.

Appium Selectors

General preferences

  • ACCESSIBILITY ID

  • CLASS NAME

  • IOS PREDICATE STRING

  • IOS CLASS CHAIN

  • XPATH

  • ID -- usually equivalent to Accessibility ID if there is one

  • NAME -- can be very slow because it searches all name attributes when used by itself

Rules to remember

  1. choose the best selector strategy
  2. sometimes it's not always obvious
  3. test out different selectors
  4. find something easy to locate to reduce the tree size
  5. build predicates / class chains that eliminate variables
  6. the order of class chain elements matters
  7. start with easy to prune rules
  8. sometimes (rarely) multiple queries can be faster
  9. getting all the elements in a group and then filtering client side
  10. use indexes when it makes sense or doesn't matter (e.g. first, last)

ACCESSIBILITY ID

  • usually the best option

  • unique

  • efficient to search for

  • the only strategy that work the same cross platform

  • uses "content desc" property on Android

    $('~my_accessibility_id')

CLASS_NAME

  • equivalent to tag name for HTML

  • usually not unique

  • fast to search

  • can be used to get a collection for secondary search

    $('XCUIElementTypeStaticText')

IOS PREDICATE STRING

  • combine simple critera to form more complex matches
  • native Javascript search strategy -- built into IOS
  • usable in IOS 10+ using '-ios predicate string' locator strategy
  • more efficient than XPath
  • (because XML has to be generated and then deserialized to map to )
  • Predicate expressions are executed in the same order listed

Properties:

  • name
  • value
  • label
  • type
  • visible
  • enabled
  • rect

complex predicate example

type == 'XCUIElementTypeStaticText'
AND value BEGINSWITH[c] 'Ncc Auto Five'
AND name CONTAINS 'bar'
AND visible == 1

webdriver.io locator

$('-ios predicate string:' + 
  'type==XCUIElementTypeStaticText' + ' && ' +  
  'name==title_label'   ' && ' +  
  'label CONTAINS "Ncc Auto" &&' + 
)

IOS CLASS CHAIN

  • built into Appium
  • similar to XPath but more efficient
  • less functionality
  • ability to refer to elements by index
  • good for hierarchical location
  • allows attributes
  • search for CONTAINS, BEGINSWITH

webdriverio example:

$('-ios class chain:' +
  '**/XCUIElementTypeStaticText[`name == "title_label"`]'
)

XPATH

  • least efficient
  • because of serialization
  • slowest on IOS
  • specific over general
  • good for index
  • can get siblings and parent

example:

//XCUIElementTypeStaticText[@name="title_label"][1]

Reference

Overview of different locator strategies

https://appiumpro.com/editions/8

Predicate Strings

http://appium.io/docs/en/writing-running-appium/ios/ios-predicate/#ios-predicate

https://github.com/facebookarchive/WebDriverAgent/wiki/Predicate-Queries-Construction-Rules

https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Predicates/Articles/pSyntax.html

Class Chain

https://github.com/facebookarchive/WebDriverAgent/wiki/Class-Chain-Queries-Construction-Rules

Webdriver.io syntax

https://webdriver.io/docs/selectors.html

Tips for performance

https://github.com/facebookarchive/WebDriverAgent/wiki/How-To-Achieve-The-Best-Lookup-Performance

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment