Puppeteer Automation Skill
Core Patterns
Basic Script
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch({ headless: 'new' });
const page = await browser.newPage();
await page.setViewport({ width: 1280, height: 720 });
await page.goto('https://example.com', { waitUntil: 'networkidle0' });
await page.type('#username', 'user@test.com');
await page.type('#password', 'password123');
await page.click('button[type="submit"]');
await page.waitForNavigation({ waitUntil: 'networkidle0' });
const title = await page.title();
console.log('Title:', title);
await browser.close();
})();
Wait Strategies
// Wait for selector
await page.waitForSelector('.result', { visible: true, timeout: 10000 });
// Wait for navigation
await Promise.all([
page.waitForNavigation({ waitUntil: 'networkidle0' }),
page.click('a.nav-link'),
]);
// Wait for function
await page.waitForFunction('document.querySelector(".count").innerText === "5"');
// Wait for network request
const response = await page.waitForResponse(resp =>
resp.url().includes('/api/data') && resp.status() === 200
);
Screenshot & PDF
await page.screenshot({ path: 'screenshot.png', fullPage: true });
await page.pdf({ path: 'page.pdf', format: 'A4', printBackground: true });
Network Interception
await page.setRequestInterception(true);
page.on('request', request => {
if (request.resourceType() === 'image') request.abort();
else request.continue();
});
// Mock API
page.on('request', request => {
if (request.url().includes('/api/data')) {
request.respond({
status: 200,
contentType: 'application/json',
body: JSON.stringify({ items: [] }),
});
} else request.continue();
});
TestMu AI Cloud
For full setup, capabilities, and shared capability reference, see reference/cloud-integration.md.
const capabilities = {
browserName: 'Chrome', browserVersion: 'latest',
'LT:Options': {
platform: 'Windows 11', build: 'Puppeteer Build',
user: process.env.LT_USERNAME, accessKey: process.env.LT_ACCESS_KEY,
},
};
const browser = await puppeteer.connect({
browserWSEndpoint: `wss://cdp.lambdatest.com/puppeteer?capabilities=${encodeURIComponent(JSON.stringify(capabilities))}`,
});
Quick Reference
| Task |
Code |
| Launch headed |
puppeteer.launch({ headless: false }) |
| Evaluate JS |
await page.evaluate(() => document.title) |
| Extract text |
await page.$eval('.el', el => el.textContent) |
| Extract all |
await page.$$eval('.items', els => els.map(e => e.textContent)) |
| Set cookie |
await page.setCookie({ name: 'token', value: 'abc' }) |
| Emulate device |
await page.emulate(puppeteer.devices['iPhone 12']) |
Deep Patterns → reference/playbook.md
| § |
Section |
Lines |
| 1 |
Production Setup & Configuration |
Launch options, Jest integration |
| 2 |
Page Object Pattern |
BasePage, LoginPage, DashboardPage |
| 3 |
Network Interception & Mocking |
Request mock, response capture |
| 4 |
Wait Strategies |
DOM, network, custom conditions |
| 5 |
Screenshots, PDF & Media |
Full page, clip, PDF, video |
| 6 |
Authentication & Cookies |
API login, session save/restore |
| 7 |
iFrame, Dialog & File Operations |
Upload, download, dialogs |
| 8 |
Performance & Metrics |
Web Vitals, Lighthouse, coverage |
| 9 |
Accessibility Testing |
axe-core integration |
| 10 |
CI/CD Integration |
GitHub Actions, Docker |
| 11 |
Debugging Quick-Reference |
11 common problems |
| 12 |
Best Practices Checklist |
13 items |
1---2name: lambdatest-puppeteer-skill3description: Generates Puppeteer scripts for browser automation, scraping, and PDF generation. Triggers on: "Puppeteer", "headless Chrome", "page.goto", "scrape", "PDF generation".4license: MIT5---67# Puppeteer Automation Skill89## Core Patterns1011### Basic Script1213```javascript14const puppeteer = require('puppeteer');1516(async () => {17 const browser = await puppeteer.launch({ headless: 'new' });18 const page = await browser.newPage();19 await page.setViewport({ width: 1280, height: 720 });2021 await page.goto('https://example.com', { waitUntil: 'networkidle0' });22 await page.type('#username', 'user@test.com');23 await page.type('#password', 'password123');24 await page.click('button[type="submit"]');25 await page.waitForNavigation({ waitUntil: 'networkidle0' });2627 const title = await page.title();28 console.log('Title:', title);2930 await browser.close();31})();32```3334### Wait Strategies3536```javascript37// Wait for selector38await page.waitForSelector('.result', { visible: true, timeout: 10000 });3940// Wait for navigation41await Promise.all([42 page.waitForNavigation({ waitUntil: 'networkidle0' }),43 page.click('a.nav-link'),44]);4546// Wait for function47await page.waitForFunction('document.querySelector(".count").innerText === "5"');4849// Wait for network request50const response = await page.waitForResponse(resp =>51 resp.url().includes('/api/data') && resp.status() === 20052);53```5455### Screenshot & PDF5657```javascript58await page.screenshot({ path: 'screenshot.png', fullPage: true });59await page.pdf({ path: 'page.pdf', format: 'A4', printBackground: true });60```6162### Network Interception6364```javascript65await page.setRequestInterception(true);66page.on('request', request => {67 if (request.resourceType() === 'image') request.abort();68 else request.continue();69});7071// Mock API72page.on('request', request => {73 if (request.url().includes('/api/data')) {74 request.respond({75 status: 200,76 contentType: 'application/json',77 body: JSON.stringify({ items: [] }),78 });79 } else request.continue();80});81```8283### TestMu AI Cloud8485For full setup, capabilities, and shared capability reference, see [reference/cloud-integration.md](reference/cloud-integration.md).8687```javascript88const capabilities = {89 browserName: 'Chrome', browserVersion: 'latest',90 'LT:Options': {91 platform: 'Windows 11', build: 'Puppeteer Build',92 user: process.env.LT_USERNAME, accessKey: process.env.LT_ACCESS_KEY,93 },94};9596const browser = await puppeteer.connect({97 browserWSEndpoint: `wss://cdp.lambdatest.com/puppeteer?capabilities=${encodeURIComponent(JSON.stringify(capabilities))}`,98});99```100101## Quick Reference102103| Task | Code |104|------|------|105| Launch headed | `puppeteer.launch({ headless: false })` |106| Evaluate JS | `await page.evaluate(() => document.title)` |107| Extract text | `await page.$eval('.el', el => el.textContent)` |108| Extract all | `await page.$$eval('.items', els => els.map(e => e.textContent))` |109| Set cookie | `await page.setCookie({ name: 'token', value: 'abc' })` |110| Emulate device | `await page.emulate(puppeteer.devices['iPhone 12'])` |111112## Deep Patterns → `reference/playbook.md`113114| § | Section | Lines |115|---|---------|-------|116| 1 | Production Setup & Configuration | Launch options, Jest integration |117| 2 | Page Object Pattern | BasePage, LoginPage, DashboardPage |118| 3 | Network Interception & Mocking | Request mock, response capture |119| 4 | Wait Strategies | DOM, network, custom conditions |120| 5 | Screenshots, PDF & Media | Full page, clip, PDF, video |121| 6 | Authentication & Cookies | API login, session save/restore |122| 7 | iFrame, Dialog & File Operations | Upload, download, dialogs |123| 8 | Performance & Metrics | Web Vitals, Lighthouse, coverage |124| 9 | Accessibility Testing | axe-core integration |125| 10 | CI/CD Integration | GitHub Actions, Docker |126| 11 | Debugging Quick-Reference | 11 common problems |127| 12 | Best Practices Checklist | 13 items |