English

Playwrightでchannelにchromeを指定すると出るエラーの直し方

​​Playwright​E2Eテスト,TypeScript,トラブルシュート

Playwright の設定で channel: 'chrome' を指定したテストが Chromium distribution 'chrome' is not found で落ちるのは、Playwright が決まった場所で Google Chrome を探し、見つけられなかったためです。Playwright がふだん使う Chromium とは違い、Google Chrome は npx playwright install だけでは入りません。Chrome を入れる(公式の手順は npx playwright install chrome)か、Chrome でなくてよいテストなら channel を外すと、エラーは消えます。Google Chrome が入っていない PC やサーバーで、Chrome を指定した E2E テストを動かしたときに出ます。

補足

検証環境は Ubuntu 24.04、Node.js 22.23.5、@playwright/test 1.63.0(Chromium 158.0.8127.36、ヘッドレス)、TypeScript 5.9.3 です。検証環境に Google Chrome は入っておらず、Chrome の配布元(dl.google.com)にも接続できなかったため、npx playwright install chrome で Chrome が入るところまでは確かめていません。「ファイルがある状態」は、Playwright の Chromium を起動するリンクで代わりにしました。Windows と macOS、chrome-beta などほかの channel、Microsoft Edge(msedge)は試していません。

エラーを起こす

設定ファイルには、公式ドキュメントの、Google Chrome と Microsoft Edge を使う方法の説明にある書き方のとおり、channel: 'chrome' を指定したプロジェクトを1つだけ書きました。

ts
// playwright.config.ts
import { defineConfig, devices } from '@playwright/test';

export default defineConfig({
  testDir: './tests',
  reporter: 'list',
  projects: [
    {
      name: 'Google Chrome',
      use: { ...devices['Desktop Chrome'], channel: 'chrome' },
    },
  ],
});

テストは、HTML を渡して見出しを確かめるだけのものです。

ts
// tests/hello.spec.ts
import { test, expect } from '@playwright/test';

test('見出しが表示される', async ({ page }) => {
  await page.setContent('<h1>猫の手カウンター</h1>');
  await expect(page.getByRole('heading', { level: 1 })).toHaveText('猫の手カウンター');
});

Google Chrome が入っていない状態で実行すると、ブラウザを起動するところで失敗しました。終了コードは1です。

text
$ npx playwright test

Running 1 test using 1 worker

  ✘  1 [Google Chrome] › tests/hello.spec.ts:3:5 › 見出しが表示される (2ms)


  1) [Google Chrome] › tests/hello.spec.ts:3:5 › 見出しが表示される ─────────────────────────────────────────

    Error: browserType.launch: Chromium distribution 'chrome' is not found at /opt/google/chrome/chrome
    Run "npx playwright install chrome"

    Error Context: test-results/hello-見出しが表示される-Google-Chrome/error-context.md

  1 failed
    [Google Chrome] › tests/hello.spec.ts:3:5 › 見出しが表示される ──────────────────────────────────────────

メッセージの意味は「Chromium 系のブラウザ chrome が /opt/google/chrome/chrome に見つからない。npx playwright install chrome を実行すること」です。テストを書かずに chromium.launch({ channel: 'chrome' }) を直接呼んだスクリプトでも、同じ2行のメッセージで止まりました。

Playwright が Chrome を探す場所

Playwright 1.63.0 のソースを見ると、channel: 'chrome' のときに起動するファイルの場所は、OS ごとに1つずつ決め打ちされています。

OS 探す場所
Linux /opt/google/chrome/chrome
macOS /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
Windows 環境変数 LOCALAPPDATA、PROGRAMFILES などが指すフォルダの下の Google\Chrome\Application\chrome.exe

Playwright は、ここにファイルがあるかどうかを確かめます(Windows では候補の場所を順に確かめます)。見つからなければ、最初の場所を添えてエラーを投げます。PATH の中の google-chrome コマンドは見ていません。検証環境でも、google-chrome コマンドが PATH にある状態で /opt/google/chrome/chrome だけを無くすと、同じエラーになりました。

channel の書き方と起動するブラウザの図。playwright.config.ts の use に channel を書かない(devices['Desktop Chrome'] だけ)と、npx playwright install で入る、Playwright が入れた Chromium を起動する。channel: 'chrome' と書くと /opt/google/chrome/chrome を見て、ファイルがあればそれを起動し、なければ起動せずに Chromium distribution 'chrome' is not found at /opt/google/chrome/chrome のエラーになる
Linux の Playwright 1.63.0 で、channel の書き方によって起動するブラウザが変わる流れ

公式ドキュメントにも、Playwright は、そのマシンに入っている Google Chrome と Microsoft Edge を使って動かせるが、既定ではそれらを入れない、と書かれています。npx playwright install を実行してあっても、入るのは Playwright 用の Chromium で、Google Chrome ではありません。

Google Chrome を入れて直す

Chrome でテストする必要があるなら、Chrome を入れます。公式ドキュメントでは、Chrome や Edge が入っていなければ Playwright のコマンドで入れられる、と案内されています。エラーのメッセージにも出ていたコマンドです。

