APP automatic positioning method

The difference between xpath positioning and the web:

Put a picture first:


First, appium 1.5 and later versions have abandoned the name attribute (such as name=billing, which will not be supported for positioning), so the basic positioning is just using the id. Not much else to say.

Second, let's talk about the positioning of xpath. The main scenario is when there is no id or no text, or when the text is an uncontrollable value (or a value that will change, for example, the text field is 10 yuan, and this 10 may
change every time). In fact, the simple point is to locate by path, including one-level or multi-level paths. By the way, there are two path modes, one is an absolute path (with the first label as the reference), the other is a relative path
(with other known labels as the reference), and try to locate it as much as possible. Use relative paths.

1. Let's talk about the use of xpath in scenes with id or text. (Why not use id or name directly? The following are relative paths)

The name mentioned above is abandoned, but the writing method of xpath such as //android.widget.TextView[@text="bills " ] is supported.

For example, the ids of "Bill" and "I want" above are both com.wlqq:id/title_left_btn, and assuming that the current page only has these two position ids written earlier, then when you use id to locate "Bill" , you can use xpath, because the id is no longer unique. The "bills" are located by id: xpath=(//android.widget.TextView[ @ resource-id = "com.wlqq:id/title_left_btn"])[1], and the "I want" is located:
xpath= (//android.widget.TextView[ @ resource-id ="com.wlqq:id/title_left_btn"])[2]
Note three points here:
a, the subscript starts from 1, not 0;
b, if There are subscripts, you need to enclose the previous part with parentheses, and you need to add xpath= in front. Maybe some people are used to adding xpath= in front, but like me, you are only used to writing // at the beginning, not xpath= Been pitted. . . Anyway, it's not easy to find because I didn't write xpath=, or it may be my own pit.
c, which is different from the web is the value of the label. Here, the value of class = android.widget.TextView is taken instead of the label TextView seen. The specific reason is not investigated. Anyway, remember to use class instead of tags.
In addition, the above is just to illustrate the usage of xpath when there is only one level, and one level can be regarded as a relative path. Because it doesn't start writing from the attribute at the first position. The writing rules of xpath are basically as few as possible. So the fewer layers the better. If you have 1 layer that can be uniquely positioned, you don't need 2 layers. Might be a bit crap.

2. Now let's talk about the scene without id or name. Let's take a picture first:


Now there is a scenario where I need to click on the little man icon above, but he has no id and text attributes. The only way to think of it is to talk about the xpath below.
The writing method of absolute path is: if the first one on the graph is the top, it is

In this way, 7 levels are required, which are written in turn:
//android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.RelativeLayout/android.widget.RelativeLayout/android.widget .LinearLayout[2]/android.widget.ImageButton
This way of writing, pay attention to the following points:
a , [2] Note that it is 2 instead of 3, because it is related to the value of the label. There are only 2 LinearLayouts.
b , the length of the path is too long, and because there is only the value of the class, there may be more than one for some page controls, that is, this writing method may not be the only one.
c , the absolute path is rarely used. If the character is too bad, there is no id or name on the page, then there is no way. Or consider some coordinates.

3. (Important) Use relative paths to locate in scenarios where there is no id or name. It mainly introduces the parent-child relationship (subordinate and superior) and sibling relationship in the hierarchical relationship.
    As you can see, there is a unique Chinese word in this picture - "wallet". We can use this wallet to locate our villain picture. First analyze the positional relationship

. Looking for the relationship is as shown in the figure, the villain icon 3 is the ImageButton, the son of the 2LinearLayout label, the brother of wallet 1. The son is easy to understand, and the hierarchical relationship of xpath is also represented by the parent-child relationship. //android.widget.LinearLayout/android.widget.ImageButton This can represent the son of the younger brother. But now the question is how to represent the younger brother of the wallet? There is an axis in xpath, which can be simply understood as a function. I think so. preceding-sibling:: You can find the elder brother node in front of the node, following-sibling:: You can find the younger brother node behind the node. For more usage of axes, you can use Baidu xpath syntax. There is also a more used parent:: , which can find the parent node of the node. But the parent node can be represented by ... Let's talk about how to use it in detail:
    the basic knowledge has been introduced here. Then the positioning method here is the 3 levels in the above figure: //android.widget.TextView[ @ text = "wallet"]/following-sibling::android.widget.LinearLayout/android.widget.ImageButton. The first level is the only place to find the wallet as mentioned above, and the next level is the younger brother of the wallet, that is, following-sibling::android.widget.LinearLayout. Of course, note that because it is next to each other, the younger brother did not get off work. It is conceivable that if it is the first younger brother, add a subscript. Brother is the same.
The relationship between the brothers was used in the front, and the relationship between the son and the father is described below. The relationship between father and son is still illustrated with diagrams

. Our wallet 1 father 2 has a son 3 son 4 is our villain icon. This is looking for relationships. The relationship is found, then we can use this relationship to write xpath. That is , the second class = android.widget.LinearLayout 's son (/android. widget.LinearLayout[2]) son (little person/android.widget.ImageButton), well, we connect it: //android.widget.TextView[ @ text = "wallet"]/parent::android.widget. RelativeLayout/android.widget.LinearLayout[2]/android.widget.ImageButton. By the way, the position of father can be replaced by .., compared to many people who know that .. in the path refers to the superior. So you can use //android.widget.TextView[ @ text = "wallet"]/../android.widget.LinearLayout[2]/android.widget.ImageButton instead of the above writing.
  Note: Finally, let me emphasize again, why the subscript is [2] about this place, because it is only related to the same class. The class of the wallet is different. So it doesn't count. 
   About the parent-child relationship of relative paths, as well as the sibling relationship, everyone should have some experience. If you still don't understand, let's take a more complicated example. Probably just to illustrate the syntax below. Actually the following may not be so complicated to write. First picture:

   Assuming that we need to locate our immediate positioning button by adding the position to the shopping cart, then one of our writing methods is the 7-level relationship on the diagram. That is, add to cart 7 (//android.widget.TextView[@text="add to cart"]) parent 1 (/..) parent 2 (/..) parent 3 (/.. ) 2nd brother 4 (/following-sibling::android.view.View[2]) son 5 (/android.view.View) son 6 (which is our Buy Now /android.widget.TextView), The connection is
    //android.widget.TextView[ @ text = "Add to cart"]/../../../following-sibling::android.view.View[2]/android.view.View/ android.widget.TextView.
Note: When using text, avoid using the default input value of the input box, because when you actually enter the value, there is no text, and the path cannot be found. In addition, fuzzy matching can also be used, xpath has a contains function. Usage //android.widget.TextView[contains( @ text , "shopping cart")]. You can also find the "Add to shopping cart" position. Go experience it yourself. . .
  In the end I don't know if you understand it? Welcome friends to leave a message and correct me. I also like technical exchanges.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324602288&siteId=291194637