1. Background
The experiment object that this article tries to use is the SDK comes with the Notepad application instance, suppose already has two notes namely "Note1" and "Note2" to add to Notepad above, what we want to do is try to use XPath method to locate "Note2" This listview below the TextView control.
Note that there are 3 textview types of controls on the interface:
- Top of the Textview,index as the entire ListView title text for "Notes" is 0
- The middle text for "Note2" of the Textview,index is also 0
- The bottom text for "note1" of the Textview,index is 1
2. XPath anchor element Instance 2.1 Gets the control by judging the control property
All properties of a control can be used as a judgment, such as whether its Text,index,resource-id is clickable, and so on, for example:
2.1.1 Finding a target control with text
el = Driver.findelementbyxpath ("//android.widget.textview[contains (@text, ' Note2 ')]"); Assertthat (El.gettext (), Equalto ("Note2"));
2.1.2 to find the target control by index if we use index to find the control Note2 as in the following way, it will fail because the XPath simply says "I'm looking for a control with index 0 on the Android.widget.TextView type on the page. "But as the background says we actually have 3 TextView controls, the top and the middle controls whose index is 0. So the final control is actually the first one to find the top text" Notes "TextView.
el = Driver.findelementbyxpath ("//android.widget.textview[contains (@index, 0)]"); Assertthat (El.gettext (), Equalto ("Note2"));
Then we'll have to figure out a way to add more paths, so that XPath can tell what is needed is the TextView of index 0 below, not the one above. Looking at the hierarchical structure of the uiautomatorviewer control, we find that the two TextView are forked from the linearlayout, so we should start with this path by specifying the array subscript we need to " Under LinearLayout the second framelayout below the listview below the TextView of index 0:
el = Driver.findelementbyxpath ("//android.widget.linearlayout[1]/android.widget.framelayout/ Android.widget.listview/android.widget.textview[contains (@index, 0)] "); Assertthat (El.gettext (), Equalto ("Note2"));
"Appium" locates elements based on XPath