还在为如何编写Web自动化测试用例而烦恼嘛?资深测试工程师手把手教你Selenium 测试用例编写

编写Selenium测试用例就是模拟用户在浏览器上的一系列操作,通过脚本来完成自动化测试。

编写测试用例的优势:

  • 开源,免费。

  • 支持多种浏览器 IE,Firefox,Chrome,Safari。

  • 支持多平台 Windows,Linux,Mac。

  • 支持多语言 Python,Java,C#。

  • 对 Web 支持良好。

  • 简单,灵活。

  • 支持分布式测试用例执行。

引入依赖

引入依赖是为了调用 webdriver 中的方法来与浏览器进行交互以实现操作步骤。

  • Python版本

<pre class="copy-codeblocks" style="font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace; font-size: 15.008px; display: block; position: relative; overflow: visible; color: rgb(34, 34, 34); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">from selenium import webdriver </pre>

测试用例的流程

测试用例是为了实施测试从而向被测试的系统提供的一组集合,这组集合包含:测试环境、操作步骤、测试数据、预期结果等。

注意:一条测试用例的最终结果只有一个:成功或者失败。

三大核心要素为 :标题 、步骤 、预期结果

  • 标题:是对测试用例的描述,标题应该清楚的表达测试用例的内容

  • 步骤:对测试执行过程进行描述

  • 预期结果:提供测试执行的预期结果,预期结果一般是根据需求得出,如果实际结果和预期结果一致则测试通过,反之失败。

实战演练

首先是在代码的最上面导入了 Selenium 的包,其次是测试方法命名、测试步骤、断言。测试用例内容是要断言打开百度页面所展现的内容与所期望的值是否相等。

  • Python版本

<pre class="copy-codeblocks" style="font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace; font-size: 15.008px; display: block; position: relative; overflow: visible; color: rgb(34, 34, 34); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">`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 == "百度"` </pre>

这里要断言打开百度页面所展现的内容与所期望的值是否相等。因为实际获取到的内容应该是百度一下而不是百度,所以断言错误。

<pre class="copy-codeblocks" style="font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace; font-size: 15.008px; display: block; position: relative; overflow: visible; color: rgb(34, 34, 34); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">`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` </pre>

获取更多相关资料戳:https://qrcode.ceba.ceshiren.com/link?name=article&project_id=qrcode&from=jianshu&timestamp=1656554914&author=wuyue

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容