当前位置:网站首页>还在为如何编写Web自动化测试用例而烦恼嘛?资深测试工程师手把手教你Selenium 测试用例编写
还在为如何编写Web自动化测试用例而烦恼嘛?资深测试工程师手把手教你Selenium 测试用例编写
2022-07-06 06:07:00 【测试界的飘柔】
编写Selenium测试用例就是模拟用户在浏览器上的一系列操作,通过脚本来完成自动化测试。
编写测试用例的优势:
开源,免费。
支持多种浏览器 IE,Firefox,Chrome,Safari。
支持多平台 Windows,Linux,Mac。
支持多语言 Python,Java,C#。
对 Web 支持良好。
简单,灵活。
支持分布式测试用例执行。
引入依赖
引入依赖是为了调用 webdriver 中的方法来与浏览器进行交互以实现操作步骤。
- Python版本
from selenium import webdriver
测试用例的流程
测试用例是为了实施测试从而向被测试的系统提供的一组集合,这组集合包含:测试环境、操作步骤、测试数据、预期结果等。
注意:一条测试用例的最终结果只有一个:成功或者失败。
三大核心要素为 :标题 、步骤 、预期结果
- 标题:是对测试用例的描述,标题应该清楚的表达测试用例的内容
- 步骤:对测试执行过程进行描述
- 预期结果:提供测试执行的预期结果,预期结果一般是根据需求得出,如果实际结果和预期结果一致则测试通过,反之失败。
实战演练
首先是在代码的最上面导入了 Selenium 的包,其次是测试方法命名、测试步骤、断言。测试用例内容是要断言打开百度页面所展现的内容与所期望的值是否相等。
Python版本
from selenium import webdriver
# 测试的标题即 test_search
def test_search():
driver = webdriver.Chrome()
# 测试的步骤
driver.get('https://www.baidu.com')
search = driver.find_element_by_id('su').get_attribute('value')
# 断言预期结果
assert search == "百度"
这里要断言打开百度页面所展现的内容与所期望的值是否相等。因为实际获取到的内容应该是百度一下而不是百度,所以断言错误。
FAILED [100%]
test_demo.py:3 (test_search)
百度一下 != 百度
Expected :百度
Actual :百度一下
<Click to see difference>
def test_search():
driver = webdriver.Chrome()
driver.get('https://www.baidu.com')
search = driver.find_element_by_id('su').get_attribute('value')
> assert search == "百度"
E AssertionError: assert '百度一下' == '百度'
test_demo.py:9: AssertionError
最后: 可以在公众号:伤心的辣条 ! 自行领取一份216页软件测试工程师面试宝典文档资料【免费的】。以及相对应的视频学习教程免费分享!,其中包括了有基础知识、Linux必备、Shell、互联网程序原理、Mysql数据库、抓包工具专题、接口测试工具、测试进阶-Python编程、Web自动化测试、APP自动化测试、接口自动化测试、测试高级持续集成、测试架构开发测试框架、性能测试、安全测试等。
我推荐一个【Python自动化测试交流群:746506216】,大家可以一起探讨交流软件测试,共同学习软件测试技术、面试等软件测试方方面面,助你快速进阶Python自动化测试/测试开发,走向高薪之路。
喜欢软件测试的小伙伴们,如果我的博客对你有帮助、如果你喜欢我的博客内容,请 “点赞” “评论” “收藏” 一 键三连哦!
边栏推荐
- Novice entry SCM must understand those things
- Introduction to promql of # yyds dry goods inventory # Prometheus
- Seven imperceptible truths in software testing
- [API interface tool] Introduction to postman interface
- MPLS test report
- Configuring OSPF GR features for Huawei devices
- How Huawei routers configure static routes
- properties文件
- Application of Lie group in gtsam
- Reading notes of effective managers
猜你喜欢

IDEA 新UI使用

请求转发与重定向

Application of Lie group in gtsam

養了只小猫咪

nodejs实现微博第三方登录
![[eolink] PC client installation](/img/91/8b3c4264e544b14f926e91edddf18d.png)
[eolink] PC client installation

Gtest之TEST宏的用法

The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower

SQLMAP使用教程(三)实战技巧二

【Postman】Collections-运行配置之导入数据文件
随机推荐
JDBC Requset 对应内容及功能介绍
Gtest之TEST宏的用法
Detailed explanation of BF and KMP
Investment strategy discussion and market scale prediction report of China's solid state high power amplifier industry from 2022 to 2028
【Tera Term】黑猫带你学TTL脚本——嵌入式开发中串口自动化神技能
Usage of test macro of GTEST
【Postman】动态变量(也称Mock函数)
全程实现单点登录功能和请求被取消报错“cancelToken“ of undefined的解决方法
Idea new UI usage
Hongliao Technology: how to quickly improve Tiktok store
H3C firewall rbm+vrrp networking configuration
Réflexions sur la sécurité des données (réimpression)
nodejs实现微博第三方登录
How to recover Huawei router's forgotten password
Introduction to promql of # yyds dry goods inventory # Prometheus
【课程笔记】编译原理
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
Cannot create PoolableConnectionFactory (Could not create connection to database server. 错误
Thoughts on data security (Reprint)
Request forwarding and redirection