Original address: http://blog.csdn.net/zhubaitian/article/details/39754233
Appium has a number of ways to get controls, as described in the article, "Appium-based control localization practices for various findelement on Android." One is to locate the control based on the XPath of the page on which the control resides.
This article attempts to experiment with the Appium how to use XPath to locate the control, if there is a wrong place, please point out.
1. Background the experiment object used in this paper is the SDK's own Notepad application example, assuming that there are two notes respectively "Note1" and "Note2" added to Notepad, we have to 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 gets control way 2.1 by judging the control property gets all the properties of the control control can be used as a judgment, such as whether its text,index,resource-id is clickable, etc., for example: 2.1.1 To find the target control by text
[Java]View Plaincopy
- 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.
[Java]View Plaincopy
- 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:
[Java]View Plaincopy
- el = Driver.findelementbyxpath ("//android.widget.linearlayout[1]/android.widget.framelayout/ Android.widget.listview/android.widget.textview[contains (@index, 0)] ");
- Assertthat (El.gettext (), Equalto ("Note2"));
2.1.3 to find the target control TBD through other properties, these are not listed, the implementation of the above two examples can be
2.2 Obtaining a control by specifying the location of the selection collection in my practice and understanding, getting controls in this way is unreliable, at least two questions.
- Issue 1: The control's position in the collection is not controllable.
[Java]View Plaincopy
- el = Driver.findelementbyxpath ("//android.widget.textview[1]");
- Assertthat (El.gettext (), Equalto ("Note2"));
In the above example, I would like to find out the middle of the upper and lower 3 textview in the Uiautomatorviewer, but the result is to return to my really top "Notes".
- Issue 2: Finding a collection of specified elements is not effective by increasing the path layer limit.
[Java]View Plaincopy
- el = Driver.findelementbyxpath ("//android.widget.framelayout[1]/android.widget.linearlayout[1]/ Android.widget.framelayout/android.widget.listview/android.widget.textview[0] ");
- Assertthat (El.gettext (), Equalto ("Note2"));
The above example is intended to indicate that the "I want the control is the first of the two TextView controls under the ListView" by increasing the limit of the parent path, but returned to me is "an unknown server-side error occurred while Processing the command ", the feeling is crossed out of the look. 2.4 XPath Usage official manual As for other non-mainstream access to the control by XPath, please refer to the official XPath manual in Chinese version: http://wenku.baidu.com/link?url= Tldn9x4ucgmzyykj4dpsgsdbshxew6w8btyqqaxd0ktmy6lbqqliil9fhtczmdieo8o5zkv-fzlw96mfgvmwz-qz35epobyv8hkltuyxbv3
"Go" appium to get control instances based on XPath essay