This article is from the official account of the project: “AirtestProject” Copyright notice: It is allowed to be reproduced, but the original link must be retained. Do not use it for commercial or illegal purposes

preface

Many of us have asked questions about element positioning, such as: What attributes can be used to locate elements? What happens when an element cannot be located using node attributes? Why can’t a node be found when it clearly exists? .

Therefore, we have specially compiled this article to describe in detail several ways of positioning POCO elements, hoping that they can be used flexibly in practical projects.

Use the IDE’s POCO helper window to generate element location scripts

The POCO helper window of AirtestIDE provides us with many ways to generate element location scripts. The easiest way to do this is to click the Record button in the POCO auxiliary window, and then click the target element:

The script window will then automatically generate a script for clicking on the control, leaving poCO (“star_single”) as the code for locating the yellow star image element.

Of course, we can also use the two inspector buttons next to the record button to help us locate elements. The difference between the two inspector buttons is whether the current page is locked:

We can see that as the inspector locates an element, the UI tree in the auxiliary window also locates the corresponding element. In this case, we can double-click the corresponding location of the UI tree to automatically generate the script for locating the element in the script editing window:

Element positioning using basic selectors

As you can see from above, the element location scripts that the IDE automatically generates for us are in the form of POCO (” XXX “).

In fact, by adding a pair of parentheses after the POCO instance, we can make element selection. The selector iterates through all the elements, picking out and returning those that meet the given criteria.

The parameters in parentheses are the given conditions, represented by attribute name-value pairs, where the first parameter represents the node name, like POCO (“star_single”). It can also be followed by some optional parameters that represent the node’s attributes and expected attribute values:

poco("star_single".type="Image")
Copy the code

Element location using relative selectors

If you can’t select the element you want directly using node attributes (or just using basic selectors), you can also select the elements by using the render hierarchy between elements, such as parent-child relationships, sibling relationships, ancestor-descendant relationships, and so on:

poco("plays").child("playBasic").offspring("star_single")
Copy the code

More information about the methods API for father-child and sibling relationships can be found on this page: poco.readthedocs. IO /zh_CN/lates…

Use spatial order selectors to locate elements

Selection by ordinal (order) is always arranged in spatial order, first from left to right, and then from top to bottom line as before. As shown in the following figure, we select a number of elements with type=”Text” using the selector, and then select each element individually using the index order:

name0 = poco("Content").child(type="Text")[0].get_name()
name1 = poco("Content").child(type="Text")[1].get_name()
name2 = poco("Content").child(type="Text")[2].get_name()

print(name0+""+name1+""+name2)
Copy the code

An exception to index selection is that once the selection has been made, if the position of the element has changed, the subscript number is still the value determined at the moment of selection. That is, if the selected element ① is now at ③, then poCO (…) is still used. [0] to access instead of poCO (…) [2].

If an element disappears (removed from the interface or hidden) after the selection, an exception may occur if that element is accessed again, and the rest of the elements can continue to be accessed.

summary

In practice, we do not need to stick to a single selector for element location. Instead, we can use multiple selectors to help us locate an element accurately based on the actual situation.

In addition, sometimes the positioning script generated by the recording function is very long and the positioning hierarchy is too complex, so it is easy to generate the following errors:

At this point, we can choose a less complicated element positioning method based on the structure of the UI tree.


Airtest website: airtest.netease.com/ Airtest tutorial website: airtest.doc.io.netease.com/ build enterprise private cloud service: airlab.163.com/b2b

Follow our official account below to see more of our previous tutorials, and reply to “Chat Group” to join our official developer chat group