基于airtest —— selenium的UI自动化测试

一. airtest-selenium环境搭建

1.1 安装与介绍

airtest-selenium库是基于selenium库的进一步封装:
https://airtest.doc.io.netease.com/tutorial/13_Selenium/

pip install airtest-selenium
pip install pynput

airtest-selenium库的几个特点:
1) 对切换标签的界面进行了友好封装,
2)支持图像识别功能,
3)自动进行log记录(参考selenium-java的监听模式),
4)兼容selenium的原生api

1.2 下载浏览器和浏览器驱动

本文使用Chrome浏览器,将下载与之对应的浏览器驱动chromedirver.exe

1) 查看chrome版本

chrome://version/

2) 关闭Chrome浏览器的自动更新功能

如果浏览器自动更新后,浏览器的版本会发生变化,这样可能会导致已下载的chromedirver.exe失效

右击我的电脑——管理——服务和应用程序——服务——停止:google更新服务(gupdate)、google更新服务(gupdatem);启动类型:手动

3) chromedriver与chrome版本的映射关系

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) 下载Chrome驱动,并将其与Chrome.exe存放到同一目录下

Chrome.exe的默认存放路径为:
C:\Program Files (x86)\Google\Chrome\Application

chromedriver下载地址:
http://npm.taobao.org/mirrors/chromedriver

扫描二维码关注公众号,回复: 15582763 查看本文章

不同版本的chrome下载:
https://www.slimjet.com/chrome/google-chrome-old-version.php

3. 验证环境

# -*- 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. 安装问题记录

如果出现如下报错,尝试以管理员权限重新启动Pycharm软件

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

二. 常用使用场景说明

2.1 Chrome浏览器常用参数配置

# -*- 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()

注意事项:
若无界面运行时,抛出MoveTargetOutOfBoundsException异常, 则给浏览器设置--window-size参数后再试

2.2 Alert框的处理

driver.switch_to.alert.accept()

注意事项:
若当前页面存在alert框,此时关闭浏览器时会抛出异常

2.3 登录操作中简单滑动滑块操作

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 自动处理Chrome浏览器的通知弹框

# 创建一个名字为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/#/"

注意事项:
  1) 文件中蓝色部分内容需要替换成你自己要测试的网站
  2) Chrome在不同版本下设置自动处理通知弹框的方法不一样,本示例中使用的Chrome版本为Chrome 86
  3) Chrome中DefaultPluginsSetting参数说明, 请点击这里


最后感谢每一个认真阅读我文章的人,看着粉丝一路的上涨和关注,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走

​这些资料,对于在从事【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴我走过了最艰难的路程,希望也能帮助到你!

最后: 可以在我的VX公众号:【自动化测试老司机】 免费领取一份216页软件测试工程师面试宝典文档资料。以及相对应的视频学习教程免费分享!,其中包括了有基础知识、Linux必备、Shell、互联网程序原理、Mysql数据库、抓包工具专题、接口测试工具、测试进阶-Python编程、Web自动化测试、APP自动化测试、接口自动化测试、测试高级持续集成、测试架构开发测试框架、性能测试、安全测试等。也可以和我交流互动哈

猜你喜欢

转载自blog.csdn.net/myh919/article/details/131424850