-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample-context-options.js
More file actions
171 lines (162 loc) · 5.2 KB
/
example-context-options.js
File metadata and controls
171 lines (162 loc) · 5.2 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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import { defineConfig, consoleLoggerPlugin, scriptInjectionPlugin } from './src/index.js';
export default defineConfig({
platforms: {
// 桌面平台配置
desktop: {
name: 'Desktop Platform',
url: 'https://example.com',
contextOptions: {
viewport: { width: 1920, height: 1080 },
userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
locale: 'en-US',
timezoneId: 'America/New_York',
colorScheme: 'light',
reducedMotion: 'no-preference',
deviceScaleFactor: 1,
isMobile: false,
hasTouch: false,
ignoreHTTPSErrors: true,
acceptDownloads: true,
extraHTTPHeaders: {
'X-Custom-Header': 'desktop-testing'
}
},
scripts: [
{ path: './scripts/desktop.js', autoInject: true }
]
},
// 移动平台配置
mobile: {
name: 'Mobile Platform',
url: 'https://example.com',
contextOptions: {
viewport: { width: 375, height: 667 }, // iPhone SE
userAgent: 'Mozilla/5.0 (iPhone; CPU iPhone OS 14_0 like Mac OS X) AppleWebKit/605.1.15',
locale: 'en-US',
timezoneId: 'America/Los_Angeles',
colorScheme: 'light',
deviceScaleFactor: 2,
isMobile: true,
hasTouch: true,
permissions: ['geolocation', 'notifications'],
extraHTTPHeaders: {
'X-Custom-Header': 'mobile-testing'
}
},
scripts: [
{ path: './scripts/mobile.js', autoInject: true }
]
},
// 平板配置
tablet: {
name: 'Tablet Platform',
url: 'https://example.com',
contextOptions: {
viewport: { width: 768, height: 1024 }, // iPad
userAgent: 'Mozilla/5.0 (iPad; CPU OS 14_0 like Mac OS X) AppleWebKit/605.1.15',
locale: 'zh-CN',
timezoneId: 'Asia/Shanghai',
colorScheme: 'dark',
deviceScaleFactor: 2,
isMobile: false,
hasTouch: true,
permissions: ['camera', 'microphone'],
extraHTTPHeaders: {
'X-Device-Type': 'tablet',
'X-Test-Environment': 'development'
}
},
scripts: [
{ path: './scripts/tablet.js', autoInject: true }
]
},
// 高级配置示例
advanced: {
name: 'Advanced Testing Platform',
url: 'https://example.com',
contextOptions: {
viewport: { width: 1440, height: 900 },
userAgent: 'Custom Test Agent 1.0',
locale: 'ja-JP',
timezoneId: 'Asia/Tokyo',
colorScheme: 'dark',
reducedMotion: 'reduce',
forcedColors: 'active',
deviceScaleFactor: 1.5,
isMobile: false,
hasTouch: false,
offline: false,
ignoreHTTPSErrors: true,
acceptDownloads: true,
bypassCSP: true,
permissions: ['geolocation', 'camera', 'microphone', 'notifications'],
extraHTTPHeaders: {
'Authorization': 'Bearer test-token',
'X-API-Version': 'v2',
'X-Test-Suite': 'advanced-features'
},
httpCredentials: {
username: 'testuser',
password: 'testpass'
},
// 录制选项(可选)
recordVideo: {
dir: './test-videos/',
size: { width: 1440, height: 900 }
},
recordHar: {
path: './test-logs/network.har',
mode: 'full',
content: 'attach'
}
},
scripts: [
{ path: './scripts/advanced.js', autoInject: true }
]
}
},
plugins: [
consoleLoggerPlugin(),
scriptInjectionPlugin(),
// 自定义插件:显示上下文信息
{
name: 'context-info',
order: 100,
async platformReady(platformId, page) {
// 注入脚本来显示上下文信息
await page.addScriptTag({
content: `
console.log('🌐 Platform Context Info:', {
platform: '${platformId}',
viewport: {
width: window.innerWidth,
height: window.innerHeight,
devicePixelRatio: window.devicePixelRatio
},
userAgent: navigator.userAgent,
language: navigator.language,
cookieEnabled: navigator.cookieEnabled,
onLine: navigator.onLine,
touchSupport: 'ontouchstart' in window
});
// 在页面上显示平台信息
const infoDiv = document.createElement('div');
infoDiv.innerHTML = \`
<h3>Platform: ${platformId}</h3>
<p>Viewport: \${window.innerWidth}x\${window.innerHeight}</p>
<p>Device Pixel Ratio: \${window.devicePixelRatio}</p>
<p>Language: \${navigator.language}</p>
<p>Touch Support: \${'ontouchstart' in window ? 'Yes' : 'No'}</p>
\`;
infoDiv.style.cssText = 'position: fixed; top: 10px; right: 10px; background: rgba(0,0,0,0.8); color: white; padding: 15px; border-radius: 8px; font-family: monospace; font-size: 12px; z-index: 9999;';
document.body.appendChild(infoDiv);
`
});
}
}
],
browserOptions: {
headless: false,
devtools: true,
}
});