home / skills / aidotnet / moyucode / playwright

playwright skill

/skills/tools/playwright

This skill enables end-to-end browser automation across Chromium, Firefox, and WebKit for testing, scraping, and web automation tasks.

npx playbooks add skill aidotnet/moyucode --skill playwright

Review the files below or copy the command above to add this skill to your agents.

Files (1)
SKILL.md
3.6 KB
---
name: playwright
description: 微软开发的跨浏览器自动化框架。使用单一API支持Chromium、Firefox和WebKit,用于测试、爬虫和自动化。
metadata:
  short-description: 跨浏览器自动化框架
source:
  repository: https://github.com/microsoft/playwright
  license: Apache-2.0
  stars: 68k+
---

# Playwright Tool

## Description
Cross-browser automation for testing, scraping, and web automation supporting Chromium, Firefox, and WebKit.

## Source
- Repository: [microsoft/playwright](https://github.com/microsoft/playwright)
- License: Apache-2.0
- Maintainer: Microsoft

## Installation

```bash
npm install playwright
npx playwright install  # Install browsers
```

## Usage Examples

### Browser Automation

```typescript
import { chromium, firefox, webkit } from 'playwright';

async function automateTask() {
  // Launch any browser
  const browser = await chromium.launch({ headless: true });
  const context = await browser.newContext({
    viewport: { width: 1920, height: 1080 },
    userAgent: 'Custom User Agent',
  });
  const page = await context.newPage();
  
  await page.goto('https://example.com');
  
  // Click, type, interact
  await page.click('button.login');
  await page.fill('input[name="email"]', '[email protected]');
  await page.fill('input[name="password"]', 'password123');
  await page.click('button[type="submit"]');
  
  // Wait for navigation
  await page.waitForURL('**/dashboard');
  
  await browser.close();
}
```

### Screenshot & PDF

```typescript
async function captureContent(url: string) {
  const browser = await chromium.launch();
  const page = await browser.newPage();
  
  await page.goto(url);
  
  // Screenshot
  await page.screenshot({
    path: 'screenshot.png',
    fullPage: true,
  });
  
  // PDF (Chromium only)
  await page.pdf({
    path: 'document.pdf',
    format: 'A4',
    margin: { top: '1cm', bottom: '1cm' },
  });
  
  await browser.close();
}
```

### Web Scraping with Locators

```typescript
async function scrapeProducts(url: string) {
  const browser = await chromium.launch();
  const page = await browser.newPage();
  
  await page.goto(url);
  
  // Use locators for reliable element selection
  const products = await page.locator('.product-card').all();
  
  const data = await Promise.all(products.map(async (product) => ({
    name: await product.locator('.name').textContent(),
    price: await product.locator('.price').textContent(),
    rating: await product.locator('.rating').getAttribute('data-value'),
  })));
  
  await browser.close();
  return data;
}
```

### API Testing

```typescript
import { request } from 'playwright';

async function testAPI() {
  const context = await request.newContext({
    baseURL: 'https://api.example.com',
    extraHTTPHeaders: {
      'Authorization': 'Bearer token123',
    },
  });
  
  // GET request
  const response = await context.get('/users');
  const users = await response.json();
  
  // POST request
  const createResponse = await context.post('/users', {
    data: { name: 'John', email: '[email protected]' },
  });
  
  await context.dispose();
}
```

### E2E Testing

```typescript
import { test, expect } from '@playwright/test';

test('user can login', async ({ page }) => {
  await page.goto('/login');
  
  await page.fill('[name="email"]', '[email protected]');
  await page.fill('[name="password"]', 'password');
  await page.click('button[type="submit"]');
  
  await expect(page).toHaveURL('/dashboard');
  await expect(page.locator('h1')).toHaveText('Welcome');
});
```

## Tags
`browser`, `testing`, `automation`, `e2e`, `scraping`

## Compatibility
- Codex: ✅
- Claude Code: ✅

Overview

This skill provides cross-browser automation for testing, scraping, and web automation using a single TypeScript API that supports Chromium, Firefox, and WebKit. It focuses on reliable, fast interactions with pages, precise element selection via locators, and built-in tools for screenshots, PDFs, and HTTP API testing. It is suited for end-to-end tests, automated workflows, and robust scraping tasks.

How this skill works

The skill launches browser instances (headless or headed), creates isolated contexts and pages, and performs actions like click, fill, navigation, and waiting using stable selectors called locators. It also exposes a request API for HTTP testing, and utilities to capture screenshots, generate PDFs, and manage browser lifecycle. Scripts are written in TypeScript/JavaScript and can run locally or in CI with installed browser binaries.

When to use it

  • End-to-end testing across Chromium, Firefox, and WebKit with a single API
  • Automated form interactions, login flows, and UI regression checks
  • Web scraping where dynamic rendering and JS execution are required
  • Generating screenshots or PDFs of pages for reporting or archiving
  • API testing that needs browser context or cookie/session integration

Best practices

  • Use browser.newContext to isolate state between tests or tasks
  • Prefer locators over brittle CSS/XPath to make selectors resilient
  • Run browsers headless in CI and install required browser binaries before tests
  • Await navigation and network idle conditions when asserting page changes
  • Limit parallel browser instances to avoid resource exhaustion on CI runners

Example use cases

  • E2E test: automate login, perform actions, assert dashboard content
  • Scraper: extract product lists and structured metadata from JS-heavy pages
  • Visual regression: capture full-page screenshots before/after UI changes
  • PDF export: render invoices or reports to A4 PDFs from web templates
  • API + UI flow: create test data via API then verify it appears in the UI

FAQ

Do I need to install browsers separately?

Run the provided installer command to download browser binaries before running scripts or CI jobs.

Can Playwright run tests in parallel?

Yes. Use isolated browser contexts and the test runner to run tests in parallel while managing resource limits.

Is PDF generation supported across all browsers?

PDF generation is supported in Chromium-based browsers; other engines may not provide PDF output.