playwright vs puppeteer vs selenium-webdriver
網頁自動化測試工具
playwrightpuppeteerselenium-webdriver類似套件:

網頁自動化測試工具

網頁自動化測試工具是用於自動化瀏覽器操作的庫,幫助開發者進行功能測試、性能測試和回歸測試。這些工具可以模擬用戶行為,驗證應用程式的功能是否正常,並提高測試的效率和準確性。選擇合適的工具取決於項目的需求、技術棧和團隊的熟悉程度。

npm下載趨勢

3 年

GitHub Stars 排名

統計詳情

套件
下載數
Stars
大小
Issues
發布時間
許可
playwright156,894,39599,1385.03 MB2920 天前Apache-2.0
puppeteer15,563,36595,79243.2 kB21912 天前Apache-2.0
selenium-webdriver2,155,02034,58523.7 MB1935 天前Apache-2.0

功能比較: playwright vs puppeteer vs selenium-webdriver

瀏覽器支持

  • playwright:

    Playwright 支持 Chromium、Firefox 和 WebKit,這意味著你可以在多個瀏覽器上運行測試,確保應用在不同環境中的一致性。它還支持移動設備模擬,讓測試更全面。

  • puppeteer:

    Puppeteer 主要針對 Chromium 和 Chrome,雖然它可以與其他瀏覽器一起使用,但支持程度較低。這使得 Puppeteer 在 Chrome 環境中表現優異,但在跨瀏覽器測試方面的能力有限。

  • selenium-webdriver:

    Selenium WebDriver 支持多種瀏覽器,包括 Chrome、Firefox、Safari 和 Edge,並且可以在不同的操作系統上運行。這使得 Selenium 成為一個靈活的選擇,適合需要多平台測試的項目。

API 設計

  • playwright:

    Playwright 的 API 設計現代且直觀,支持自動等待和鏈式調用,這使得編寫測試變得簡單且易於維護。它的設計考慮了現代網頁應用的需求,提供了強大的功能,如頁面截圖和錄製。

  • puppeteer:

    Puppeteer 的 API 簡單易用,適合快速上手。它提供了許多方便的功能,如網頁截圖、PDF 生成和自動填充表單,但在某些情況下可能不如 Playwright 靈活。

  • selenium-webdriver:

    Selenium WebDriver 的 API 較為傳統,雖然功能強大,但可能需要更多的樣板代碼來設置測試。這使得它在簡單測試中可能顯得繁瑣,但在複雜場景中則提供了更大的靈活性。

學習曲線

  • playwright:

    Playwright 的學習曲線相對平緩,特別是對於已經熟悉 JavaScript 的開發者。其文檔詳細且範例豐富,能夠幫助新手快速上手。

  • puppeteer:

    Puppeteer 的學習曲線也相對平緩,因為它的 API 設計簡單明瞭,適合初學者。對於需要快速實現自動化的項目,Puppeteer 是一個理想的選擇。

  • selenium-webdriver:

    Selenium WebDriver 的學習曲線較陡,特別是對於初學者來說。由於其功能強大且靈活,開發者需要花時間理解其工作原理和最佳實踐。

性能

  • playwright:

    Playwright 在性能方面表現優異,特別是在處理多頁面和異步操作時。它的自動等待機制減少了測試失敗的可能性,從而提高了測試的穩定性。

  • puppeteer:

    Puppeteer 的性能在 Chrome 環境中非常好,但在處理大型應用或複雜操作時,可能會出現性能瓶頸。對於簡單的自動化任務,Puppeteer 的性能是足夠的。

  • selenium-webdriver:

    Selenium WebDriver 的性能在不同瀏覽器之間可能有所不同,並且在處理大型測試套件時,可能需要進行優化。使用 Selenium Grid 可以提高性能,特別是在分佈式測試環境中。

社群和支持

  • playwright:

    Playwright 擁有活躍的社群和良好的文檔支持,開發者可以輕鬆找到問題的解決方案和範例。由於其相對較新的特性,社群正在快速增長。

  • puppeteer:

    Puppeteer 的社群也非常活躍,並且由於其與 Chrome 的緊密集成,許多資源和範例可以輕鬆找到。

  • selenium-webdriver:

    Selenium WebDriver 擁有最成熟的社群和廣泛的文檔支持,許多資源和範例可供參考。作為業界標準,許多公司和開源項目都在使用 Selenium,這使得尋找支持和解決方案變得容易。

如何選擇: playwright vs puppeteer vs selenium-webdriver

  • playwright:

    選擇 Playwright 如果你需要支持多個瀏覽器(如 Chromium、Firefox 和 WebKit)以及更強大的功能,如自動等待和多頁面支持。Playwright 特別適合需要跨瀏覽器測試的現代應用程式。

  • puppeteer:

    選擇 Puppeteer 如果你的項目主要針對 Chrome 瀏覽器,並且需要簡單易用的 API 來進行網頁抓取或自動化測試。Puppeteer 提供了強大的功能來操作 Chrome,但對於其他瀏覽器的支持較弱。

  • selenium-webdriver:

    選擇 Selenium WebDriver 如果你需要一個成熟的解決方案,支持多種瀏覽器和平台,並且需要與現有的測試框架(如 JUnit 或 TestNG)集成。Selenium 是業界標準,適合大型和複雜的測試需求。

playwright 的 README

🎭 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 forInstall
Playwright TestEnd-to-end testingnpm init playwright@latest
Playwright CLICoding agents (Claude Code, Copilot)npm i -g @playwright/cli@latest
Playwright MCPAI agents and LLM-driven automationnpx @playwright/mcp@latest
Playwright LibraryBrowser automation scriptsnpm i playwright
VS Code ExtensionTest authoring and debugging in VS CodeInstall 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

LinuxmacOSWindows
Chromium1 156.0.8078.4:white_check_mark::white_check_mark::white_check_mark:
WebKit 27.2:white_check_mark::white_check_mark::white_check_mark:
Firefox 157.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