$npx -y skills add LambdaTest/agent-skills --skill puppeteer-skillGenerates Puppeteer scripts for browser automation, scraping, and PDF generation. Triggers on: "Puppeteer", "headless Chrome", "page.goto", "scrape", "PDF generation".
| 1 | # Puppeteer Automation Skill |
| 2 | |
| 3 | ## Core Patterns |
| 4 | |
| 5 | ### Basic Script |
| 6 | |
| 7 | ```javascript |
| 8 | const puppeteer = require('puppeteer'); |
| 9 | |
| 10 | (async () => { |
| 11 | const browser = await puppeteer.launch({ headless: 'new' }); |
| 12 | const page = await browser.newPage(); |
| 13 | await page.setViewport({ width: 1280, height: 720 }); |
| 14 | |
| 15 | await page.goto('https://example.com', { waitUntil: 'networkidle0' }); |
| 16 | await page.type('#username', 'user@test.com'); |
| 17 | await page.type('#password', 'password123'); |
| 18 | await page.click('button[type="submit"]'); |
| 19 | await page.waitForNavigation({ waitUntil: 'networkidle0' }); |
| 20 | |
| 21 | const title = await page.title(); |
| 22 | console.log('Title:', title); |
| 23 | |
| 24 | await browser.close(); |
| 25 | })(); |
| 26 | ``` |
| 27 | |
| 28 | ### Wait Strategies |
| 29 | |
| 30 | ```javascript |
| 31 | // Wait for selector |
| 32 | await page.waitForSelector('.result', { visible: true, timeout: 10000 }); |
| 33 | |
| 34 | // Wait for navigation |
| 35 | await Promise.all([ |
| 36 | page.waitForNavigation({ waitUntil: 'networkidle0' }), |
| 37 | page.click('a.nav-link'), |
| 38 | ]); |
| 39 | |
| 40 | // Wait for function |
| 41 | await page.waitForFunction('document.querySelector(".count").innerText === "5"'); |
| 42 | |
| 43 | // Wait for network request |
| 44 | const response = await page.waitForResponse(resp => |
| 45 | resp.url().includes('/api/data') && resp.status() === 200 |
| 46 | ); |
| 47 | ``` |
| 48 | |
| 49 | ### Screenshot & PDF |
| 50 | |
| 51 | ```javascript |
| 52 | await page.screenshot({ path: 'screenshot.png', fullPage: true }); |
| 53 | await page.pdf({ path: 'page.pdf', format: 'A4', printBackground: true }); |
| 54 | ``` |
| 55 | |
| 56 | ### Network Interception |
| 57 | |
| 58 | ```javascript |
| 59 | await page.setRequestInterception(true); |
| 60 | page.on('request', request => { |
| 61 | if (request.resourceType() === 'image') request.abort(); |
| 62 | else request.continue(); |
| 63 | }); |
| 64 | |
| 65 | // Mock API |
| 66 | page.on('request', request => { |
| 67 | if (request.url().includes('/api/data')) { |
| 68 | request.respond({ |
| 69 | status: 200, |
| 70 | contentType: 'application/json', |
| 71 | body: JSON.stringify({ items: [] }), |
| 72 | }); |
| 73 | } else request.continue(); |
| 74 | }); |
| 75 | ``` |
| 76 | |
| 77 | ### TestMu AI Cloud |
| 78 | |
| 79 | For full setup, capabilities, and shared capability reference, see [reference/cloud-integration.md](reference/cloud-integration.md). |
| 80 | |
| 81 | ```javascript |
| 82 | const capabilities = { |
| 83 | browserName: 'Chrome', browserVersion: 'latest', |
| 84 | 'LT:Options': { |
| 85 | platform: 'Windows 11', build: 'Puppeteer Build', |
| 86 | user: process.env.LT_USERNAME, accessKey: process.env.LT_ACCESS_KEY, |
| 87 | }, |
| 88 | }; |
| 89 | |
| 90 | const browser = await puppeteer.connect({ |
| 91 | browserWSEndpoint: `wss://cdp.lambdatest.com/puppeteer?capabilities=${encodeURIComponent(JSON.stringify(capabilities))}`, |
| 92 | }); |
| 93 | ``` |
| 94 | |
| 95 | ## Quick Reference |
| 96 | |
| 97 | | Task | Code | |
| 98 | |------|------| |
| 99 | | Launch headed | `puppeteer.launch({ headless: false })` | |
| 100 | | Evaluate JS | `await page.evaluate(() => document.title)` | |
| 101 | | Extract text | `await page.$eval('.el', el => el.textContent)` | |
| 102 | | Extract all | `await page.$$eval('.items', els => els.map(e => e.textContent))` | |
| 103 | | Set cookie | `await page.setCookie({ name: 'token', value: 'abc' })` | |
| 104 | | Emulate device | `await page.emulate(puppeteer.devices['iPhone 12'])` | |
| 105 | |
| 106 | ## Deep Patterns → `reference/playbook.md` |
| 107 | |
| 108 | | § | Section | Lines | |
| 109 | |---|---------|-------| |
| 110 | | 1 | Production Setup & Configuration | Launch options, Jest integration | |
| 111 | | 2 | Page Object Pattern | BasePage, LoginPage, DashboardPage | |
| 112 | | 3 | Network Interception & Mocking | Request mock, response capture | |
| 113 | | 4 | Wait Strategies | DOM, network, custom conditions | |
| 114 | | 5 | Screenshots, PDF & Media | Full page, clip, PDF, video | |
| 115 | | 6 | Authentication & Cookies | API login, session save/restore | |
| 116 | | 7 | iFrame, Dialog & File Operations | Upload, download, dialogs | |
| 117 | | 8 | Performance & Metrics | Web Vitals, Lighthouse, coverage | |
| 118 | | 9 | Accessibility Testing | axe-core integration | |
| 119 | | 10 | CI/CD Integration | GitHub Actions, Docker | |
| 120 | | 11 | Debugging Quick-Reference | 11 common problems | |
| 121 | | 12 | Best Practices Checklist | 13 items | |