首页 > 其他分享 >基于airtest-selenium的UI自动化测试

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

时间:2022-10-17 22:35:28浏览次数:54  
标签:浏览器 Chrome selenium driver UI airtest options

一. 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参数说明, 请点击这里

原文地址:https://www.cnblogs.com/reconova-56/p/13967170.html

标签:浏览器,Chrome,selenium,driver,UI,airtest,options
From: https://www.cnblogs.com/songzhenhua/p/16800981.html

相关文章

  • Selenium4Web自动化4-鼠标键盘模拟操作
    一、Web元素交互参考官方文档:https://www.selenium.dev/zh-cn/documentation/webdriver/elements/interactions/用于操纵表单的高级指令集.仅有五种基本命令可用于元......
  • 一起学kubernetes系列(3)‌K8S的WebUI:Dashboard&Kuboard
    ​本篇介绍2套kubernetes的WEBUI管理控制台Kubernetes官方Dashboard部署KubernetesDashboard是Kubernetes的官方WebUI。使用KubernetesDashboard,您可以:向Kubern......
  • Airtest自动化测试实操案例 | Windows应用篇
    转自公众号:AirtestProject前言之前有同学留言说想看Windows应用的自动化,那么今天我们就用1个简单的例子,带大家一起来看一下Windows应用的自动化究竟有哪些坑。不过在此之......
  • Selenium4Web自动化3-等待机制详解
    一、sleepsleep(timeout)是设定一个固定的等待时长,代码运行到此处,会强行进行等待指定的时间,使用方便的同时,效率最低,不建议使用。缺点:不能准确把握需要等待的时间(有时操......
  • arduino操作PN532
    介绍参考​​http://www.arduino.cn/thread-5960-1-1.html​​​......
  • Selenium4Web自动化2-页面元素定位
    一前端页面的组成分析详解1常见标签标签语言,常见的标签有:a:超链接img:图片input:输入框、文件上传button:按钮select:下拉框iframe:窗体p:文字。。。。。2标签语......
  • JavaGUI编程个人笔记
    GUI编程(了解)组件窗口弹窗面板文本框列表框按钮图片监听事件鼠标事件键盘事件破解工具简介Gui开发核心技术:SwingAWT界面不美观需要jre环境但还要学习......
  • Python爬虫之基于selenium实现12306模拟登录
    参考:https://blog.csdn.net/m0_54490473/article/details/122751814解决了个主要问题,滑块验证错误。文件由:vscode编写,浏览器驱动为EDGE.驱动下载地址:https://develop......
  • selenium验证码处理-打码平台操作
    1.进入打码平台(超人,斐斐)斐斐-------官网:   超人-------官网:   2.选择对应的开发文档,下载对应的demo示例,并把demo的python脚本放到项目包管理地址去参考:......
  • vue+elementui 的表格单元格内修改数据
    cellDbClick(row,column,cell,event){   varthat=this     event.target.innerHTML=''     varcellInput=document.createElem......