agentleFS
nurmdrafinurmdrafi/pi-native/skills/puppeteer/SKILL.md

puppeteer

Skill4 starsChanged 25 days agoMITView on GitHub

Puppeteer / headless Chrome automation: scripted browser flows, scraping, screenshot and PDF generation, network interception, CDP sessions. Triggers on: "Puppeteer", "headless Chrome", "page.goto", "scrape", "render HTML to PDF", "print to PDF", "PDF parity", "extra blank page", "PDF fonts wrong", "table columns shift".

  • Reads credentials

2 other projects use a version of this, 1 of them edited.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

2 rules. About 0 are common, 1 is distinctive, and 1 is very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈1

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 1

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    printBackground + print-color-adjust: exact: keep header fills and borders.

    line 125
  2. Distinctive:

    Check page dimensions, count and order before comparing text bounds and embedded fonts.

    line 151

≈ “Common” is an estimate: at least 1 in 5 of the 408,460 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Puppeteer Automation Skill

Puppeteer ships breaking API moves every few majors; the snippets below are kept current for puppeteer >= 22 and called out where older guides are wrong:

  • headless: 'new' was removed — use headless: true (full Chrome) or headless: 'shell' (old headless shell, lighter, no extensions).
  • page.waitForTimeout was removed — use new Promise(r => setTimeout(r, ms)) or a real condition (waitForSelector / waitForFunction).
  • page.setContent(html, { waitUntil: 'networkidle0' }) is rejected by the typings in v25: wait for load, then await page.waitForNetworkIdle().
  • browser.on('disconnected', …) fires on crash/close — a long-lived service must drop its cached browser there, otherwise every later call fails.
  • Inside containers Chromium needs --no-sandbox --disable-setuid-sandbox (no user namespaces for the runtime user) and its shared libraries plus fonts installed, or it exits before the first navigation.

Core Patterns

Basic Script

const puppeteer = require('puppeteer');

(async () => {
    const browser = await puppeteer.launch({ headless: true });
    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 });

A PDF that must match a reference render is a different job from a PDF that merely exists — go to Print parity below before shipping one.

Print parity (HTML → PDF)

Chrome is the only engine that lays out the document you author and the PDF you print with the same code, so parity work is about pinning the inputs it leaves loose. scripts/html-to-pdf.mjs does the whole sequence for one file:

node scripts/html-to-pdf.mjs input.html out.pdf --public ./public \
    --margin 5 --expect-pages 1

Each invariant below silently changes the output; none of them errors:

  • An unloaded webfont is a different document. waitUntil: 'load' does not cover @font-face; printing mid-load falls back to the system family and every glyph width shifts. Await document.fonts.ready before page.pdf().
  • Webfonts are cross-origin when the document came from setContent. That document has an opaque origin, so @font-face fetches need an Access-Control-Allow-Origin header. Without it Chrome logs a font error and prints the fallback — the asset server has to answer CORS, not the page.
  • The UA sheet sizes tables at 1em of medium (16px), not the document size. A table inside body{font-size:10px} still prints at 16px: cells look oversized and rows inflate. Add table { font-size: inherit } when porting another engine's stylesheet.
  • A floated row contributes no height. Children of float:left/right collapse the parent, so the next block starts inside the float. Clear it (<div style="clear: both"></div>) instead of trusting float handling.
  • Auto table layout shrinks columns around empty cells. One null value narrows that column in every row; table-layout: fixed plus header widths keeps siblings at their share regardless of content.
  • printBackground + print-color-adjust: exact keep header fills and borders; without both, colored cells print white.
  • px → pt is exactly 0.75 (96px = 1in = 72pt), so a px stylesheet ports without unit conversion; page.pdf({ margin }) takes its own units (mm).
  • A stray page means the first block overflowed. Negative margins, a background on body, or an un-cleared float push the layout down; fix the cause rather than shrinking line-height until it fits.
  • Page size has one source. preferCSSPageSize: false keeps format + margin authoritative, so a template @page rule cannot resize the sheet.

For migration parity, the reference is the final PDF from the original generator, not the HTML preview. Compare the same sanitized input snapshot, selected records, assets, locale and time. Before adjusting CSS, check each printed value against its expected record and section. Include repeated records with different answers and intentional blanks: an unavailable source is not evidence for a blank or a fallback value. Report a binding mismatch separately from a positioning mismatch.

Inspect both assembled PDFs, including appended pages:

pdfinfo out.pdf | grep Pages      # page count
pdffonts out.pdf                  # embedded family: a system name means failure
pdftohtml -xml -stdout out.pdf    # per-string top/left/width for a diff
pdftocairo -svg out.pdf out.svg   # border weights/colors as vector data

Check page dimensions, count and order before comparing text bounds and embedded fonts. Rasterize both PDFs with the same rasterizer and resolution; overlay each page to catch shifted seals, clipped captions, missing glyphs and distorted attachment images. Neither matching text nor matching geometry proves data identity. Trace extra or missing pages to selection, assembly or pagination before changing margins. Record the fixture, render settings and result per format; no paired reference means unverified, even if a sibling format passes.

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

TaskCode
Launch headedpuppeteer.launch({ headless: false })
Evaluate JSawait page.evaluate(() => document.title)
Extract textawait page.$eval('.el', el => el.textContent)
Extract allawait page.$$eval('.items', els => els.map(e => e.textContent))
Set cookieawait page.setCookie({ name: 'token', value: 'abc' })
Emulate deviceawait page.emulate(puppeteer.devices['iPhone 12'])

Deep Patterns → reference/playbook.md

Also in reference/: advanced-patterns.md (PDF/screenshot tuning, parallelism, CDP session reuse) and cloud-integration.md (TestMu AI / LambdaTest remote grids).

§SectionLines
1Production Setup & ConfigurationLaunch options, Jest integration
2Page Object PatternBasePage, LoginPage, DashboardPage
3Network Interception & MockingRequest mock, response capture
4Wait StrategiesDOM, network, custom conditions
5Screenshots, PDF & MediaFull page, clip, PDF, video
—Print parity (HTML → PDF)In-file section above + scripts/html-to-pdf.mjs
6Authentication & CookiesAPI login, session save/restore
7iFrame, Dialog & File OperationsUpload, download, dialogs
8Performance & MetricsWeb Vitals, Lighthouse, coverage
9Accessibility Testingaxe-core integration
10CI/CD IntegrationGitHub Actions, Docker
11Debugging Quick-Reference11 common problems
12Best Practices Checklist13 items

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.