软件测试/测试开发丨UI自动化测试用例结构分析

点此获取更多相关资料

本文为霍格沃兹测试开发学社学员学习笔记分享

原文链接:https://ceshiren.com/t/topic/26899

一、用例结构

1.1、标准的用例结构

  • 用例标题 搜狗搜索功能
  • 前提条件 进入搜狗首页
  • 用例步骤 1、输入关键词;2、点击搜索
  • 预期结果 1、搜索成功;2、搜索结果中包含关键字
  • 实际结果

1.2、自动化测试用例结构

自动化测试用例 作用
用例标题 测试包、文件、类、方法名称 用例的唯一标识
前提条件 setup、setup_class(Pytest);BeforeEach、BeforeAll(JUnit) 测试用例前的准备动作,比如读取数据或者driver的初始化
用例步骤 测试方法内的代码逻辑 测试用例具体的步骤行为
预期结果 assert 实际结果 = 预期结果 断言,印证用例是否执行成功
实际结果 assert 实际结果 = 预期结果 断言,印证用例是否执行成功
后置动作 teardown、teardown_class(Pytest);@AfterEach、@AfterAll(JUnit) 脏数据清理、关闭进程

二、录制脚本分析

  • 脚本步骤:

    1. 访问搜狗网站
    2. 搜索框输入"霍格沃兹测试开发"
    3. 点击搜索按钮
python 复制代码
class Test():
  def setup_method(self, method):
    self.driver = webdriver.Chrome()
    self.vars = {}
  
  def teardown_method(self, method):
    self.driver.quit()
  
  def test_sougou(self):
    # 打开网页,设置窗口
    self.driver.get("https://www.sogou.com/")
    self.driver.set_window_size(1235, 693)
    # 输入搜索信息
    self.driver.find_element(By.ID, "query").click()
    self.driver.find_element(By.ID, "query").send_keys("霍格沃兹测试开发")
    # 点击搜索
    self.driver.find_element(By.ID, "stb").click()

三、录制脚本优化

  • 添加隐式等待
  • 添加断言
python 复制代码
class Test():
    def setup_method(self, method):
        self.driver = webdriver.Chrome()
        self.driver.maximize_window()
        self.driver.implicitly_wait(3)
        # self.vars = {}

    def teardown_method(self, method):
        self.driver.quit()

    def test_sougou(self):
        # 打开网页,设置窗口
        self.driver.get("https://www.sogou.com/")
        self.driver.set_window_size(1235, 693)
        # 输入搜索信息
        # self.driver.find_element(By.ID, "query").click()
        self.driver.find_element(By.ID, "query").send_keys("霍格沃兹测试开发")
        # 点击搜索
        self.driver.find_element(By.ID, "stb").click()
        # element = self.driver.find_element(By.ID, "stb")
        # actions = ActionChains(self.driver)
        # actions.move_to_element(element).perform()
        time.sleep(5)
        # 添加断言
        fond_element = self.driver.find_element(By.ID, "sogou_vr_30000000_0")
        assert "霍格沃兹测试开发" in fond_element.text
相关推荐
Mr_Zuo9 分钟前
Android调用python库和方法的实现
android·python
大模型真好玩11 分钟前
深入浅出LangChain AI Agent智能体开发教程(五)—LangChain接入工具基本流程
人工智能·python·mcp
该用户已不存在11 分钟前
2025 年,Python工具别只知道Pycharm了
前端·后端·python
LiuYiCheng1234561 小时前
Python爬虫实践:高效下载XKCD漫画全集
爬虫·python
程序媛一枚~1 小时前
使用Python,OpenCV计算跑图的图像彩色度
开发语言·python·opencv
DAWN_T171 小时前
关于网络模型的使用和修改/保存和读取
网络·人工智能·pytorch·python·深度学习·神经网络·机器学习
golitter.1 小时前
python中的 @dataclass
开发语言·python
LiuYiCheng1234561 小时前
WebCrawler库:从网页抓取到智能处理的Python利器
开发语言·python
演绎平生1 小时前
一个Pycharm窗口添加多个项目来满足运行多个项目的需求
python
小新学习屋1 小时前
《剑指offer》-算法篇-位运算
python·算法·leetcode·职场和发展·数据结构与算法