preface

Selenium is an automated testing tool for Web applications. It runs directly in the browser and can simulate the behavior of users in the browser.

In AirtestIDE, the developer has also given us a window to use Selenium. However, the framework used is the Airtest-Selenium framework. Airtest-Selenium is a wrapper around Selenium’s Python library, which adds a partial image recognition interface and can also generate test reports in web version.

In this article, we will use the Selenium window on the IDE to show you how to automate testing on the browser.

Selenium window in the IDE

Call up a window for Selenium in the AirtestIDE

By default, the IDE does not display Selenium Windows, so you need to check Selenium Window in the Window menu at the top of the IDE, and then the Selenium Window will be displayed on the left side of the IDE interface.

Shortcut button for inserting initial code

Selenium Window provides us with some common buttons, such as the Earth-like button under the Window, which automatically inserts some initialization code in the script editing Window:

Before inserting code, we need to select Yes in the yellow provide box that pops up in the script window to allow inserting code:

The script editing window inserts the following code automatically:

Introduce selenium's WebDriver module
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from airtest_selenium.proxy import WebChrome

# Create an instance, the code runs here, will open a Chrome browser
driver = WebChrome()
driver.implicitly_wait(20)
Copy the code

Note that if you are using this button for the first time, the IDE will pop up and say you need to set the browser path:

In this case, you need to go to Options — Settings, find Selenium, and set the path of Chrome to insert the above code.

Common methods of Selenium

Open the specified url

Let’s take baidu home page as an example:

driver.get("https://www.baidu.com/")
Copy the code

The driver.get() method opens a specified url in the browser, passing it the url address.

Maximized window
driver.maximize_window()
Copy the code
Close the window/browser
If the browser has only one window, the browser will also be closed
driver.close()

Exit driver close all Windows
driver.quit()
Copy the code

Airtest – Selenium profile

As mentioned above, Airtest-Selenium is based on the Selenium syntax, but the Airtest developers have also encapsulated the following methods:

Image recognition interface

Airtest-selenium encapsulates image recognition with two interfaces, image recognition click and image recognition assertion:

① Click the airtest_touch button below Selenium Window

Capture the image you want to click on the browser page, double-click to complete the screenshot, the script editing window will automatically generate an image recognition click script

driver.airtest_touch(Template(r"tpl1582031994893.png", record_pos = (8.99, 5.23), resolution = (100, 100)))Copy the code

② Click the airtest_touch button below Selenium Window

Capture the image you want to assert on the browser page, double-click to complete the screenshot, the script editing window will automatically generate an image recognition assertion script

driver.assert_template(Template(r"tpl1582032716811.png", record_pos=(0.51, 1.315), resolution=(100, 100),"Airlab website opened successfully.")
Copy the code

For more assertion examples of Selenium, check out our previous tweet “Testing What Everyone should Know about Assertion…” .

It is worth noting that these two interfaces are image recognition encapsulation based on the Airtest framework. If the image script cannot find the corresponding image in the web page, it will throw the exception Target Not found on screen.

Multi-tab recording

Selenium provides an interface for switching tabs.

driver.switch_to.window(driver.window_handles[number])
Copy the code

After this statement is executed, you can switch to the number open TAB. However, this interface is not easy to understand and invoke for users, because it requires them to remember the order in which tags are opened.

Most of the time, the operation of switching tabs generally occurs in the following two situations: opening a new window and closing a TAB. Therefore, Airtset-Selenium encapsulates two interfaces:

driver.switch_to_new_tab()
driver.switch_to_previous_tab()
Copy the code

Within this interface, Airtest-Selenium maintains the organization of the tabs. The user simply calls switch_to_new_tab when opening a new TAB.

In addition, when ending the current TAB and returning to the previous TAB, switch_to_previous_tab() can be called, no longer need to consider the current number of such issues.

Generate a report

An interface encapsulated by Airtest-Selenium will generate a corresponding report after running. You can click the generate report button in the AirtestIDE to view the corresponding report content.

summary

Let’s sum up today’s lecture with a small practical case:

# -*- encoding=utf8 -*-
__author__ = "19617"

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)

driver.get("https://airlab-gl.163.com/b2b")

driver.maximize_window()

driver.assert_template(Template(r"tpl1582032716811.png", record_pos=(0.51, 1.315), resolution=(100, 100),"Airlab website opened successfully.")

driver.airtest_touch(Template(r"tpl1582034527805.png", record_pos = (8.975, 5.765), the resolution = (100, 100))) driver.switch_to_new_tab() driver.switch_to_previous_tab() driver.quit()Copy the code

trailer

The Selenium quick-and-dirty | the next battle Will give you about the following:

① Chrome debugging tools ② Selenium element positioning ③ Selenium interaction with the page