-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Expand file tree
/
Copy pathtest-helper.js
More file actions
56 lines (52 loc) · 1.49 KB
/
test-helper.js
File metadata and controls
56 lines (52 loc) · 1.49 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
/**
* Helper utilities for web application testing with Playwright
*/
/**
* Wait for a condition to be true with timeout
* @param {Function} condition - Function that returns boolean
* @param {number} timeout - Timeout in milliseconds
* @param {number} interval - Check interval in milliseconds
*/
async function waitForCondition(condition, timeout = 5000, interval = 100) {
const startTime = Date.now();
while (Date.now() - startTime < timeout) {
if (await condition()) {
return true;
}
await new Promise(resolve => setTimeout(resolve, interval));
}
throw new Error('Condition not met within timeout');
}
/**
* Capture browser console logs
* @param {Page} page - Playwright page object
* @returns {Array} Array of console messages
*/
function captureConsoleLogs(page) {
const logs = [];
page.on('console', msg => {
logs.push({
type: msg.type(),
text: msg.text(),
timestamp: new Date().toISOString()
});
});
return logs;
}
/**
* Take screenshot with automatic naming
* @param {Page} page - Playwright page object
* @param {string} name - Base name for screenshot
*/
async function captureScreenshot(page, name) {
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
const filename = `${name}-${timestamp}.png`;
await page.screenshot({ path: filename, fullPage: true });
console.log(`Screenshot saved: ${filename}`);
return filename;
}
module.exports = {
waitForCondition,
captureConsoleLogs,
captureScreenshot
};