playwright README

The project's README from github.com/microsoft/playwright. As one markdown file: README.md.

🎭 Playwright

npm version Chromium version Firefox version WebKit version Join Discord

Documentation | API reference

Playwright is a framework for web automation and testing. It drives Chromium, Firefox, and WebKit with a single API — in your tests, in your scripts, and as a tool for AI agents.

Get Started

Choose the path that fits your workflow:

Best for Install
Playwright Test End-to-end testing npm init playwright@latest
Playwright CLI Coding agents (Claude Code, Copilot) npm i -g @playwright/cli@latest
Playwright MCP AI agents and LLM-driven automation npx @playwright/mcp@latest
Playwright Library Browser automation scripts npm i playwright
VS Code Extension Test authoring and debugging in VS Code Install from Marketplace

Playwright Test

Playwright Test is a full-featured test runner built for end-to-end testing. It runs tests across Chromium, Firefox, and WebKit with full browser isolation, auto-waiting, and web-first assertions.

Install

npm init playwright@latest

Or add manually:

npm i -D @playwright/test
npx playwright install

Write a test

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

test('has title', async ({ page }) => {
  await page.goto('https://playwright.dev/');
  await expect(page).toHaveTitle(/Playwright/);
});

test('get started link', async ({ page }) => {
  await page.goto('https://playwright.dev/');
  await page.getByRole('link', { name: 'Get started' }).click();
  await expect(page.getByRole('heading', { name: 'Installation' })).toBeVisible();
});

Run tests

npx playwright test

Tests run in parallel across all configured browsers, in headless mode by default. Each test gets a fresh browser context — full isolation with near-zero overhead.

Key capabilities

Auto-wait and web-first assertions. No artificial timeouts. Playwright waits for elements to be actionable, and assertions automatically retry until conditions are met.

Locators. Find elements with resilient locators that mirror how users see the page:

page.getByRole('button', { name: 'Submit' })
page.getByLabel('Email')
page.getByPlaceholder('Search...')
page.getByTestId('login-form')

Test isolation. Each test runs in its own browser context — equivalent to a fresh browser profile. Save authentication state once and reuse it across tests:

// Save state after login
await page.context().storageState({ path: 'auth.json' });

// Reuse in other tests
test.use({ storageState: 'auth.json' });

Tracing. Capture execution traces, screenshots, and videos on failure. Inspect every action, DOM snapshot, network request, and console message in the Trace Viewer:

// playwright.config.ts
export default defineConfig({
  use: {
    trace: 'on-first-retry',
  },
});
npx playwright show-trace trace.zip

Parallelism. Tests run in parallel by default across all configured browsers.

Full testing documentation


Playwright CLI

Playwright CLI is a command-line interface for browser automation designed for coding agents. It's more token-efficient than MCP — commands avoid loading large tool schemas and accessibility trees into the model context.

Install

npm install -g @playwright/cli@latest

Optionally install skills for richer agent integration:

playwright-cli install --skills

Usage

Point your coding agent at a task:

Test the "add todo" flow on https://demo.playwright.dev/todomvc using playwright-cli.
Take screenshots for all successful and failing scenarios.

Or run commands directly:

playwright-cli open https://demo.playwright.dev/todomvc/ --headed
playwright-cli type "Buy groceries"
playwright-cli press Enter
playwright-cli screenshot

Session monitoring

Use playwright-cli show to open a visual dashboard with live screencast previews of all running browser sessions. Click any session to zoom in and take remote control.

playwright-cli show

Full CLI documentation | GitHub


Playwright MCP

The Playwright MCP server gives AI agents full browser control through the Model Context Protocol. Agents interact with pages using structured accessibility snapshots — no vision models or screenshots required.

Setup

Add to your MCP client (VS Code, Cursor, Claude Desktop, Windsurf, etc.):

{
  "mcpServers": {
    "playwright": {
      "command": "npx",
      "args": ["@playwright/mcp@latest"]
    }
  }
}

One-click install for VS Code:

Install in VS Code

For Claude Code:

claude mcp add playwright npx @playwright/mcp@latest

How it works

Ask your AI assistant to interact with any web page:

Navigate to https://demo.playwright.dev/todomvc and add a few todo items.

The agent sees the page as a structured accessibility tree:

- heading "todos" [level=1]
- textbox "What needs to be done?" [ref=e5]
- listitem:
  - checkbox "Toggle Todo" [ref=e10]
  - text: "Buy groceries"

It uses element refs like e5 and e10 to click, type, and interact — deterministically and without visual ambiguity. Tools cover navigation, form filling, screenshots, network mocking, storage management, and more.

Full MCP documentation | GitHub


Playwright Library

