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

1438 篇文章 112 订阅
1428 篇文章 66 订阅

一. 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

不同版本的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参数说明, 请点击这里


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值