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

Recently, in our q&A group, we often encounter such situations:

  • The Airtest API was forced onto the Poco control by mistake
  • The Poco API was mistakenly imposed on the Airtest screenshot script
  • Write a script that you think you’re going to run, regardless of how the API is actually used

So today we’ve sorted out some of the common API mistakes, hoping to help you avoid a few mistakes when writing scripts.

A summary of easy to use API errors

1. Exists for mixing Airtest and Poco frameworks ()

A method with high error probability!! First, an example of a mistake:

A typical case of Airtest’s API being forced on Poco controls. The expectation of this student is that he wants to determine if this control exists and do something.

But his problem is the incorrect use of the EXISTS method.

To make it clear, both Airtest and THE Poco framework use this API to check for the presence of something on the current page, but Airtest exists to check for the presence of a screenshot object, as follows:

Exists (Template(r"tpl1622704594490. PNG ", record_pos=(-0.185, -0.211), resolution=(810, 1440)))Copy the code

The EXISTS of the Poco framework is used to detect the presence of a control on the current page as follows:

poco(text="poco").exists()
Copy the code

The correct way to write it is:

if poco(text="poco").exists():
    pass
Copy the code

The second thing to note is that the two apis return slightly different values:

Poco (text="poco").exists() False exists(Template(r"tpl1622704594490. PNG ", record_pos=(-0.185, -0.211), resolution=(810, 1440)))Copy the code

Airtest exists with the condition if:

As you can see, there is no “Settings” screenshot target on the current page, but the script does run inside the “if”, indicating that the “if” passed. Why is that?

When the script is run to exists method, one image recognition algorithm finds a result with a confidence of 0.7039xx, which is greater than the default image threshold 0.7. Therefore, the result of this exists is to return the coordinate of the result, resulting in the if statement passing.

This is very common, and students often wonder why my if statement doesn’t work. In fact, no, the solution is also very simple: raise the threshold of the image and filter out the results that may be misjudged:

If exists(Template(r"tpl1622705519946.png", threshold=0.8, record_pos=(0.143, -0.342), resolution=(810, 1440))): Print (" found ")Copy the code

Raise the threshold to 0.8 to filter out errors greater than 0.7, and the script should be as expected:

2. Write your own imaginary script

As usual, first look at the wrong demonstration:

The Poco framework’s exists method returns True and False. Booleers do not have click methods. Only UI controls have click methods.

This is a classic case of scripting not according to syntax, but according to your own imagination. So the correct way to write it is also very simple:

# Write separately!! Poco (text="poco").click() # or if poco(text="poco").exists() : poco(text="poco").click()Copy the code

The student’s expectation should be to wait for the control to appear and then click on it:

An empty object error is reported here. Let’s look at the API documentation for wait_for_appearance:

As you can see, wait_for_appearance does not return any value, so it is not possible to follow the click method.

Poco (text="poco").wait_for_appearance() poco(text="poco").click()Copy the code
3. Forcing Poco apis onto Airtest screenshots

Take a look at the wrong example:

While wait_for_appearance() is a UI control (Poco), we need to use the wait() method of the Airtest framework:

ImgTemplate (r"tpl1622713460444.png", record_pos=(0.148, -0.346), resolution=(810, Text =" poCO ".wait_for_appearance()Copy the code

summary

I hope you can check out the API documentation of Airtest and Poco before writing the script to see the API details and sample scripts.

Finally, the API documentation links for these two frameworks are attached, if you need them, please help yourself:

  • Airtest API documentation: Airtest. Readthedocs. IO/zh_CN/lates…
  • Poco readthedocs. IO /zh_CN/lates…

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

Official Q group: 654700783

Ah, so serious all see here, help in the left side of the article click on the likes and favorites, give me a support, ash often thank ~