Use playwright as a library for browser automation scripts — web scraping, PDF generation, screenshot capture, and any workflow that needs programmatic browser control without a test runner.

Install

npm i playwright

Examples

Take a screenshot:

import { chromium } from 'playwright';

const browser = await chromium.launch();
const page = await browser.newPage();
await page.goto('https://playwright.dev/');
await page.screenshot({ path: 'screenshot.png' });
await browser.close();

Generate a PDF:

import { chromium } from 'playwright';

const browser = await chromium.launch();
const page = await browser.newPage();
await page.goto('https://playwright.dev/');
await page.pdf({ path: 'page.pdf', format: 'A4' });
await browser.close();

Emulate a mobile device:

import { chromium, devices } from 'playwright';

const browser = await chromium.launch();
const context = await browser.newContext(devices['iPhone 15']);
const page = await context.newPage();
await page.goto('https://playwright.dev/');
await page.screenshot({ path: 'mobile.png' });
await browser.close();

Intercept network requests:

import { chromium } from 'playwright';

const browser = await chromium.launch();
const page = await browser.newPage();
await page.route('**/*.{png,jpg,jpeg}', route => route.abort());
await page.goto('https://playwright.dev/');
await browser.close();

Library documentation | API reference


VS Code Extension

The Playwright VS Code extension brings test running, debugging, and code generation directly into your editor.

Run and debug tests from the editor with a single click. Set breakpoints, inspect variables, and step through test execution with a live browser view.

Generate tests with CodeGen. Click "Record new" to open a browser — navigate and interact with your app while Playwright writes the test code for you.

Pick locators. Hover over any element in the browser to see the best available locator, then click to copy it to your clipboard.

Trace Viewer integration. Enable "Show Trace Viewer" in the sidebar to get a full execution trace after each test run — DOM snapshots, network requests, console logs, and screenshots at every step.

Install the extension | VS Code guide


Cross-Browser Support

Linux macOS Windows
Chromium1 154.0.8037.0 :white_check_mark: :white_check_mark: :white_check_mark:
WebKit 26.6 :white_check_mark: :white_check_mark: :white_check_mark:
Firefox 155.0 :white_check_mark: :white_check_mark: :white_check_mark:

Headless and headed execution on all platforms. 1 Uses Chrome for Testing by default.

Other Languages

Playwright is also available for Python, .NET, and Java.

Resources

playwright (microsoft/playwright)

Searching Database…

这是什么?Playwright 是一个用于网页测试和自动化的框架,通过同一套 API 驱动 Chromium、Firefox 和 WebKit。它可以用于测试套件、自动化脚本,也可以作为 AI 智能体的工具。
这是一个工具,还是有好几种不同的用法?实际上它包含五个相关的部分:Playwright Test(端到端测试运行器)、Playwright CLI(供编程智能体使用)、Playwright MCP(AI 智能体的浏览器控制)、Playwright Library(普通脚本编写),以及用于编写和调试测试的 VS Code 扩展。
我该如何开始编写端到端测试?使用 `npm init playwright@latest` 搭建项目,编写一个测试,然后运行它。默认情况下,测试以无头模式在所有已配置的浏览器中并行执行。
试用之前需要安装什么?只需要 Node.js 20 或更高版本。Chromium、Firefox 和 WebKit 的浏览器程序会通过各工具对应的 npm install 命令自动安装。
MCP 服务器是否依赖截图或视觉模型来查看页面?不依赖。智能体通过结构化的无障碍快照和元素引用(如 e5、e10)与页面交互,因此点击和输入都是确定性的,无需任何基于图像的推理。
它实际支持哪些浏览器和操作系统?Chromium、WebKit 和 Firefox 均支持 Linux、macOS 和 Windows,并且同时支持无头模式和有界面模式。
测试失败后,Trace Viewer 会显示什么?它会提供完整的执行追踪记录,包括运行期间捕获的每一个操作、DOM 快照、网络请求、控制台消息和截图,让你在失败发生之前看清到底发生了什么。
我能不手写就生成测试代码吗?可以。VS Code 扩展提供了 CodeGen 录制功能:点击“Record new”,在弹出的浏览器中与应用交互,Playwright 就会为你编写对应的测试代码。
除了测试,Playwright Library 还能做什么?它是一个普通的浏览器自动化库,适用于需要以编程方式控制浏览器、但不需要测试运行器的脚本,例如网页抓取、PDF 生成、截图、移动设备模拟和网络请求拦截。
如何监控智能体通过 CLI 运行的浏览器会话?运行 `playwright-cli show` 即可打开可视化仪表板,其中实时显示每个正在运行的会话的画面预览。点击任意会话,即可放大查看并进行远程控制。
Creator
microsoft
Size
239.6 MB
Stars
97.9K
Age
6y
Commits · 90 Days