Based on airtest - UI automation testing of selenium

1. Airtest-selenium environment construction

1.1 Installation and introduction

The airtest-selenium library is a further package based on the selenium library:
https://airtest.doc.io.netease.com/tutorial/13_Selenium/

pip install airtest-selenium
pip install pynput

Several features of the airtest-selenium library:
1) Friendly encapsulation of the interface for switching tabs,
2) Support for image recognition,
3) Automatic log recording (refer to selenium-java's listening mode),
4) Compatible with selenium's native api

1.2 Download browser and browser driver

This article uses the Chrome browser to download the corresponding browser driver chromedirver.exe

1) Check the chrome version

chrome://version/

2) Turn off the automatic update function of Chrome browser

If the browser version changes after the browser is automatically updated, this may invalidate the downloaded chromedirver.exe

Right-click My Computer - Manage - Services and Applications - Services - Stop: Google Update Service (gupdate), Google Update Service (gupdatem); Startup Type: Manual

3) Mapping relationship between chromedriver and chrome version

ChromeDriver版本    Chrome版本
    v2.44           v69-71
    v2.42           v68-70
    v2.41           v67-69
    v2.40           v66-68
    v2.38           v65-67
    v2.37           v64-66
    v2.35           v62-64
    v2.34           v61-63
    v2.33           v60-62
    v2.32           v59-61
    v2.31           v58-60

4) Download the Chrome driver and store it in the same directory as Chrome.exe

The default storage path of Chrome.exe is:
C:\Program Files (x86)\Google\Chrome\Application

chromedriver download link:
http://npm.taobao.org/mirrors/chromedriver

Download different versions of chrome:
https://www.slimjet.com/chrome/google-chrome-old-version.php

3. Verify the environment

# -*- coding:utf-8 -*-
# Author:chinablue

import time

from airtest_selenium.proxy import WebChrome

driver = WebChrome(executable_path=r"C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe")
driver.maximize_window()
driver.get("http://www.baidu.com/")
time.sleep(5)
driver.quit()

4. Installation problem record

If the following error occurs, try to restart the Pycharm software with administrator privileges

selenium.common.exceptions.WebDriverException: Message: unknown error: Failed to create Chrome process.

2. Description of common usage scenarios

2.1 Chrome browser common parameter configuration

# -*- coding: utf-8 -*-
# @Time    : 2020/11/18 20:52
# @Author  : chinablue

import time

from airtest_selenium.proxy import WebChrome
from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_argument('--disable-gpu')

# 指定浏览器的分辨率
options.add_argument('--window-size=1920,1080')

# 关闭提示: Chrome正受到自动测试软件的控制
# options.add_argument('--disable-infobars')  # 老版本Chrome浏览器的写法
options.add_experimental_option("excludeSwitches", ['enable-automation'])

# 无界面运行
# options.add_argument('--headless')

# 配置代理
# options.add_argument("--proxy-server=http://127.0.0.1:9631")

# 其他设置: 不加载图片, 设置语言, 设置User-Agent等

driver = WebChrome(executable_path=r"C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe",
                   chrome_options=options)

driver.get("http://www.baidu.com/")

time.sleep(5)
driver.quit()

Note:
If a MoveTargetOutOfBoundsException is thrown when there is no interface running, set the --window-size parameter to the browser and try again

2.2 Processing of Alert box

driver.switch_to.alert.accept()

Note:
If there is an alert box on the current page, an exception will be thrown when the browser is closed

2.3 Simple slider operation in login operation

from selenium.webdriver import ActionChains

action_chains = ActionChains(driver)
d1 = driver.find_element_by_class_name("el-icon-d-arrow-right")  # 定位滑块
action_chains.click_and_hold(d1).perform() # 鼠标左键按住滑块不动
action_chains.reset_actions()  # 清楚之前的action
action_chains.move_by_offset(300, 0).perform() # 平行移动滑块, 其中300表示x轴, 0表示y轴

2.4 Automatically handle the notification box of Chrome browser

# 创建一个名字为run.reg的文件, 内容如下. 双击执行此文件来修改注册表信息
Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Google]

[HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Google\Chrome]
"DefaultPluginsSetting"=dword:00000001

[HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Google\Chrome\PluginsAllowedForUrls]
"1"="http://ztc.njtopsun.com/topsun/#/"

Note:
  1) The blue part of the file needs to be replaced with the website you want to test
  2) Different versions of Chrome have different ways to automatically process notification popups, and the version of Chrome used in this example is Chrome 86
  3) For the description of DefaultPluginsSetting parameters in Chrome, please click here


Finally, I would like to thank everyone who has read my article carefully. Seeing the fans’ growth and attention all the way, there is always a need for reciprocity. Although it is not a very valuable thing, if you can use it, you can take it directly.

​These materials should be the most comprehensive and complete preparation warehouse for friends who are engaged in [software testing]. This warehouse has also accompanied me through the most difficult journey, and I hope it can help you too!

Finally: You can get a 216-page software test engineer interview collection document for free on my VX public account: [Automated Test Veteran Driver] . And the corresponding video learning tutorials are free to share! , which includes basic knowledge, Linux essentials, Shell, Internet program principles, Mysql database, packet capture tool topics, interface testing tools, advanced testing-Python programming, Web automated testing, APP automated testing, interface automated testing, testing Advanced continuous integration, test framework development test framework, performance testing, security testing, etc. You can also interact with me

Guess you like

Origin blog.csdn.net/myh919/article/details/131424850