playwright vs puppeteer vs selenium-webdriver vs nightmare
ブラウザ自動化と E2E テストライブラリの比較
playwrightpuppeteerselenium-webdrivernightmare類似パッケージ:

ブラウザ自動化と E2E テストライブラリの比較

これらのライブラリは、ブラウザの操作をプログラムで制御するためのツールです。playwrightpuppeteer は現代的な API を提供し、selenium-webdriver は長年の標準であり、nightmare はかつて人気でしたが現在は维护されていません。テスト自動化やデータ収集の目的に応じて選定が必要です。

npmのダウンロードトレンド

3 年

GitHub Starsランキング

統計詳細

パッケージ
ダウンロード数
Stars
サイズ
Issues
公開日時
ライセンス
playwright34,136,74984,1733.72 MB6081ヶ月前Apache-2.0
puppeteer7,486,42993,80563 kB29210時間前Apache-2.0
selenium-webdriver1,766,95334,11717.9 MB19722日前Apache-2.0
nightmare12,71919,966-2037年前MIT

ブラウザ自動化ツールの選定:Playwright, Puppeteer, Selenium, Nightmare

現代の Web 開発において、ブラウザの操作を自動化するツールは E2E テストやスクレイピングに不可欠です。playwright, puppeteer, selenium-webdriver, nightmare はそれぞれ異なる歴史と設計思想を持っています。ここでは、実務での採用基準となる技術的な違いを深掘りします。

🛑 メンテナンス状況と推奨度

まず最も重要な点として、nightmare は公式にアーカイブ済みであり、新しいプロジェクトでの使用は推奨されません。

nightmare
Electron ベースで動作していましたが、メンテナンスが停止しています。バグ修正やセキュリティアップデートは期待できません。

// nightmare: 非推奨のため記載のみ
const Nightmare = require('nightmare');
const nightmare = Nightmare({ show: true });
// 新規プロジェクトでは使用しないでください

playwright, puppeteer, selenium-webdriver
これらは actively maintained(活発に維持)されています。特に playwrightpuppeteer は現代的な非同期処理に最適化されています。

🌐 サポートするブラウザエンジン

ツールによって対応ブラウザが異なります。これが選定の第一基準になります。

playwright
Chromium, Firefox, WebKit の 3 つのエンジンをネイティブにサポートします。Safari の動作確認も可能です。

// playwright: 複数ブラウザを同じ API で操作
const { chromium, firefox, webkit } = require('playwright');
const browser = await webkit.launch(); // Safari エンジン

puppeteer
基本的に Chrome または Chromium に特化しています。Firefox のサポートは実験的な位置づけです。

// puppeteer: Chrome 主体
const puppeteer = require('puppeteer');
const browser = await puppeteer.launch({ product: 'chrome' });

selenium-webdriver
WebDriver 標準プロトコルを使用するため、実機の Firefox, Safari, Edge などを直接制御できます。

// selenium-webdriver: 実ブラウザ駆動
const { Builder } = require('selenium-webdriver');
const driver = await new Builder().forBrowser('firefox').build();

⚡ 設定と実行の簡易さ

セットアップの手間と、コードの書きやすさは開発効率に直結します。

playwright
ブラウザバイナリを自動で管理します。追加の設定なしで headless モードで動作します。

// playwright: 設定が最小限
const browser = await chromium.launch({ headless: true });
const page = await browser.newPage();
await page.goto('https://example.com');

puppeteer
同様にブラウザバイナリを管理しますが、Chrome 依存です。設定項目は細かいです。

// puppeteer: Chrome 設定が詳細
const browser = await puppeteer.launch({ headless: 'new' });
const page = await browser.newPage();
await page.goto('https://example.com');

selenium-webdriver
別途 WebDriver バイナリ(chromedriver など)の管理が必要になる場合があります(Selenium Manager で改善されつつあります)。

// selenium-webdriver: driver 管理が必要
const driver = await new Builder().forBrowser('chrome').build();
await driver.get('https://example.com');

🔄 安定性と自動待ち機能

フラッキーなテスト(気まぐれな失敗)を減らす機能が重要です。

playwright
要素の操作前に、自動的に表示可能になるまで待機します。明示的な sleep が不要です。

// playwright: 自動待ち組み込み
await page.click('#submit-button'); // 要素がクリック可能になるまで待つ

puppeteer
基本的なナビゲーション待機はありますが、要素の状態待機は明示的に書く必要があります。

// puppeteer: 待機を明示
await page.waitForSelector('#submit-button');
await page.click('#submit-button');

selenium-webdriver
暗黙的待機(Implicit Wait)を設定できますが、動作が複雑になることがあります。

// selenium-webdriver: 待機設定
driver.manage().setTimeouts({ implicit: 10000 });
await driver.findElement(By.id('submit-button')).click();

📊 比較まとめ

特徴playwrightpuppeteerselenium-webdrivernightmare
メンテナンス✅ 活発✅ 活発✅ 活発❌ 終了
ブラウザ3 エンジンChrome 中心全ブラウザElectron
セットアップ簡単簡単やや複雑簡単
自動待機✅ 強力⚠️ 一部⚠️ 設定必要⚠️ 一部
用途E2E テストスクレイピングレガシー/多言語非推奨

💡 結論

新しいプロジェクトでは playwright が最もバランスに優れています。複数ブラウザのサポートと安定性が魅力です。puppeteer は Chrome 固有の機能や PDF 生成などに適しています。selenium-webdriver は既存の Selenium グリッド環境や、特定のブラウザドライバが必要な場合に選択します。nightmare は技術的負債となるため避けてください。

