当前位置:网站首页>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方法,完成交互
边栏推荐
猜你喜欢

Combination series - there are combinations when there are arrangements

The Selenium IDE of the Selenium test automation

Getting started with UnityShader (3) - Unity's Shader

Resnet&API

使用NVM进行node版本切换管理

An article makes it clear!What is the difference and connection between database and data warehouse?

Spark学习(2)-Spark环境搭建-Local

使用 Chainlink Keepers 实现智能合约函数的自动化执行

UnityShader入门学习(一)——GPU与Shader

Unity Shader入门精要学习——透明效果
随机推荐
Comparison of Optical Motion Capture and UWB Positioning Technology in Multi-agent Cooperative Control Research
常用工具命令速查表
什么是消息队列呢?
【Pytorch】F.softmax()方法说明
OAuth2:四种授权方式
Nuget打包并上传教程
In the future, the interviewer asks you why it is not recommended to use Select *, please answer him out loud!
我把问烂了的MySQL面试题总结了一下
Redis与分布式:集群搭建
IDEA连接MySQL数据库并使用数据
OAuth2:资源服务器
ERROR: Failed building wheel for osgeo
Sentinel热点参数限流
R语言的画图代码及差异性分析[通俗易懂]
公告
Nuget package and upload tutorial
Essential Learning for Getting Started with Unity Shader - Transparency Effect
UnityShader入门学习(二)——渲染流水线
OpenCV测量物体的尺寸技能 get~
leetcode:485.最大连续 1 的个数