Web 自动化测试实战与 Allure 报告
Web 自动化测试实战与 Allure 报告
学习价值
学习体系
- 掌握 Allure 安装与运行方式。
- 掌握 Allure 报告两种生成方式。
- 掌握 Allure 报告添加用例信息和附件的写法。
- 能够将 Allure 结合到自动化用例脚本中,根据自动化执行情况生成相应的报告内容。
知识模块
- L1. 安装与运行
- L2. 用例描述
- L3. 报告添加附件
- L4. 报告定制
实战需求
以下实战要求加上 Allure 报告的如下配置:
- 用例模块名称
- 用例标题
- 用例描述
- 用例步骤
- 附件信息(日志、图片)
实战 1:职位搜索实战
要求包含标题和详情的两种验证,写为两条用例,分别为:
验证标题
- 打开招聘网页。
- 输入职位关键词:测试工程师。
- 点击搜索按钮。
- 断言出现的第一个标题内容包含搜索的关键词:测试工程师。
验证详情
- 打开招聘网页。
- 输入职位关键词:"测试工程师"。
- 点击搜索按钮。
- 点击一个职位进入职位详情页面
- 断言职位详情包含搜索的关键词信息:"测试"。
实战 2:测试人社区搜索实战
- 打开测试人论坛。
- 点击搜索按钮。
- 输入关键字:ChromeDriver。
- 点击高级搜索按钮。
- 点击高级筛选器。
- 选择分类为"提问区"、话题为"开放"、排序为"赞最多"。
- 点击搜索按钮。
- 断言出现的第一个标题或简介文本内容包含 chromedriver。
实战 3:测试人社区登录实战
要求包含登陆成功和登录失败两种场景
- 打开测试人论坛
- 点击登录按钮
- 输入账号和密码
- 验证登录结果符合要求
相关知识点
实战内容
实战 1:职位搜索实战
@allure.feature("招聘网站")
@allure.story("招聘网站_职位搜索模块")
class TestBytedance:
def setup_method(self):
# 初始化浏览器
self.driver = webdriver.Chrome()
# 最大化窗口
self.driver.maximize_window()
# 设置隐式等待
self.driver.implicitly_wait(15)
with allure.step("打开招聘网站"):
# 进入字节招聘网站
self.driver.get("https://jobs.bytedance.com/")
def teardown_method(self):
# 退出浏览器
self.driver.quit()
@allure.title("成功搜索职位并验证标题包含关键词")
def test_job_title(self):
'''
验证搜索功能,输入关键词,判断第一个搜索结果包含搜索的关键词信息
:return:
'''
with allure.step("输入搜索的关键词"):
# 点击搜索框,输入搜索的关键词
self.driver.find_element(By.CLASS_NAME, 'atsx-input.atsx-input-lg.search-input').send_keys("测试工程师")
with allure.step("点击搜索按钮"):
# 点击搜索按钮
self.driver.find_element(By.CLASS_NAME, 'atsx-btn.sofiaBold.atsx-btn-primary.atsx-btn-circle').click()
# 等待搜索结果出现
sleep(5)
with allure.step("判断搜索第一个结果包含搜索的关键词"):
# 获取职位名称
job_ele = self.driver.find_element(By.CLASS_NAME, 'positionItem-title-text')
# 断言搜索的关键词在标题当中
self.driver.save_screenshot('./img/职位列表.png')
allure.attach.file('./img/职位列表.png',
name='职位列表',
attachment_type=allure.attachment_type.PNG,
extension='.png')
logger.info(f'职位详情搜索成功,内容为:{job_ele.text}')
assert "测试工程师" in job_ele.text
@allure.title("成功搜索职位并验证职位详情中包含关键词相关内容")
def test_job_desc(self):
'''
验证搜索功能,输入关键词,点击进入职位详情,职位详情需要包含搜索的关键词相关内容
:return:
'''
with allure.step("输入搜索的关键词"):
# 点击搜索框,输入搜索的关键词
self.driver.find_element(By.CLASS_NAME, 'atsx-input.atsx-input-lg.search-input').send_keys("测试工程师")
with allure.step("点击搜索按钮"):
# 点击搜索按钮
self.driver.find_element(By.CLASS_NAME, 'atsx-btn.sofiaBold.atsx-btn-primary.atsx-btn-circle').click()
# 等待搜索结果出现
sleep(5)
with allure.step("点击进入结果详情页面"):
# 获取职位名称
# 点击 进入职位详情
self.driver.find_element(By.CLASS_NAME, 'positionItem-title-text').click()
sleep(2)
windows = self.driver.window_handles
self.driver.switch_to.window(windows[-1])
with allure.step("判断搜索结果详情包含搜索的关键词"):
# 进入 职位详情
job_des_eles = self.driver.find_elements(By.CLASS_NAME, 'block-content')
# 将职位描述信息存进列表
job_desc = []
for i in job_des_eles:
job_desc.append(i.text)
# 断言 测试字样 在职位描述当中
self.driver.save_screenshot('./img/职位详情.png')
allure.attach.file('./img/职位详情.png',
name='职位详情',
attachment_type=allure.attachment_type.PNG,
extension='.png')
logger.info(f'职位详情搜索成功,内容为:{job_desc}')
assert any("测试" in item for item in job_desc), job_desc
实战 2:测试人社区搜索实战
@allure.feature("测试人网站")
@allure.story("测试人网站-高级搜索模块")
class TestCeshiren:
def setup_method(self):
# 初始化driver
self.driver = webdriver.Chrome()
# 添加隐式等待
self.driver.implicitly_wait(15)
# 最大化窗口
self.driver.maximize_window()
def teardown_method(self):
# 退出浏览器
self.driver.quit()
@allure.title("搜索关键词且包含筛选项的结果成功,搜索结果标题中包含关键词")
def test_ceshiren_search(self):
'''
验证搜索功能,输入关键词,选择筛选项为:分类为"提问区"、话题为"开放"、排序为"赞最多"
:return:
'''
with allure.step('进入测试人网站'):
# 打开测试人网页
self.driver.get('https://ceshiren.com/')
with allure.step("点击搜索按钮,并输入关键词"):
# 点击 搜索 放大镜图标
self.driver.find_element(By.ID, 'search-button').click()
# 输入关键字
self.driver.find_element(By.ID, 'search-term').send_keys('ChromeDriver')
logger.info('输入的关键词为:ChromeDriver')
with allure.step("进入高级搜索页面,筛选搜索条件"):
# 点击高级搜索按钮
self.driver.find_element(By.CLASS_NAME, 'show-advanced-search').click()
# 等待切换到 高级搜索页面
sleep(3)
self.driver.find_element(By.ID, 'search-in-category-header').click()
self.driver.find_element(By.CSS_SELECTOR, '.select-kit-collection [title="提问区"]').click()
# 选择话题-开放
self.driver.find_element(By.ID, 'search-status-options-header').click()
self.driver.find_element(By.CSS_SELECTOR, '.select-kit-collection [title="开放"]').click()
# 选择排序-赞最多
self.driver.find_element(By.ID, 'search-sort-by-header').click()
self.driver.find_element(By.CSS_SELECTOR, '.select-kit-collection [title="赞最多"]').click()
# 保存筛选项的图片
self.driver.save_screenshot('./img/筛选项图.png')
allure.attach.file('./img/筛选项图.png',
name='筛选项图',
attachment_type=allure.attachment_type.PNG,
extension='.png')
with allure.step("点击搜索按钮"):
# 点击 搜索 按钮
self.driver.find_element(By.CSS_SELECTOR, '.search-bar [aria-label="搜索"]').click()
with allure.step("判断搜索结果的第一个内容符合搜索的关键词"):
# 断言第一个搜索内容
sleep(3)
search_rs = self.driver.find_element(By.CSS_SELECTOR, '.fps-result-entries .blurb.container')
# 保存搜索结果图片
self.driver.save_screenshot('./img/搜索结果图.png')
allure.attach.file('./img/搜索结果图.png',
name='搜索结果图',
attachment_type=allure.attachment_type.PNG,
extension='.png')
res = search_rs.text.lower()
logger.info(f'第一个搜索结果的标题文本为:{res}')
# 断言 chromedriver 在第一个搜索结果的文本中
assert "chromedriver" in res
实战 3:测试人社区登录实战
@allure.feature("测试人网站")
@allure.story("测试人网站-登录模块")
class TestCeshiren:
...省略 setup teardown 操作
@allure.title("登录成功")
def test_ceshiren_login_success(self):
'''
输入正确的账号和密码,登录成功
:return:
'''
with allure.step("点击登录按钮"):
# 点击登录
self.driver.find_element(By.CSS_SELECTOR, '.login-button').click()
with allure.step("输入账号和密码,并且点击登录按钮"):
# 输入账号密码
correct_name = "ceshiren_sc0513"
correct_pwd = "hogwarts20240513"
self.driver.find_element(By.CSS_SELECTOR, '[autocomplete="username"]').send_keys(correct_name)
self.driver.find_element(By.CSS_SELECTOR, '[autocomplete="current-password"]').send_keys(correct_pwd)
# 点击登录按钮
self.driver.find_element(By.ID, 'login-button').click()
with allure.step("判断结果登录成功"):
sleep(5)
# 断言登录成功
logger.info(f"用户信息为,登录名为{correct_name},密码为{correct_pwd}")
self.driver.save_screenshot("./img/登录成功.png")
allure.attach.file('./img/登录成功.png',
name='登录成功',
attachment_type=allure.attachment_type.PNG,
extension='.png')
ele = self.driver.find_element(By.CSS_SELECTOR, '[title="QYE"]')
assert ele
@allure.title("密码输入错误,登录失败")
def test_ceshiren_login_fail(self):
'''
输入正确的账号、错误的密码,登录失败,提示词为:用户名、电子邮件或密码不正确
:return:
'''
with allure.step("点击登录按钮"):
# 点击登录成功
self.driver.find_element(By.CSS_SELECTOR, '.login-button').click()
with allure.step("输入账号和密码,并且点击登录按钮"):
# 输入账号密码
error_name = "ceshiren_sc0513"
error_pwd = "hogwarts20240513333"
self.driver.find_element(By.CSS_SELECTOR, '[autocomplete="username"]').send_keys("ceshiren_sc0513")
self.driver.find_element(By.CSS_SELECTOR, '[autocomplete="current-password"]').send_keys(
"hogwarts20240513333")
# 点击登录按钮
self.driver.find_element(By.ID, 'login-button').click()
sleep(5)
with allure.step("判断结果登录失败"):
# 断言登录失败
ele = self.driver.find_element(By.ID, 'modal-alert')
logger.info(f'登录失败,登录名为{error_name},密码为{error_pwd}')
self.driver.save_screenshot("./img/登录失败.png")
allure.attach.file('./img/登录失败.png',
name='登录失败',
attachment_type=allure.attachment_type.PNG,
extension='.png')
assert ele.text == '用户名、电子邮件或密码不正确'
总结
- 掌握 Allure2 安装和运行方式。
- 掌握 Allure2 添加用例相关信息的方式。
- 掌握 Allure2 用例添加附件的方法。
- 了解 Allure2 报告定制的方式。