| name | chrome |
| description | Browser automation using Puppeteer or Playwright. Use for web testing, screenshots, form filling, and automated browser interactions. |
Chrome Automation
Automate browser interactions using Puppeteer or Playwright.
Prerequisites
npm install puppeteer
npm install playwright
npx playwright install chromium
Puppeteer Quick Start
Basic Script
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://example.com');
await page.screenshot({ path: 'screenshot.png' });
await browser.close();
})();
Run: node script.js
With Visible Browser
const browser = await puppeteer.launch({
headless: false,
slowMo: 50,
});
Playwright Quick Start
Basic Script
const { chromium } = require('playwright');
(async () => {
const browser = await chromium.launch();
const page = await browser.newPage();
await page.goto('https://example.com');
await page.screenshot({ path: 'screenshot.png' });
await browser.close();
})();
Common Operations
Navigation
await page.goto('https://example.com');
await page.goto('https://example.com', { waitUntil: 'networkidle0' });
await page.goBack();
await page.goForward();
await page.reload();
Screenshots
await page.screenshot({ path: 'full.png', fullPage: true });
const element = await page.$('#header');
await element.screenshot({ path: 'header.png' });
await page.screenshot({
path: 'screenshot.png',
type: 'png',
quality: 90,
clip: { x: 0, y: 0, width: 800, height: 600 }
});
Click Actions
await page.click('#button');
await page.click('button.submit');
await page.dblclick('#item');
await page.click('#element', { button: 'right' });
await Promise.all([
page.waitForNavigation(),
page.click('a.link')
]);
Form Filling
await page.type('#email', 'user@example.com');
await page.fill('#email', 'user@example.com');
await page.$eval('#email', el => el.value = '');
await page.type('#email', 'user@example.com');
await page.select('#country', 'US');
await page.check('#agree');
await page.click('#agree');
await page.setInputFiles('#file', '/path/to/file.pdf');
const input = await page.$('#file');
await input.uploadFile('/path/to/file.pdf');
Waiting
await page.waitForSelector('#loaded');
await page.waitForFunction(() =>
document.body.textContent.includes('Success')
);
await page.waitForNavigation();
await page.waitForLoadState('networkidle');
await page.waitForTimeout(1000);
Extract Data
const text = await page.textContent('#element');
const text = await page.$eval('#element', el => el.textContent);
const href = await page.getAttribute('a', 'href');
const href = await page.$eval('a', el => el.href);
const items = await page.$$eval('.item', els =>
els.map(el => el.textContent)
);
const html = await page.content();
Evaluate JavaScript
const result = await page.evaluate(() => {
return document.title;
});
const text = await page.evaluate((selector) => {
return document.querySelector(selector).textContent;
}, '#element');
Testing Patterns
Login Flow
async function login(page, username, password) {
await page.goto('https://app.example.com/login');
await page.fill('#username', username);
await page.fill('#password', password);
await page.click('button[type="submit"]');
await page.waitForSelector('#dashboard');
}
Form Submission Test
async function testForm(page) {
await page.goto('https://example.com/form');
await page.fill('#name', 'Test User');
await page.fill('#email', 'test@example.com');
await page.select('#country', 'US');
await page.check('#agree');
await page.click('button[type="submit"]');
await page.waitForSelector('.success-message');
const message = await page.textContent('.success-message');
console.assert(message.includes('Thank you'));
}
Visual Regression
await page.screenshot({ path: 'baseline.png', fullPage: true });
await page.screenshot({ path: 'current.png', fullPage: true });
Device Emulation
const iPhone = puppeteer.devices['iPhone 12'];
await page.emulate(iPhone);
const iPhone = playwright.devices['iPhone 12'];
const context = await browser.newContext({ ...iPhone });
await page.setViewportSize({ width: 375, height: 812 });
Network
await page.route('**/api/*', route => {
route.fulfill({ status: 200, body: JSON.stringify({ mocked: true }) });
});
await page.route('**/*.{png,jpg,jpeg}', route => route.abort());
page.on('request', req => console.log(req.url()));
page.on('response', res => console.log(res.status(), res.url()));
Best Practices
- Use explicit waits - Not timeouts
- Handle errors - try/catch important
- Close browsers - Always clean up
- Use headless for CI - Faster, no display needed
- Test selectors - Prefer data-testid
- Screenshot on failure - Debug easier
- Reuse contexts - Faster than new browsers