当前位置:网站首页>Web自动化实战——Selenium4(自动化测试环境的搭建)
Web自动化实战——Selenium4(自动化测试环境的搭建)
2022-07-31 14:44:00 【T_JC】
1. WEB自动化实战—Selenium4
- web自动化测试需求和挑战
- 属于E2的测试,是软件质量保证的最后一道防线
- 点点点:低调,复杂的场合无法测试
- 好的测试,不仅仅模拟用户行为,还要记录、调试网页细节
- 优势:
- 提速增效
- 解放双手
- 技能提示
- 目前主流工具
- Cypress
- Playwright
- Selenium
2. Selenium的优势:
浏览器支持醉的,兼容最好
支持多种编程语言
生态成熟,文档丰富
进行APP自动化测试,事半功倍
pip豆瓣源安装 :pip install xxx包 -i https://pypi.douban.com/simple/
Selenium自动化环境搭建
一键搭建
pip install webdriver-helper
webdriver-helper:
- 自动获取浏览器的版本、操作系统类型
- 自动下载浏览器驱动
- 自动创建和返回WebDriver对象
控制浏览器
with get_webdriver() as driver:
driver.get(“https://baidu.com”)
自动关闭浏览器
3. Selenium自动化实战
web自动化测试三板斧:
- 定位元素
- 交互元素
- 进行断言
3.1 八大定位策略
ID、name、css、xpath、classname、tag_name、link_text
3.2 pytest夹具使用
# 创建一个夹具
@pytest.fixture()
def driver():
driver = get_webdriver() # 启动浏览器
driver.get("http://appfront.huice.com/")
# 前置部分,在测试用例之前执行
# 写登录逻辑
yield driver # yield 生成器的写法
# 后置部分,在测试用例之后执行
print('关闭浏览器')
driver.quit()
浏览器启动的次数 = 测试用例的个数
如何避免,重复启动浏览器(每一次启动大概耗时3秒)
pytest夹具范围:
五种范围:
function:默认,每个用例执行一次
class:每个类执行一次
module:每个文件执行一次
package:每个包执行一次
session:整个测试过程只执行一次
写法:@pytest.fixture(scope=“session”)
4. 解决登录的问题
- 自动登录
- 不必重复登录
5. 解决不登录的问题
- 不重启浏览器
- 保持不登录的状态,以便对登录进行测试
网站的登录状态,是如何保持的?
cookie
selenium可以清除cookies :driver.delete_all_cookies() 清除cookie
6. POM设计模式
- pytest解决了用例层面的问题
- pom解决了页面层面的问题
Page Object Model:页面对象 设计模式
- 使用 对象 代表 页面
- 使用 对象 的 属性 代表 页面的元素
- 使用 对象 的 方法 代表 页面的操作
定义过程:
- 定义 BasePage (抽象类)
- 定义 Page (页面类)
- 定义元素的交互 (对象方法)
使用过程:
- 打开页面,实例化Page
- 调用Page方法,完成交互
边栏推荐
- Linux bash: redis-server: command not found
- I summed up the bad MySQL interview questions
- Small test knife: Go reflection helped me convert Excel to Struct
- element-plus虚拟表格virtual-list组件中是怎么实现清理lodash.memoize缓存的?
- Use of el-tooltip
- 深入浅出边缘云 | 4. 生命周期管理
- Introduction to BigDecimal, common methods
- Description of Hikvision camera streaming RTSP address rules
- How to grab configuration information for DELL SC compellent storage system
- Five dimensions to start MySQL optimization
猜你喜欢
随机推荐
Network cable RJ45 interface pins [easy to understand]
[Blue Bridge Cup Trial Question 46] Scratch Magnet Game Children's Programming Scratch Blue Bridge Cup Trial Question Explanation
以后面试官问你 为啥不建议使用Select *,请你大声回答他!
OAuth2:微服务权限校验Session共享
UnityShader入门学习(三)——Unity的Shader
Introductory UnityShader learning (2) - the rendering pipeline
最近很火的国产接口神器Apipost体验
[Pytorch] torch.argmax() usage
[QNX Hypervisor 2.2 User Manual]9.14 safety
MANIFEST.MF文件(PDB文件)
I summed up the bad MySQL interview questions
Detailed guide to compare two tables using natural full join in SQL
高等数学——常用不定积分公式
svn安装及使用(身体功能手册)
Shang Silicon Valley-JVM-Memory and Garbage Collection (P1~P203)
梅克尔工作室-第一次
LeetCode二叉树系列——226.翻转二叉树
The use of thread pool two
OpenCV测量物体的尺寸技能 get~
海康摄像机取流RTSP地址规则说明