Skip to content
Shauvik Roy Choudhary edited this page Jun 22, 2015 · 3 revisions

How to use

  1. Find complete XPath (root to node) by using uiautomatorviewer

  2. In your code, use withXPath("/new/xpath/string") or withXPath2("/new/xpath/string") to get the reference to the element in your espresso test.

Currently only complete xPath queries are supported. We plan to add support for selecting elements using shorthand // or using attributes @ selectors in future.

Implementation Details

XPath Matching with Espresso is Tricky!

In uiautomatorviewer, you see a different tree than what you see while espresso is running the test. We will call these as [UIA Hierarchy] and [Runtime Hierarchy].

Here are some of the differences between these:

  1. The uiautomatorviewer tree reports base classes (e.g., android.view.View) whereas espresso reports instances (e.g., com.android.internal.widget.ActionBarView)

  2. The nodes in the two trees are differently ordered (e.g., Action bar comes before the app content in UIAutomatorViewer tree whereas it comes after the app content in instrumentation tree)

We've solved this by adding our custom xPath Matching that takes into account these differences while doing the matching. It is implemented in the following 2 methods:

  1. withXPath - right to left matching for xPath expression (leaf to root node)

  2. withXPath2 - left to right matching for xPath expression (root node to leaf)

Both these functions take the XPath and traverse the view hierarchy to find the node representing the element that is the right fit for the XPath. Currently, the functions accept the entire XPath expression from the root to the node in the form of [/element_class]+

One example of Matching XPath expression (constructed from UIAutomator View hierarchy) and the corresponding node's real XPath in the runtime view hierarchy are shown below:

  • [UIA XPath] = ``

  • [Runtime XPath] = ``

We will add support for // and @ in the XPath expressions based on User Interest.

Clone this wiki locally