Tim*_*nov 1 pytest selenium-webdriver 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(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()
Run Code Online (Sandbox Code Playgroud)
但它不起作用 - 当某些测试失败时,我在报告中看不到任何屏幕截图。
我试过了:
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)
Run Code Online (Sandbox Code Playgroud)
但这些都不起作用......
我最终是如何做到的:
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()
Run Code Online (Sandbox Code Playgroud)
PyPi 库:
allure-pytest==2.3.2b1
allure-python-commons==2.3.2b1
pytest==3.4.2
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6185 次 |
| 最近记录: |