【问题标题】:How do I include screenshot in python pytest html report如何在 python pytest html 报告中包含屏幕截图
【发布时间】:2018-05-25 17:59:31
【问题描述】:

当我在浏览器“https://192.168.xx.xxx/Test/ScreenCapture”中点击以下网址时 我在浏览器中得到了被测设备屏幕的截图。

如何在我的 pytest html 测试报告中添加屏幕截图。 目前我正在使用以下代码捕获指定测试目录中的屏幕截图。

url = 'https://192.168.xx.xxx/Test/ScreenCapture'
driver.get(url)    driver.save_screenshot('/home/tests/screen.png')

我正在使用以下命令运行我的 pytest: py.test --html=report.html --self-contained-html screentest.py

【问题讨论】:

    标签: python-2.7 selenium-webdriver pytest


    【解决方案1】:

    我找到了一个自己找到解决方案的人(@Vic152),这是原帖:https://github.com/pytest-dev/pytest-html/issues/186 关键是调用item.funcargs['request']获取当前的测试请求上下文。

    注意:如果你像我一样使用 Pytest 3.0+,请将 getfuncargvalue() 替换为 getfixturevalue()

    我在这里复制代码:

    @pytest.mark.hookwrapper
    def pytest_runtest_makereport(item, call):
    
        timestamp = datetime.now().strftime('%H-%M-%S')
    
        pytest_html = item.config.pluginmanager.getplugin('html')
        outcome = yield
        report = outcome.get_result()
        extra = getattr(report, 'extra', [])
        if report.when == 'call':
    
            feature_request = item.funcargs['request']
    
            driver = feature_request.getfuncargvalue('browser')
            driver.save_screenshot('D:/report/scr'+timestamp+'.png')
    
            extra.append(pytest_html.extras.image('D:/report/scr'+timestamp+'.png'))
    
            # always add url to report
            extra.append(pytest_html.extras.url('http://www.example.com/'))
            xfail = hasattr(report, 'wasxfail')
            if (report.skipped and xfail) or (report.failed and not xfail):
                # only add additional html on failure
                extra.append(pytest_html.extras.image('D:/report/scr.png'))
                extra.append(pytest_html.extras.html('<div>Additional HTML</div>'))
            report.extra = extra
    

    【讨论】:

      【解决方案2】:

      来自文档https://pypi.org/project/pytest-html/:您可以通过创建“额外”来向 HTML 报告添加详细信息

      extra.image(image, mime_type='image/gif', extension='gif')
      

      你需要做一个钩子。再次来自文档:

      import pytest
      @pytest.mark.hookwrapper
      def pytest_runtest_makereport(item, call):
          pytest_html = item.config.pluginmanager.getplugin('html')
          outcome = yield
          report = outcome.get_result()
          extra = getattr(report, 'extra', [])
          if report.when == 'call':
              # always add url to report
              extra.append(pytest_html.extras.url('http://www.example.com/'))
              xfail = hasattr(report, 'wasxfail')
              if (report.skipped and xfail) or (report.failed and not xfail):
                  # only add additional html on failure
                  extra.append(pytest_html.extras.html('<div>Additional HTML</div>'))
              report.extra = extra
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-04-25
        • 2012-02-14
        • 1970-01-01
        • 2020-12-18
        • 2018-06-14
        • 1970-01-01
        • 2013-01-20
        • 2015-07-19
        相关资源
        最近更新 更多