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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
| # test_complete_environment.py
@pytest.fixture
def complete_test_environment(playwright):
"""完整的测试环境"""
# 启动浏览器
browser = playwright.chromium.launch(headless=True)
# 创建上下文
context = browser.new_context(
viewport={"width": 1920, "height": 1080},
locale="en-US",
timezone_id="America/New_York",
permissions=["geolocation", "notifications"]
)
# 创建页面
page = context.new_page()
# 创建 API 上下文
api_context = playwright.request.new_context({
"base_url": "http://localhost:8080",
"extra_http_headers": {
"Content-Type": "application/json"
}
})
# 设置网络拦截
def handle_route(route: Route):
if "localhost:8080/api" in route.request.url:
# 记录 API 调用
print(f"API Call: {route.request.method} {route.request.url}")
route.continue_()
page.route("**/*", handle_route)
# 设置性能监控
performance_data = []
def handle_metrics(metrics):
performance_data.append(metrics)
page.on("metrics", handle_metrics)
# 创建测试工具集
test_tools = {
"page": page,
"context": context,
"browser": browser,
"api_context": api_context,
"performance_data": performance_data,
# 工具方法
"login": lambda username, password: (
page.goto("http://localhost:8080"),
page.fill("#username", username),
page.fill("#password", password),
page.click("#login-button"),
page.wait_for_url("http://localhost:8080/dashboard")
),
"screenshot_on_failure": lambda: page.screenshot(
path=f"test-results/screenshots/failure_{int(time.time())}.png"
),
"get_performance_metrics": lambda: performance_data[-1] if performance_data else None
}
yield test_tools
# 清理
page.close()
context.close()
browser.close()
api_context.dispose()
def test_complete_workflow(complete_test_environment):
"""完整工作流测试"""
tools = complete_test_environment
# 创建登录表单
tools["page"].set_content("""
<!DOCTYPE html>
<html>
<head><title>Login</title></head>
<body>
<form id="login-form">
<input type="text" id="username" placeholder="Username">
<input type="password" id="password" placeholder="Password">
<button type="submit" id="login-button">Login</button>
</form>
<script>
document.getElementById('login-form').addEventListener('submit', async (e) => {
e.preventDefault();
const username = document.getElementById('username').value;
const password = document.getElementById('password').value;
const response = await fetch('/api/login', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({username, password})
});
if (response.ok) {
window.location.href = '/dashboard';
}
});
</script>
</body>
</html>
""")
# 登录
tools["login"]("testuser", "testpass")
# 验证登录成功
assert tools["page"].url == "http://localhost:8080/dashboard"
# 验证用户信息显示
user_info = tools["page"].locator("#user-info")
assert user_info.is_visible()
assert "Test User" in user_info.text_content()
assert "test@example.com" in user_info.text_content()
# 获取性能指标
metrics = tools["get_performance_metrics"]()
if metrics:
print(f"页面性能指标: {metrics}")
|