web自动化--自动化测试用例结构分析
标准的用例结构
- 用例标题
- 前提条件
- 用例步骤
- 预期结果
- 实际结果
用例标题 | 类型 | 前提条件 | 用例步骤 | 预期结果 | 实际结果 |
---|---|---|---|---|---|
搜狗搜索功能 | 正例 | 进入搜狗首页 | 1. 输入搜索关键词 2.按下回车键 |
1. 搜索成功 2. 搜索结果列表包含关键字 |
用例结构对比
自动化测试用例 | 作用 | |
---|---|---|
用例标题 | 测试包、文件、类、方法名称 | 用例的唯一标识 |
前提条件 | setup、setup_class(Pytest); BeforeEach、BeforeAll(JUnit) |
测试用例前的准备动作,比如读取数据或者driver的初始化 |
用例步骤 | 测试方法内的代码逻辑 | 测试用例具体的步骤行为 |
预期结果 | assert 实际结果 = 预期结果 | 断言,印证用例是否执行成功 |
实际结果 | assert 实际结果 = 预期结果 | 断言,印证用例是否执行成功 |
后置动作 | teardown、teardown_class(Pytest); @AfterEach、@AfterAll(JUnit) |
脏数据清理、关闭driver进程 |
IDE录制脚本
- 脚本步骤:
- 访问搜狗网站
- 搜索框输入“霍格沃兹测试开发”
- 点击搜索按钮
# Generated by Selenium IDE
import pytest
import time
import json
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
class Test():
def setup_method(self, method): #前提条件
self.driver = webdriver.Chrome()
self.vars = {}
def teardown_method(self, method): #后置动作,driver退出
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()
#无法判断用例是否执行成功或者失败
#添加判断assert
result = self.driver.find_element(By.CSS_SELECTOR,"#sogou_vr_30000000_0 > em")
assert result.text =="搜索" #实际结果和预期结果判断