ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

pytest添加运行失败截图和使用定制的css

2020-04-18 21:04:42  阅读:253  来源: 互联网

标签:截图 driver html pytest report css


pytest添加运行失败截图

在conftest.py中,定义截图的方法,失败时自动截图,并将截图保存到html页面中

from common.base_driver import BaseDriver
import pytest

driver = None

#失败自动截图,展示到html报告中
@pytest.mark.hookwrapper
def pytest_runtest_makereport(item):
    pytest_html = item.config.pluginmanager.getplugin("html")
    outcome = yield
    report = outcome.get_result()
    extra = getattr(report, 'extra', [])

    if report.when == "call" or report.when == "setup":
        xfail = hasattr(report, 'wasxfail')
        if (report.skipped and xfail) or (report.failed and not xfail):
            file_name = report.nodeid.replace("::", "_") + ".png"
            screen_img = _capture_screenshot()
            if file_name:
                html = '<div><img src="data:image/png;base64,%s" alt="screenshot" style="width:600px;height:300px;" ' \
                       'onclick="window.open(this.src)" align="right"/></div>' % screen_img
                extra.append(pytest_html.extras.html(html))
        report.extra = extra


#截图保存为base_64,展示在html中
def _capture_screenshot():
    return driver.get_screenshot_as_base64()



#定义公共的fixture
@pytest.fixture
def common_driver():
    global driver
    driver = BaseDriver().base_driver()
    yield driver
    driver.close_app()
    driver.quit()


#定义含有toast弹框的fixture
@pytest.fixture
def common_toast_driver():
    global driver
    driver = BaseDriver().base_driver(automationName="UIAutomator2")
    yield driver
    driver.close_app()
    driver.quit()

注意的是fixture修饰的方法中,不要忘了global driver

运行之后显示测试报告的图片是宽度和高度过大,导致图片比较难看,因此我们需要优化下

自定义css

在html_reports目录下定义一个assets目录,在目录下将pytest-html插件源码中的style.css拷贝到assets下

使用F12查看报告的图片的位置,是在class="extra"下的img目录中

修改style.css,为了让这个样式优先级最高,加入!import

.extra div div img {
	width: 300px!important;
	height: 500px!important;
}

修改main.py,使用--css, style.css路径--self-contained-html(这个表示将我们定义的css样式合并到html页面中)

import pytest
import time
from common.conf_dir import html_reports_dir

cur_time = time.strftime("%Y-%m-%d_%H-%M-%S")
pytest.main([
        #"--reruns=1",
        #"--reruns-delay=10",
        "-m", "fail",
        "--junitxml", f"{html_reports_dir}/autotest_report_{cur_time}.xml",
        "--html", f"{html_reports_dir}/autotest_report_{cur_time}.html",
        "--css", f"{html_reports_dir}/assets/style.css",
        "--self-contained-html"]
)

最终效果

这次报告看起来美观多了~

参考文章

《pytest文档8-html报告报错截图+失败重跑》
《pytest 之 pytest-html生成html报告》

标签:截图,driver,html,pytest,report,css
来源: https://www.cnblogs.com/my_captain/p/12728012.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有