text
$ npx playwright install chrome

Linux では、このコマンドは Playwright に同梱のシェルスクリプト(reinstall_chrome_stable_linux.sh)を実行します。1.63.0 のスクリプトの中身は、次の流れでした。

  1. Ubuntu か Debian でなければ止まる(Arm64 の Linux も止まる)
  2. google-chrome パッケージがすでに入っていれば、apt-get remove で消す
  3. apt-get update を実行する
  4. Chrome の安定版の .deb ファイルを dl.google.com から curl で取ってきて、apt-get install で入れる
  5. google-chrome --version を表示する

apt-get を使うので、管理者の権限が要ります。Playwright のソースでは、root 以外のユーザーで実行すると、sudo(なければ su)を通してスクリプトを動かすようになっていました。

検証環境で実行すると、apt-get update までは進みましたが、dl.google.com に接続できず、次のメッセージで終わりました(終了コード1。前の部分は省きました)。

text
…(前略)
curl: (56) CONNECT tunnel failed, response 403
Failed to install browsers
Error: Failed to install chrome

dl.google.com に接続できない環境では、このコマンドでは入りません。その場合は、Chrome を別の方法で入れるか、次の節のように channel を外すことになります。

注意

公式ドキュメントには、このコマンドで入れた Chrome は OS の標準の場所に入り、すでに入っている Chrome を上書きする、と書かれています。ふだん使っている PC で実行すると、その PC の Chrome が入れ直されます。

決まった場所にファイルがあれば、エラーは出ない

検証環境では Chrome を入れられませんでした。ただ、この環境の /opt/google/chrome/chrome には、Playwright の Chromium を起動するリンクが最初からありました。ここまでの検証では、これを別の名前に移して Chrome が無い状態にしていたので、元の場所に戻しました。設定ファイルは最初のまま(channel: 'chrome' のまま)で実行すると、エラーは出ずにテストが通りました。

text
$ npx playwright test

Running 1 test using 1 worker

  ✓  1 [Google Chrome] › tests/hello.spec.ts:3:5 › 見出しが表示される (374ms)

  1 passed (1.5s)

このとき browser.version() が返したのは、Chromium と同じ 158.0.8127.36 でした。Playwright のソースでも、決まった場所にファイルがあるかどうかだけを見ていて、それが本物の Google Chrome かどうかは確かめていません。エラーが消えたことで分かるのは、そこにファイルがあることまでです。

channel を外して、Playwright の Chromium で動かす

Chrome でなくてもよいテストなら、channel を外すのがいちばん手早い直し方です。

ts
// playwright.config.ts
import { defineConfig, devices } from '@playwright/test';

export default defineConfig({
  testDir: './tests',
  reporter: 'list',
  projects: [
    {
      name: 'chromium',
      use: { ...devices['Desktop Chrome'] },
    },
  ],
});

devices['Desktop Chrome'] は、画面の大きさや User-Agent を Chrome に合わせる設定で、Chrome を起動するものではありません。channel を書かなければ、npx playwright install で入れた Chromium が起動します。Chrome が入っていない状態のまま、この設定で実行すると、テストは通りました。

text
$ npx playwright test

Running 1 test using 1 worker

  ✓  1 [chromium] › tests/hello.spec.ts:3:5 › 見出しが表示される (106ms)

  1 passed (562ms)

公式ドキュメントでも、ふだんは Playwright の Chromium を使うのがよく、Chrome などの安定版を使うのは、いま配られているブラウザでの確認が求められる場合や、Chromium に入っていない動画・音声のコーデックを使う場合だと説明されています。

Chrome のプロジェクトだけが落ちるとき

chromium と Google Chrome の2つのプロジェクトを並べた設定では、Chrome のほうだけが同じエラーで落ち、chromium のテストは通りました。全体の終了コードは1になるので、CI ではテストが失敗した扱いになります。

text
$ npx playwright test

Running 2 tests using 2 workers

  ✘  2 [Google Chrome] › tests/hello.spec.ts:3:5 › 見出しが表示される (2ms)
  ✓  1 [chromium] › tests/hello.spec.ts:3:5 › 見出しが表示される (236ms)
…(中略)
  1 failed
    [Google Chrome] › tests/hello.spec.ts:3:5 › 見出しが表示される ──────────────────────────────────────────
  1 passed (740ms)

手元には Chrome がなく、CI でだけ Chrome でも確かめたい、という場合は、手元では --project で chromium だけを動かせます。この指定で実行すると、Chrome のプロジェクトは動かず、テストは通りました(終了コード0)。

text
$ npx playwright test --project=chromium

Running 1 test using 1 worker

  ✓  1 [chromium] › tests/hello.spec.ts:3:5 › 見出しが表示される (113ms)

  1 passed (663ms)

ブラウザそのものが無いときに出る、よく似たエラーについては、PlaywrightのExecutable doesn't existの直し方にまとめています。そちらは channel を書かない場合に、Playwright 用の Chromium が無いときのエラーです。

​Posted by 86