-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtake-emergency-screenshot.js
More file actions
78 lines (64 loc) · 2.2 KB
/
take-emergency-screenshot.js
File metadata and controls
78 lines (64 loc) · 2.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
#!/usr/bin/env node
/**
* Emergency screenshot utility
* Takes screenshots of all open Chrome processes when tests are force killed
*/
const puppeteer = require('puppeteer');
const path = require('path');
const fs = require('fs');
// Create screenshots folder if it doesn't exist
const screenshotsDir = path.join(__dirname, 'emergency-screenshots');
try {
fs.mkdirSync(screenshotsDir, { recursive: true });
} catch (err) {
if (err.code !== 'EEXIST') {
console.error('Failed to create screenshots directory:', err);
}
}
// Generate timestamp for filenames
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
async function takeEmergencyScreenshots() {
console.log('⚠️ Taking emergency screenshots of browser state...');
try {
// Connect to any running Chrome instances
const browser = await puppeteer.connect({
browserURL: 'http://localhost:9222',
defaultViewport: null
}).catch(() => null);
if (!browser) {
console.log('No running browser found to screenshot');
return;
}
// Get all open pages
const pages = await browser.pages();
console.log(`Found ${pages.length} open pages, taking screenshots...`);
// Take screenshots of each page
const screenshotPromises = pages.map(async (page, index) => {
try {
const url = await page.url();
const sanitizedUrl = url.replace(/[^a-z0-9]/gi, '_').substring(0, 30);
const filename = path.join(
screenshotsDir,
`emergency-${timestamp}-page${index}-${sanitizedUrl}.png`
);
await page.screenshot({
path: filename,
fullPage: true
});
console.log(`Screenshot saved: ${filename}`);
} catch (err) {
console.error(`Failed to screenshot page ${index}:`, err);
}
});
await Promise.all(screenshotPromises);
// Don't disconnect from the browser - let it be handled by the main process
} catch (err) {
console.error('Error taking emergency screenshots:', err);
}
}
// Execute immediately
takeEmergencyScreenshots()
.catch(err => {
console.error('Fatal error taking emergency screenshots:', err);
process.exit(1);
});