-
Notifications
You must be signed in to change notification settings - Fork 53
/
conftest.py
76 lines (59 loc) · 2.27 KB
/
conftest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# Playwright video recording original implementation:
# https://github.com/alethcoe/playwright_recording_pytest
from typing import Any, Dict, Generator, List
import pytest
from playwright.sync_api import (
Browser,
BrowserContext,
Page,
)
import os
import shutil
@pytest.fixture(scope="session")
def browser_context_args(browser_context_args, video_path):
return {
**browser_context_args,
"record_video_dir": video_path,
"viewport": {"width": 1920, "height": 1080},
}
@pytest.fixture(scope="session")
def video_path():
return "./videos"
def pytest_sessionstart(session):
"""
Called before test run starts
"""
if os.path.exists("./videos"):
for filename in os.listdir("./videos"):
filepath = os.path.join("./videos", filename)
try:
shutil.rmtree(filepath)
except OSError:
os.remove(filepath)
@pytest.fixture
def page(context: BrowserContext, base_url: str) -> Generator[Page, None, None]:
page = context.new_page()
page._goto = page.goto # type: ignore
page.goto = lambda *args, **kwargs: _handle_page_goto(page, list(args), kwargs, base_url) # type: ignore
yield page
# save off the test unique id
current_path_name = context.pages[0].video.path()
BrowserContext.current_video_name = current_path_name
page.close()
@pytest.fixture
def context(
browser: Browser, browser_context_args: Dict, browser_name, video_path, request
) -> Generator[BrowserContext, None, None]:
context = browser.new_context(**browser_context_args)
current_failed_tests = request.session.testsfailed
yield context
current_video_name = context.current_video_name
current_video_path = current_video_name
updated_video_path = os.path.join(video_path, f"{request.node.originalname}_{browser_name}.webm")
context.close()
os.rename(current_video_path, updated_video_path)
def _handle_page_goto(page: Page, args: List[Any], kwargs: Dict[str, Any], base_url: str) -> None:
url = args.pop()
if not (url.startswith("http://") or url.startswith("https://")):
url = base_url + url
return page._goto(url, *args, **kwargs) # type: ignore