For Node.js it's mentioned option 'retain-on-failure' (https://playwright.dev/docs/videos) to preserve only videos for failed tests. Nothing similar for the Python. How can it be solved (delete videos for passed tests)? Thanks
import pytest
from playwright.sync_api import sync_playwright
@pytest.fixture(scope="module")
def browser(headless):
with sync_playwright() as p:
browser = p.chromium.launch(headless=headless)
yield browser
browser.close()
@pytest.fixture
def context(browser):
context = browser.new_context(
record_video_dir="reports/",
record_video_size={"width": 1024, "height": 768},
)
yield context
context.close()
@pytest.fixture
def page(context):
page = context.new_page()
yield page
page.close()
Solved with a hook
# tests\conftest.py
@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()
setattr(item, "rep_call", rep)
# fixtures\fixture_base.py
@pytest.fixture
def page(context, request):
page = context.new_page()
yield page
video_path = page.video.path()
page.close()
if request.node.rep_call.outcome == "passed":
time.sleep(1) # to prevent failure on a locked state for the file
os.remove(video_path)