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: puppeteer-skill3description: Generates Puppeteer scripts for browser automation, scraping, and PDF generation. Triggers on: "Puppeteer", "headless Chrome", "page.goto", "scrape", "PDF generation".4license: MIT5---6
7# Puppeteer Automation Skill
8
9## Core Patterns
10
11### Basic Script
12
13```javascript
14const puppeteer = require('puppeteer');
15
16(async () => {
17 const browser = await puppeteer.launch({ headless: 'new' });
18 const page = await browser.newPage();
19 await page.setViewport({ width: 1280, height: 720 });
20
21 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' });
26
27 const title = await page.title();
28 console.log('Title:', title);
29
30 await browser.close();
31})();
32```
33
34### Wait Strategies
35
36```javascript
37// Wait for selector
38await page.waitForSelector('.result', { visible: true, timeout: 10000 });
39
40// Wait for navigation
41await Promise.all([
42 page.waitForNavigation({ waitUntil: 'networkidle0' }),
43 page.click('a.nav-link'),
44]);
45
46// Wait for function
47await page.waitForFunction('document.querySelector(".count").innerText === "5"');
48
49// Wait for network request
50const response = await page.waitForResponse(resp =>
51 resp.url().includes('/api/data') && resp.status() === 200
52);
53```
54
55### Screenshot & PDF
56
57```javascript
58await page.screenshot({ path: 'screenshot.png', fullPage: true });
59await page.pdf({ path: 'page.pdf', format: 'A4', printBackground: true });
60```
61
62### Network Interception
63
64```javascript
65await page.setRequestInterception(true);
66page.on('request', request => {
67 if (request.resourceType() === 'image') request.abort();
68 else request.continue();
69});
70
71// Mock API
72page.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```
82
83### TestMu AI Cloud
84
85For full setup, capabilities, and shared capability reference, see [reference/cloud-integration.md](reference/cloud-integration.md).
86
87```javascript
88const 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};
95
96const browser = await puppeteer.connect({
97 browserWSEndpoint: `wss://cdp.lambdatest.com/puppeteer?capabilities=${encodeURIComponent(JSON.stringify(capabilities))}`,
98});
99```
100
101## Quick Reference
102
103| 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'])` |
111
112## Deep Patterns → `reference/playbook.md`
113
114| § | 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 |