Selenium webdriver PyTest&诱惑:诱惑报告中失败的屏幕截图

Selenium webdriver PyTest&诱惑:诱惑报告中失败的屏幕截图,selenium-webdriver,pytest,allure,Selenium Webdriver,Pytest,Allure,我正在尝试将截图附加到诱惑报告,现在我使用以下代码: @pytest.hookimpl(hookwrapper=True, tryfirst=True) def pytest_runtest_makereport(item, call): outcome = yield rep = outcome.get_result() setattr(item, "rep_" + rep.when, rep) return rep @pytest.fixture(scop

我正在尝试将截图附加到诱惑报告,现在我使用以下代码:

@pytest.hookimpl(hookwrapper=True, tryfirst=True)
def pytest_runtest_makereport(item, call):
    outcome = yield
    rep = outcome.get_result()
    setattr(item, "rep_" + rep.when, rep)
    return rep


@pytest.fixture(scope="function")
def web_browser(request):
    # Open browser:
    b = webdriver.PhantomJS()

    # Return browser instance to test case:
    yield b

    # Do teardown (this code will be executed after each test):

    if request.node.rep_call.failed:
        # Make the screen-shot if test failed:
        try:
            # Make screen-shot for Allure report:
            allure.attach(str(request.function.__name__),
                          b.get_screenshot_as_png(),
                          type=AttachmentType.PNG)
        except:
            pass # just ignore

    # Close browser window:
    b.quit()
但它不起作用——当一些测试失败时,我在报告中看不到任何截图

我试过:

allure.attach(request.function.__name__,
              b.get_screenshot_as_png(),
              type=AttachmentType.PNG)

allure.attach('screenshot' + str(uuid.uuid4()),
              b.get_screenshot_as_png(),
              type=allure.attachment_type.PNG)

allure.attach(b.get_screenshot_as_png(),
              name='screenshot',
              type=AttachmentType.PNG)

allure.attach(b.get_screenshot_as_png(),
              name='screenshot2',
              type=allure.attachment_type.PNG)

但这一切都不管用…

我最终是如何做到的:

import pytest
import allure
from selenium import webdriver


@pytest.hookimpl(hookwrapper=True, tryfirst=True)
def pytest_runtest_makereport(item, call):
    outcome = yield
    rep = outcome.get_result()
    setattr(item, "rep_" + rep.when, rep)
    return rep


@pytest.fixture(scope="function")
def web_browser(request):
    # Open browser:
    b = webdriver.PhantomJS(executable_path='/tests/phantomjs')
    b.set_window_size(1400, 1000)

    # Return browser instance to test case:
    yield b

    # Do teardown (this code will be executed after each test):

    if request.node.rep_call.failed:
        # Make the screen-shot if test failed:
        try:
            b.execute_script("document.body.bgColor = 'white';")

            allure.attach(b.get_screenshot_as_png(),
                          name=request.function.__name__,
                          attachment_type=allure.attachment_type.PNG)
        except:
            pass # just ignore

    # Close browser window:
    b.quit()
PyPi库:

allure-pytest==2.3.2b1
allure-python-commons==2.3.2b1
pytest==3.4.2

我最终是如何做到的:

import pytest
import allure
from selenium import webdriver


@pytest.hookimpl(hookwrapper=True, tryfirst=True)
def pytest_runtest_makereport(item, call):
    outcome = yield
    rep = outcome.get_result()
    setattr(item, "rep_" + rep.when, rep)
    return rep


@pytest.fixture(scope="function")
def web_browser(request):
    # Open browser:
    b = webdriver.PhantomJS(executable_path='/tests/phantomjs')
    b.set_window_size(1400, 1000)

    # Return browser instance to test case:
    yield b

    # Do teardown (this code will be executed after each test):

    if request.node.rep_call.failed:
        # Make the screen-shot if test failed:
        try:
            b.execute_script("document.body.bgColor = 'white';")

            allure.attach(b.get_screenshot_as_png(),
                          name=request.function.__name__,
                          attachment_type=allure.attachment_type.PNG)
        except:
            pass # just ignore

    # Close browser window:
    b.quit()
PyPi库:

allure-pytest==2.3.2b1
allure-python-commons==2.3.2b1
pytest==3.4.2