Open a browser from Node.js and save a screenshot after a specific element appears. Earlier Node 8 and simple-headless-chrome examples have been removed.
Install using a Node version supported by the project and pin dependencies with a lock file.
npm install puppeteer
import puppeteer from 'puppeteer';
const browser = await puppeteer.launch();
try {
const page = await browser.newPage();
await page.setViewport({ width: 1280, height: 800 });
await page.goto('https://example.com', {
waitUntil: 'domcontentloaded',
timeout: 30000
});
await page.waitForSelector('h1', { timeout: 10000 });
await page.screenshot({ path: 'page.png', fullPage: true });
} finally {
await browser.close();
}
node capture.mjs
Wait for required elements or application state instead of fixed sleeps. Use dedicated test accounts for authentication and keep session files out of the repository. Check OS dependencies and browser sandbox configuration when running in containers.
Keep fixed-path regression tests as reproducible browser scripts. Consider AI first for exploration, test drafts, and failure analysis.