我试图将屏幕截图附加到诱惑报告中,现在我正在使用此代码:
@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)
但这没有任何作用......
答案 0 :(得分:1)
我最终如何做到这一点:
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