-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathscreenshot.js
More file actions
108 lines (93 loc) · 3 KB
/
Copy pathscreenshot.js
File metadata and controls
108 lines (93 loc) · 3 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
/**
* Screenshot capture and naming
* Pure functions for screenshot operations
*/
let vizzlyScreenshot;
// Dynamically import to avoid issues in test environment
try {
let module = await import('@vizzly-testing/cli/client');
vizzlyScreenshot = module.vizzlyScreenshot;
} catch (error) {
console.warn('Warning: Could not import Vizzly client SDK:', error.message);
// Mock for testing
vizzlyScreenshot = async () => {};
}
/**
* Generate screenshot name from page path
* Viewport info goes in properties for grouping
* @param {Object} page - Page object with path property
* @returns {string} Screenshot name
*/
export function generateScreenshotName(page) {
let { path } = page;
// Remove leading slash for cleaner names
let cleanPath = path.startsWith('/') ? path.slice(1) : path;
// Handle root path
if (!cleanPath || cleanPath === '') {
cleanPath = 'index';
}
// Replace slashes and backslashes with hyphens to create valid filename
cleanPath = cleanPath.replace(/[/\\]/g, '-');
// Replace double dots (path traversal sequences) with single dots
cleanPath = cleanPath.replace(/\.\./g, '.');
return cleanPath;
}
/**
* Generate screenshot properties from viewport
* Properties are used by Vizzly for grouping and identification
* @param {Object} viewport - Viewport object with name, width, height
* @returns {Object} Screenshot properties
*/
export function generateScreenshotProperties(viewport) {
return {
viewport: viewport.name,
viewportWidth: viewport.width,
viewportHeight: viewport.height,
};
}
/**
* Default screenshot timeout in milliseconds (45 seconds)
* If a page can't render within this time, something is likely wrong
*/
let DEFAULT_SCREENSHOT_TIMEOUT = 45_000;
/**
* Capture a screenshot from a page
* @param {Object} page - Puppeteer page instance
* @param {Object} options - Screenshot options
* @param {boolean} [options.fullPage=false] - Capture full page
* @param {boolean} [options.omitBackground=false] - Omit background
* @param {number} [options.timeout=45000] - Screenshot timeout in ms
* @returns {Promise<Buffer>} Screenshot buffer
*/
export async function captureScreenshot(page, options = {}) {
let {
fullPage = false,
omitBackground = false,
timeout = DEFAULT_SCREENSHOT_TIMEOUT,
} = options;
let screenshot = await page.screenshot({
fullPage,
omitBackground,
timeout,
});
return screenshot;
}
/**
* Capture and send screenshot to Vizzly
* @param {Object} page - Puppeteer page instance
* @param {Object} pageObj - Page object
* @param {Object} viewport - Viewport object
* @param {Object} screenshotOptions - Screenshot options
* @returns {Promise<void>}
*/
export async function captureAndSendScreenshot(
page,
pageObj,
viewport,
screenshotOptions = {}
) {
let name = generateScreenshotName(pageObj);
let properties = generateScreenshotProperties(viewport);
let screenshot = await captureScreenshot(page, screenshotOptions);
await vizzlyScreenshot(name, screenshot, { properties });
}