Airtest for WebUI automated testing (selenium)

1. Introduction

        AirtestIDE supports connecting to the chrome browser for automated testing of web pages. The library it uses is airtest-selenium, which is a layer of encapsulation of the python library of selenium. The essence is selenium automated testing.
        By default, the selenium window is not displayed. You need to find the window in the top menu bar of the IDE, and check the last selenium window option to display the selenium window on the left side of the IDE:

Each function inside corresponds to a piece of python code!
 

2. Working principle

        airtest integrates Chrome browser, conducts web automated testing based on selenium, obtains python scripts through recording, and calls Chromedriver to drive the browser to perform specified actions; therefore, test scripts can be generated by directly operating the browser

3. Environmental preparation

        Airtest comes with its own chromedriver driver, but if the version does not match, it may report an error that the driver cannot be found; therefore, it is necessary to find the driver corresponding to the browser version and overwrite the old version of Chromedriver in the root directory of airtest.
        1. Python and related environment variables
        2. Copy airtest_selenium to the python directory
        3. Related libraries: airtest, selenium, pynput, etc.
        4. Run command: python -m airtest run script name

        Tool setting: set the chrome path in the options--settings in advance;

4. Record script

        selenium window provides the following auxiliary keys to help us quickly generate airtest-selenium/selenium scripts:
        
        start_web: Quickly record the script to open the URL of the current tab page
        touch: Quickly record the script to locate and click on elements using find_element_by_xpath     
        driver.airtest_touch: Quickly record screenshots Method to click on the element's script
        text: quickly record the text input script of the search element
        assert: use the x path to locate the element, assert whether the element exists
        driver.assert_template: use a screenshot to assert the existence of the element
        snapshot: take a screenshot of the current web page
        previous_tab: toggle Go to the previous open tab page
        new_tab: switch to the newly opened tab page
        back: web page back
        forward: web page forward

start_web: Open the page in the browser; generate the following code in the editing window, modify the web address to open the specified page

driver.get("Write your test web address!")

touch: click operation, call the click() method, and generate the corresponding selenium statement; when the mouse is clicked, airtest/selenium monitors the position of the click, obtains the object through the absolute path/id, etc., and generates a python statement;

#通过 绝对路径找到元素,然后调用点击方法 实现touch
driver.find_element_by_xpath("路径").click()
#通过获取元素id,获取元素,然后调用点击,实现touch
driver.find_element_by_id("id").click()

text: Quickly record the text input script of the search element; equivalent to calling selenium's send_keys(text, keyboard) operation; two parameters: text: the input text, keyboard: key value, the operation directly done by the keyboard after entering the text; generate a statement :

#找到kw这个id的组件,输入“测试” ,按下回车键;
#模拟的是在百度首页,输入测试,按下回车
driver.find_element_by_id("kw").send_keys("测试",keys.ENTER)

assert: use x path to locate elements, or use id to locate elements to assert whether an element exists; for example, if you want to open a Baidu page, then judge whether the page is successfully opened; generate a statement:

#断言,是否存在id为 kw 的元素;第三个参数是填写测试点名称
driver.assert_exist("kw", "id", "请填写测试点.")

   snapshot: Save a screenshot of the current web page; it can be checked in the test report; generate a statement:

driver.snapshot()

 driver.airtest_touch: Click on the picture, click through image recognition, pass in the picture, and then recognize the picture; click on the picture; generate a statement:

#点击图片,截图,把图片放到括号里。
driver.airtest_touch()
'''
driver.airtest_touch(Template(r"tpl1652605973720.png", record_pos=(0.388, -0.007), resolution=(1920, 1080)))
'''

  driver.assert_template: Use screenshots to assert the existence of elements; different from the above airtest_touch, template to Jiaoyang exists, do not click; generate statements:
 

#第一个参数是图片,第二个参数是测试点名
driver.assert_template(Template(r"tpl1652606518115.png", record_pos=(0.354, -0.014), resolution=(1920, 1080)), "请填写测试点")

Page operation:

'''
        previous_tab:切换到上一个打开的标签页
        new_tab:切换到新打开的标签页
        back:网页回退
        forward:网页前进
'''


driver.switch_to_previous_tab()
driver.switch_to_new_tab()
driver.back()
driver.forward()

Guess you like

Origin blog.csdn.net/guanrongl/article/details/124776238