-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreporting.py
More file actions
56 lines (44 loc) · 1.92 KB
/
reporting.py
File metadata and controls
56 lines (44 loc) · 1.92 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
import json
from typing import Optional, List, Union
from playwright.sync_api import Page
from contextlib import contextmanager
class CustomReporterConfig:
"""Custom reporter for test execution tracking."""
def on_test_begin(self, page, test_context):
"""Called when a test begins."""
test_name = getattr(test_context, 'title', 'Unknown test')
page.evaluate("perfecto:report:testStart", json.dumps({"name": test_name}))
print(f"Test started: {test_name}")
def on_step_begin(self, page, test_context, parent_step, step):
"""Called when a test step begins."""
step_title = getattr(step, 'title', 'Unknown step')
page.evaluate("perfecto:report:stepStart", json.dumps({"name": step_title}))
print(f"Step started: {step_title}")
def on_test_end(self, page, test_context, result):
"""Called when a test ends."""
test_title = getattr(test_context, 'title', 'Unknown test')
status = getattr(test_context, 'success', False)
page.evaluate("perfecto:report:testEnd", json.dumps(result))
print(f"Test ended: {test_title} - Status: {status}")
def on_error(self, page, error):
"""Called when an error occurs."""
print(f"Error occurred: {error}")
@contextmanager
def step(page, step_name: str):
"""
Context manager for test steps. Automatically calls on_step_begin.
Usage:
with step(page, "Navigate to homepage"):
page.goto("https://example.com")
"""
reporter = getattr(page, 'reporter', None)
test_context = getattr(page, 'test_context', None)
if reporter and test_context:
step = type("Step", (), {"category": "test.step", "title": step_name})()
reporter.on_step_begin(page, test_context, None, step)
try:
yield
except Exception as e:
if reporter:
reporter.on_error(page, e)
raise