如何在通过测试的pytest bdd中包含硒截图? [英] How to include selenium screenshot in pytest bdd for passed tests?

查看:100
本文介绍了如何在通过测试的pytest bdd中包含硒截图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用pytest bdd编写硒测试.我正在使用pytest-html生成报告.为了进行调试或只是为了进行适当的日志记录,我需要selenium屏幕截图和html报告中的其余日志.但是我无法在通过的报告中使用硒截图.

I am writing tests in pytest bdd with selenium. I am using pytest-html to generate report. For debug purpose or just to have a proper logging, I want selenium screenshots and rest of the logs in html report. But I am unable to have selenium screenshot in passed report.

这是我正在尝试的事情.conftest.py中有一个pytest-html挂钩包装器

Here are the things I am trying. There is a pytest-html hook wrapper in conftest.py

conftest.py

conftest.py

@pytest.mark.hookwrapper
def pytest_runtest_makereport(item, call):
    pytest_html = item.config.pluginmanager.getplugin('html')
    outcome = yield
    report = outcome.get_result()
    print("printing report")
    extra = getattr(report, 'extra', [])
    if report.when == 'call':
        mylogs = ""
        with open('/tmp/test.log', 'r') as logfile:
            for line in logfile:
                mylogs = mylogs + line + "<br>"
        extra.append(pytest_html.extras.html('<html><body>{}</body></html>'.format(mylogs)))
        report.extra = extra

此代码在我的report.html中添加日志同样,我将在测试代码中添加一些硒屏幕截图.我想知道我们是否可以生成包含所有硒屏幕截图的报告.

This code is adding logs in my report.html Similarly, I will be adding few selenium screenshots in my test code. I want to know if we can generate a report containing all selenium screenshots.

以下是我的测试文件

test_file.py

test_file.py

def test_case():
    logger.info("I will now open browser")
    driver = webdriver.Chrome()
    driver.get('http://www.google.com')
    driver.save_screenshot('googlehome.png')
    time.sleep(3)
    driver.quit()

我希望googlehome.png和所有其他png文件成为html报告的一部分.如果我们能够生成一个像html report这样的机器人框架,我将很棒.

I want googlehome.png and all other png file to be part of html report. I will be great if the we can generate a robot framework like html report.

在pytest中有什么方法可以做到吗?

Is there any way in pytest we can do that?

以下是我用来生成报告的命令

Following is the command I use to generate report

py.test -s --html=report.html --self-contained-html  -v

推荐答案

您必须将webdriver从test传递到pytest报表系统中.就我而言,我将webdriver用作fixtuer.这样做还有很多其他优点-例如,您可以测试任何一组浏览器并从一个位置进行控制.

You have to pass webdriver from test into pytest reporting system. In my case I use webdriver as fixtuer. That have a lot of other advantages - for example you can test for any set of browsers and control that from one place.

@pytest.fixture(scope='session', params=['chrome'], ids=lambda x: 'Browser: {}'.format(x))
def web_driver(request):
    browsers = {'chrome': webdriver.Chrome}
    return browsers[]()


def test_case(web_driver):
    logger.info("I will now open browser")
    web_driver.get('http://www.google.com')


@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
    outcome = yield
    rep = outcome.get_result()
    if rep.when == 'call' and not rep.failed:
        try:
            if 'web_driver' in item.fixturenames:
                web_driver = item.funcargs['web_driver']
            else:
                return  # This test does not use web_driver and we do need screenshot for it
        # web_driver.save_screenshot and other magic to add screenshot to your report
    except Exception as e:
        print('Exception while screen-shot creation: {}'.format(e))

这篇关于如何在通过测试的pytest bdd中包含硒截图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