2 回答
TA贡献1799条经验 获得超8个赞
conftest.py这是在 docker 容器中无头运行文件的完整解决方案:
import time
from datetime import datetime
import pytest
import os
from selenium import webdriver as selenium_webdriver
from selenium.webdriver.chrome.options import Options
# set up webdriver fixture
@pytest.fixture(scope='session')
def selenium_driver(request):
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
driver = selenium_webdriver.Chrome(options=chrome_options)
driver.set_window_size(1920, 1080)
driver.maximize_window()
driver.implicitly_wait(5)
yield driver
driver.quit()
# set up a hook to be able to check if a test has failed
@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
# execute all other hooks to obtain the report object
outcome = yield
rep = outcome.get_result()
# set a report attribute for each phase of a call, which can
# be "setup", "call", "teardown"
setattr(item, "rep_" + rep.when, rep)
# check if a test has failed
@pytest.fixture(scope="function", autouse=True)
def test_failed_check(request):
yield
# request.node is an "item" because we use the default
# "function" scope
if request.node.rep_setup.failed:
print("setting up a test failed!", request.node.nodeid)
elif request.node.rep_setup.passed:
if request.node.rep_call.failed:
driver = request.node.funcargs['selenium_driver']
take_screenshot(driver, request.node.nodeid)
print("executing test failed", request.node.nodeid)
# make a screenshot with a name of the test, date and time
def take_screenshot(driver, nodeid):
time.sleep(1)
file_name = f'{nodeid}_{datetime.today().strftime("%Y-%m-%d_%H:%M")}.png'.replace("/","_").replace("::","__")
driver.save_screenshot(file_name)
TA贡献1801条经验 获得超8个赞
还有另一种方法,类似于@Ostap 的方法:使用pytest_runtest_makereport(文档、API 参考)后处理功能。简单一点:
@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
outcome = yield
rep = outcome.get_result()
if rep.when == 'call' and rep.failed:
now = datetime.now().strftime('%Y-%m-%d_%H-%M-%S')
driver.save_screenshot(f".\\Screenshots\\fail_{now}.png")
添加回答
举报