選び方: playwright vs puppeteer vs selenium-webdriver vs nightmare

  • playwright:

    複数ブラウザ(Chrome, Firefox, Safari)のサポートが必要な場合に playwright を選定します。自動待機機能が強力で、テストの安定性が高く保てます。現代の E2E テストにおける事実上の標準です。

  • puppeteer:

    Chrome または Chromium 環境に特化したい場合は puppeteer が適しています。Chrome DevTools Protocol への深いアクセスが必要な場合や、PDF 生成などの特定機能を使う場合に有利です。

  • selenium-webdriver:

    既存の Selenium インフラや、WebDriver 標準プロトコル必須の環境では selenium-webdriver を使用します。多言語サポートや実機ブラウザの細かい制御が必要なレガシーシステムに向いています。

  • nightmare:

    新しいプロジェクトでは nightmare を選択しないでください。公式にアーカイブされており、メンテナンスが停止しています。セキュリティリスクや予期せぬバグの原因となるため、既存プロジェクトからの移行を検討すべきです。

playwright のREADME

🎭 Playwright

npm version Chromium version Firefox version WebKit version Join Discord

Documentation | API reference

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API. Playwright is built to enable cross-browser web automation that is ever-green, capable, reliable, and fast.

LinuxmacOSWindows
Chromium 145.0.7632.6:white_check_mark::white_check_mark::white_check_mark:
WebKit 26.0:white_check_mark::white_check_mark::white_check_mark:
Firefox 146.0.1:white_check_mark::white_check_mark::white_check_mark:

Headless execution is supported for all browsers on all platforms. Check out system requirements for details.

Looking for Playwright for Python, .NET, or Java?

Installation

Playwright has its own test runner for end-to-end tests, we call it Playwright Test.

Using init command

The easiest way to get started with Playwright Test is to run the init command.

# Run from your project's root directory
npm init playwright@latest
# Or create a new project
npm init playwright@latest new-project

This will create a configuration file, optionally add examples, a GitHub Action workflow and a first test example.spec.ts. You can now jump directly to writing assertions section.

Manually

Add dependency and install browsers.

npm i -D @playwright/test
# install supported browsers
npx playwright install

You can optionally install only selected browsers, see install browsers for more details. Or you can install no browsers at all and use existing browser channels.

Capabilities

Resilient • No flaky tests

Auto-wait. Playwright waits for elements to be actionable prior to performing actions. It also has a rich set of introspection events. The combination of the two eliminates the need for artificial timeouts - a primary cause of flaky tests.

Web-first assertions. Playwright assertions are created specifically for the dynamic web. Checks are automatically retried until the necessary conditions are met.

Tracing. Configure test retry strategy, capture execution trace, videos and screenshots to eliminate flakes.

No trade-offs • No limits

Browsers run web content belonging to different origins in different processes. Playwright is aligned with the architecture of the modern browsers and runs tests out-of-process. This makes Playwright free of the typical in-process test runner limitations.

Multiple everything. Test scenarios that span multiple tabs, multiple origins and multiple users. Create scenarios with different contexts for different users and run them against your server, all in one test.

Trusted events. Hover elements, interact with dynamic controls and produce trusted events. Playwright uses real browser input pipeline indistinguishable from the real user.

Test frames, pierce Shadow DOM. Playwright selectors pierce shadow DOM and allow entering frames seamlessly.

Full isolation • Fast execution

Browser contexts. Playwright creates a browser context for each test. Browser context is equivalent to a brand new browser profile. This delivers full test isolation with zero overhead. Creating a new browser context only takes a handful of milliseconds.

Log in once. Save the authentication state of the context and reuse it in all the tests. This bypasses repetitive log-in operations in each test, yet delivers full isolation of independent tests.

Powerful Tooling

Codegen. Generate tests by recording your actions. Save them into any language.

Playwright inspector. Inspect page, generate selectors, step through the test execution, see click points and explore execution logs.

Trace Viewer. Capture all the information to investigate the test failure. Playwright trace contains test execution screencast, live DOM snapshots, action explorer, test source and many more.

Looking for Playwright for TypeScript, JavaScript, Python, .NET, or Java?

Examples

To learn how to run these Playwright Test examples, check out our getting started docs.

Page screenshot

This code snippet navigates to Playwright homepage and saves a screenshot.

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

test('Page Screenshot', async ({ page }) => {
  await page.goto('https://playwright.dev/');
  await page.screenshot({ path: `example.png` });
});

Mobile and geolocation

This snippet emulates Mobile Safari on a device at given geolocation, navigates to maps.google.com, performs the action and takes a screenshot.

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

test.use({
  ...devices['iPhone 13 Pro'],
  locale: 'en-US',
  geolocation: { longitude: 12.492507, latitude: 41.889938 },
  permissions: ['geolocation'],
})

test('Mobile and geolocation', async ({ page }) => {
  await page.goto('https://maps.google.com');
  await page.getByText('Your location').click();
  await page.waitForRequest(/.*preview\/pwa/);
  await page.screenshot({ path: 'colosseum-iphone.png' });
});

Evaluate in browser context

This code snippet navigates to example.com, and executes a script in the page context.

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

test('Evaluate in browser context', async ({ page }) => {
  await page.goto('https://www.example.com/');
  const dimensions = await page.evaluate(() => {
    return {
      width: document.documentElement.clientWidth,
      height: document.documentElement.clientHeight,
      deviceScaleFactor: window.devicePixelRatio
    }
  });
  console.log(dimensions);
});

Intercept network requests

This code snippet sets up request routing for a page to log all network requests.

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

test('Intercept network requests', async ({ page }) => {
  // Log and continue all network requests
  await page.route('**', route => {
    console.log(route.request().url());
    route.continue();
  });
  await page.goto('http://todomvc.com');
});

Resources