-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconftest.py
More file actions
90 lines (74 loc) · 3.21 KB
/
conftest.py
File metadata and controls
90 lines (74 loc) · 3.21 KB
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
77
78
79
80
81
82
83
84
85
86
87
import pytest
import os
import urllib.parse
import json
from dataclasses import dataclass
from playwright.sync_api import sync_playwright
from reporting import CustomReporterConfig
@dataclass
class TestContext:
title: str
success: bool = True
failure_reason: str = ""
@pytest.fixture
def rpage(request):
"""Playwright page connected to Perfecto cloud with automatic test reporting."""
capabilities = {
"browserName": os.environ.get("BROWSER_NAME", "Chrome"),
"browserVersion": os.environ.get("BROWSER_VERSION", "latest"),
"platformName": os.environ.get("PLATFORM_NAME", "Windows"),
"platformVersion": os.environ.get("PLATFORM_VERSION", "11"),
"securityToken": os.environ.get("sec_token", ""),
"report.jobName": os.environ.get("JOB_NAME", "job"),
"report.jobNumber": int(os.environ.get("JOB_NUMBER", "1")),
"report.jobBranch": os.environ.get("JOB_BRANCH", "1.1")
}
cloud = os.environ.get('cloud')
if not cloud or not capabilities["securityToken"]:
pytest.fail("Missing 'cloud' or 'sec_token' environment variables")
wss = f"wss://{cloud}.perfectomobile.com/websocket?{urllib.parse.quote(json.dumps(capabilities, separators=(',', ':')))}"
reporter = CustomReporterConfig()
test_context = TestContext(title=request.node.name)
try:
with sync_playwright() as p:
browser = None
page = None
try:
browser = p.chromium.connect(wss)
page = browser.new_page()
page.set_default_timeout(100000)
page.reporter = reporter
page.test_context = test_context
reporter.on_test_begin(page, test_context)
yield page
# Call on_test_end INSIDE the context before closing browser
try:
result = {"success": test_context.success}
if not test_context.success and test_context.failure_reason:
result["failureDescription"] = test_context.failure_reason
reporter.on_test_end(page, test_context, result)
except Exception as e:
if page:
reporter.on_error(page, e)
raise
finally:
# Close browser while event loop is still active
if page:
page.close()
if browser:
browser.close()
except Exception as e:
test_context.success = False
raise
@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item):
"""Update test context with pass/fail status and failure reason."""
outcome = yield
rep = outcome.get_result()
if rep.when == "call" and hasattr(item, 'funcargs') and 'rpage' in item.funcargs:
page = item.funcargs['rpage']
if hasattr(page, 'test_context'):
page.test_context.success = rep.outcome == "passed"
if rep.outcome == "failed" and rep.longrepr:
# Extract failure reason from the test report
page.test_context.failure_reason = str(rep.longrepr)