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

1. Introduction

Many students choose to use Airtest-Selenium for Web automation because they want to use some of the image recognition methods encapsulated in Airtest-Selenium, such as image clicks, image assertions, screenshots, and so on.

However, in the practical application process, students may encounter some problems. Here we will discuss in detail some problems that students are likely to encounter when using Airtest-Selenium for Web automation testing and their solutions.

2. Basic concepts and usage

1) Airtest-Selenium is the standard Selenium

In fact, Airtest-Selenium is standard Selenium, but we have encapsulated some additional methods about image recognition and TAB switching in this library:

  • airtest_touchImage click
  • assert_template: asserts that the image exists
  • snapshot: Take a screenshot of the page
  • switch_to_previous_tab: Switches to the previous open TAB
  • switch_to_new_tab: Switches to the newly opened TAB
  • .
2) Use the auxiliary window provided by AirtestIDE

To help you get started, we have provided an auxiliary window of the AirtestIDE, which is not displayed by default.

However, when we need to write Web automation test scripts, we can check Selenium Window on the top menu bar of the IDE.

These shortcuts help us record/quickly produce Web automation test scripts. Note, however, that in order to use the record/shortcut features of the accessibility window, we need to click the initialization button to open a Chrome window and record the script in that window.

For a detailed introduction to Airtest-Selenium, please refer to our previous tweets:

  • Use the airtest-Selenium in the AirtestIDE

  • Selenium common API introduction

3) Combine more Selenium apis

In addition, it is highly recommended that you learn some selenium basics before using Airtest-Selenium for Web automation testing.

This allows us to combine Selenium’s richer apis with richer and more complex automated test scripts. (There are extensive tutorial documents about Selenium available online, which students can find on their own.)

3. Frequently Asked Questions

1) An error was reported using the image method encapsulated by Airtest-Selenium

Error: Name ‘Template’ is not defined (name ‘Template’ is not defined)

Usually because we forgot/mistakenly deleted the method to import the Airtest library, we can add at the beginning of the script:

from airtest.core.api import *
Copy the code
2) Confuse Airtest – Selenium WebChrome() with Selenium webDriver.chrome ()

In the Airtest-Selenium library, we can instantiate a Chrome object like this:

from airtest_selenium.proxy import WebChrome
driver = WebChrome()
Copy the code

In selenium, we instantiate Chrome in the following way:

from selenium import webdriver
driver = webdriver.Chrome()
Copy the code

If we want to use image methods encapsulated by Airtest-Selenium, such as Airtest_Touch, Snapshot, and Assert_template, we must instantiate the WebChrome classes provided by Airtest-Selenium.

AttributeError: ‘WebDriver’ object has no attribute ‘snapshot’

3) Airtest-Selenium is not currently supported on Mac

Airtest-selenium is not currently supported on Mac.

Current OS is not 'Windows'! You can't use airtest function of Airtest-Selenium. > <
Copy the code

4) Airtest – Selenium reporting plug-in

When generating reports for Airtest-Selenium (Web Automated Testing), we need to load specialized reporting plug-ins.

If we use the view report button of AirtestIDE to generate it, it will automatically load it for us:

Plugin airtest_selenium. ReportCopy the code

If we use scripts to generate test reports, we need to specify the plugins parameter manually:

from airtest.report.report import LogToHtml

h1 = LogToHtml(script_root=r'D:\test\report01.air', log_root=r"D:\test\report01.air\log", export_dir=r"D:\test\report02" , lang='en', plugins=["airtest_selenium.report"])
h1.report()
Copy the code
5)unknown error: cannot find Chrome binary

This is one of the most frequently asked questions by students using Airtest-Selenium for Web testing. Normally, we can open a Chrome Window by clicking the open browser button in the Selenium Window of the AirtestIDE (because we have set the path of Chrome.exe in option-Settings beforehand) :

However, when executing the Airtest-Selenium script and running the code that opens the browser window, an error similar to the following is reported:

selenium.common.exceptions.WebDriverException: Message: unknown error: cannot find Chrome binary (Driver info: Aa6a5fdba159ac9f4c1e8cbc59bf1b5ce12b7 chromedriver = 2.37.544315 (730), the platform = Windows NT 10.0.19043 x86_64)Copy the code

If our Chrome browser is installed in the default path (and we highly recommend that you install chrome in the default path to save a lot of problems), we need to check the following two points when this error occurs:

  • Does the chromedriver match the environment variable directory location
  • Check whether the Chrome browser version matches the Chrome Driver version

If it is not in the correct directory or does not match the existing browser version, you can download the matching Chromedriver from this directory after viewing the installed Chrome version: Chromedriver.storage.googleapis.com/index.html.

If your Chrome browser is not installed in the default path, it is possible that Selenium could not find the binary Chrome. exe file and raise the above exception.

  • Uninstall the chrome browser and reinstall it to the default path
  • You can directly configure the Chrome installation directory into the environment variable without reinstalling the browser

Of course, we also support the use of code to specify the path of Chrome. exe or Chromedriver (2 versions must match) :

# -*- encoding=utf8 -*- __author__ = "AirtestProject" from airtest.core.api import * from selenium import webdriver from  selenium.webdriver.common.keys import Keys from airtest_selenium.proxy import WebChrome from selenium.webdriver.chrome.options import Options opt = Options() opt.binary_location = r"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" driver = WebChrome(options=opt,executable_path=r"C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe")Copy the code
6) The library is not installed when you run the airtest-Selenium script by leaving the AirtestIDE

If we use the AirtestIDE’s own environment to run a Web automation script, we can simply run it, because the embedded Python environment has the Airtest-Selenium library installed in advance.

However, if we specify to use a local Python environment or run a Web automation script out of the AirtestIDE, we need to install the Airtest-Selenium library in the corresponding Python environment:

pip install airtest-selenium
Copy the code

Error: No module named ‘airtest_selenium’

“No module named ‘Pynput'” : < pyNput >

pip install pynput
pip install airtest-selenium
Copy the code
7) Web automatic input and keyboard operation

Unlike on Android, iOS, and Windows platforms, we don’t use text for text entry in Web automation scripts, nor keyevent to simulate keyboard operations.

Instead, use send_keys. Here is a simple example. After opening the baidu home page, we locate the search text input box, enter a certain text, and then perform the keyboard enter operation:

# -*- encoding=utf8 -*- __author__ = "AirtestProject" from airtest.core.api import * from selenium import webdriver from  selenium.webdriver.common.keys import Keys from airtest_selenium.proxy import WebChrome driver = WebChrome() Driver. implicitly_wait(20) auto_setup(__file__) driver.get("https://www.baidu.com/") sleep(1.0) # Input text Driver.find_element_by_id ("kw").send_keys(keys.enter)Copy the code


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