pytest输出测试报告时,有时候需要把截图插入报告中,操作可以如下
用例如下:
from pytest_html import extras 导入包
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2023/5/12 17:44 # @Author : gezirui # @File : test_dsw_app_ss_011_截图实现.py # @Software: PyCharm # from Package.start_session import start_session # from Package.import_all import * from pytest_html import extras # 是一个列表对象 def test_dsw_app_ss_011(driver, by, ec, extra): # 这里的driver是调用conftest中的函数driver(), # 即diver= driver(),这里的extra也是一个列表,被导包的extras嵌套 # driver, wait = start_session() driver, wait = driver # 这里把start_session换成driver # driver.get_screenshot_as_base64() # 获取的是base64的数据 # driver.save_screenshot("会话之前.png") extra.append(extras.image(driver.get_screenshot_as_base64())) # 这里用extra列表添加一个二进制图片 print("会话开始了") db_data = ["九重华锦"] locator = (by.ID, "com.zhao.myreader:id/iv_search") ele = wait.until(ec.visibility_of_element_located(locator)) driver.save_screenshot("搜索之前.png") ele.click() # 点击搜索按钮 locator = (by.ID, "com.zhao.myreader:id/et_search_key") ele = wait.until(ec.visibility_of_element_located(locator)) # driver.save_screenshot("输入之前.png") extra.append(extras.image(driver.get_screenshot_as_base64())) ele.send_keys("莫西凡") # 输入框输入”莫“ locator = (by.ID, "com.zhao.myreader:id/tv_search_conform") ele = wait.until(ec.visibility_of_element_located(locator)) # driver.save_screenshot("查询之前.png") extra.append(extras.image(driver.get_screenshot_as_base64())) ele.click() # 点击查询按钮 locator = (by.ID, "com.zhao.myreader:id/tv_book_name") all_book = wait.until(ec.visibility_of_all_elements_located(locator)) # 找到书名元素块 all_book_name = [i.text for i in all_book] # 书名添加进列表 # driver.save_screenshot("断言之前.png") extra.append(extras.image(driver.get_screenshot_as_base64())) # if sorted(db_data) == sorted(all_book_name):print("断言通过") # 显示工作中,不用IF判断用例结果 print("开始断言") assert sorted(db_data) == sorted(all_book_name), f"断言失败,‘all_book_name’的值为{all_book_name},'db_data'的值为{db_data}" print(driver.session_id)
然后在main文件中运行测试用例。
import time import pytest # from pytest_html.html_report import # 后面的命令如果找不到,就去这里找,把下划线改成横杠就可以了 # pytest.main(["-vs", "testcase/test_dsw_app_ss_011_2.py"]) """生成测试报告""" # "--html=report/report-html", "--self-contained-html" 后面的命令来声明报告是html # pytest.main(["--html=report/report.html1", "--self-contained-html", "testcase/test_dsw_app_ss_011_2.py"]) pytest.main([f"--html=report/{int(time.time())}report.html", "--self-contained-html", "testcase/test_dsw_app_ss_011_截图实现.py"])
标签:测试报告,screenshot,--,APP,driver,pytest,html,import From: https://www.cnblogs.com/gezirui/p/17417586.html