-
Notifications
You must be signed in to change notification settings - Fork 0
XPath Matching
-
Find complete XPath (root to node) by using
uiautomatorviewer
-
In your code, use
withXPath("/new/xpath/string")
orwithXPath2("/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.
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:
-
The uiautomatorviewer tree reports base classes (e.g., android.view.View) whereas espresso reports instances (e.g., com.android.internal.widget.ActionBarView)
-
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:
-
withXPath - right to left matching for xPath expression (leaf to root node)
-
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.