Skip to content

8.2 Airtest-Selenium

The use of the selenium plugin in Airtest-IDE has been described in detail in "Web Automation Testing" in Chapter 1.This part of the document will mainly introduce the structure of the Airtest-Selenium test framework and the use of some API.

1. selenium introduction

Airtest-Selenium is based on the selenium syntax, so this chapter first introduces the selenium framework briefly (those who know more about selenium can skip this section).

Detailed explanation of selenium instance

A simple example to see how selenium works.Case content: open the python official website and search:

from selenium.webdriver import Chrome
from selenium.webdriver.common.keys import Keys

driver = Chrome()
driver.get("http://www.python.org")
elem = driver.find_element_by_name("q")
elem.send_keys("pycon")
elem.send_keys(Keys.ENTER)
driver.close()

The following will analyze the above selenium case step by step:

from selenium.webdriver import Chrome
from selenium.webdriver.common.keys import Keys

The selenium.webdriver module provides all WebDriver implementations. The currently supported WebDrivers are: Firefox, Chrome, IE and Remote. The Keys class provides support for keyboard keys, such as:ENTER / F1 /ALT, etc.

driver = Chrome()

Create an instance of Chrome WebDriver. When the script runs here, it will open a chrome browser.

driver.get("http://www.python.org")

The driver.get method will open the address filled in the URL. The WebDriver will wait until the page is completely loaded (actually wait until the onload method is completed), and then return to continue executing your script.

python elem = driver.find_element_by_name("q")

WebDriver provides a lot of methods for you to query Dom elements in the page, such as: find_element_by_ *.For example: the input box containing the name attribute can be found using the find_element_by_name method, and finding elements is also the core of Selenium, and it is also the core problem to be solved in the recording work described below.

elem.send_keys("pycon")
elem.send_keys(Keys.ENTER)

These two lines of code send a keyword, and send_keys acts like you would use a keyboard to enter keywords. Special keys can be entered using the Keys class, which inherits from selenium.webdriver.common.keys.

driver.close()

Finally, to close the browser window, you can also use the quit method instead of the close method. Quit method will close the entire browser, and close method will only close a tab.If you have only one tab open, the default behavior of most browsers is to close the browser.

  Learn the working method of selenium through the above simple example. It first locates the page elements in the current dom tree that need to be operated, and then performs user simulation operations on them, such as clicking, typing, and so on.

2. Airtest-Selenium framework

Airtest-Selenium is a layer of encapsulation of Selenium's Python library:

  • Added some image recognition interfaces;
  • The switching interface of the multi-tab page is also encapsulated by the corresponding API;
  • When the core api runs, it will automatically generate log and a web version test report.

Image recognition interface

airtest_module

Airtest-Selenium has two interfaces to the image recognition package, image recognition click and image recognition assertion.These two interfaces are based on the image recognition package of the Airtest framework. If the corresponding script cannot find the corresponding image in the webpage when the image script is run, a Target not found on screen exception will be thrown.

Multi-tab recording

Selenium provides an interface for switching tabs.

driver.switch_to.window(driver.window_handles[number])

After this statement is executed, you can switch to the numbered open tab.For users, this interface is not so easy to understand and call, because it needs to remember the order in which the tags are opened.And most of the time, the operation of switching tabs generally occurs in two cases: open a new window, close the tab. Therefore, Airtset-Selenium encapsulates two interfaces:

driver.switch_to_new_tab()
driver.switch_to_previous_tab()

Inside this interface, Airtest-Selenium maintains the organizational structure of the tabs.The user only needs to call switch_to_new_tab when opening a new tab page.In addition, when ending the current tab page and returning to the previous tab page, you can call switch_to_previous_tab ().And you no longer need to think about the problems such as what the current page is .

Reporting module

3_report

The interface encapsulated by Airtest-Selenium will generate the corresponding report after running. You can directly click the Generate Report button in AirtestIDE to view the corresponding report content.

If you want to use the command line to generate reports (requires that the local Python environment has installed the airtest library and airtest-selenium repository), you can refer to airtest documentation, and note that you need to add the parameter - plugin airtest_selenium.report.

3. How to run Airtest-Selenium from the command line

This is the same as running the airtest script, but it is easier and does not need to specify a device:

>python -m airtest run untitled.air

Note: You need to copy airtest_selenium from the root directory of Airtest-IDE and put it in the python path (It is not open source for the time being. After open source, you can download it directly through pip).

The root directory of the Mac is in / Application / AirtestIDE.app / Contents / MacOS.

4. The api of Airtest-Selenium

The contents of this section are the API encapsulated by Airtest-Selenium. The import method is included in the initialization code of Airtest-IDE.

from airtest_selenium.proxy import WebChrome

proxy api

class WebDriver(executable_path="chromedriver", port=0, options=None, service_args=None,desired_capabilities=None, service_log_path=None,chrome_options=None)

Base class: selenium.webdriver.Chrome

loop_find_element(func, text, timeout=10, interval=0):

Loop to find the target web element by func.
- parameters: · func - function to find element
        · text - param of function
        · timeout - time to find the element
        · interval - interval between operation
- returns: element that been found

find_element_by_xpath(xpath)

Find the web element by xpath.
- parameters: xpath - find the element by xpath. 
- returns: Web element of current page.

find_element_by_id(id)

Find the web element by id.
- parameters: id - find the element by attribute id.
- returns: Web element of current page.

find_element_by_name()

Find the web element by name.
- parameters: name - find the element by attribute name.
- returns: Web element of current page.

switch_to_new_tab()

Switch to the new tab.

switch_to_previous_tab()

Switch to the previous tab(which to open current tab).

airtest_touch(v)

Perform the touch action on the current page by image identification.

- parameters: v – target to touch, either a Template instance or absolute coordinates (x, y)
- returns: Finial position to be clicked.

assert_template(v)

Assert target exists on the current page.

- parameters: v – target to touch, either a Template instance
- Causes: AssertionError - if target not found.
- returns: Position of the template.

assert_exist(param, operation)

Assert element exist.
- parameters: · operation - the method that to find the element.
        · param - the param of method.
- Causes: AssertionError - if assertion failed.

get(address)

Access the web address.
- parameters: address - the address that to accesss.

back()

Back to last page.

forward()

Forward to next page.

snapshot(filename=None)

Snapshot current page.

- parameters: filename - the snapshot path.

more api

Because the Airtest-Selenium driver inherits the Selenium driver, the selenium api can be used directly. Related API can be found in selenium documentation.