はじめに
この記事では、E2E テストツールの Playwright の概要や基本的な操作方法について記載します。
Playwright の概要
Playwright は、Microsoft で開発が行われている E2E テストツールです。2020年にリリースされました。
複数ブラウザ・プラットフォーム・言語で利用可能などの特長があります。
他の E2E テストツールとしては、Cypress、Puppeteer などがあります。
Playwright は、2023年の後半から利用者数が伸びているようです。
インストール
次は実際に Playwright をインストールしてみます。
今回の開発環境は以下の通りです。
- Windows11
- VSCode
- Node.js 20.5.0
- yarn 1.22.19
- TypeScript 5.3.3
- React 18.2.0
- Playwright 1.42.1
上記の通り今回は yarn を利用してインストールします。
yarn create playwright
インストールの途中で以下のように E2E テストを置くフォルダや GitHub Actions に追加するかどうかの確認などがあります。
インストールが完了すると、以下のファイルが作成されます。
playwright.config.ts
src/___tests___/e2e/example.spec.ts
tests-examples/demo-todo-app.spec.ts
import { defineConfig, devices } from '@playwright/test';
/**
* Read environment variables from file.
* https://github.com/motdotla/dotenv
*/
// require('dotenv').config();
/**
* See https://playwright.dev/docs/test-configuration.
*/
export default defineConfig({
testDir: './src/__tests__/e2e',
/* Run tests in files in parallel */
fullyParallel: true,
/* Fail the build on CI if you accidentally left test.only in the source code. */
forbidOnly: !!process.env.CI,
/* Retry on CI only */
retries: process.env.CI ? 2 : 0,
/* Opt out of parallel tests on CI. */
workers: process.env.CI ? 1 : undefined,
/* Reporter to use. See https://playwright.dev/docs/test-reporters */
reporter: 'html',
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
use: {
/* Base URL to use in actions like `await page.goto('/')`. */
// baseURL: 'http://127.0.0.1:3000',
/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
trace: 'on-first-retry',
},
/* Configure projects for major browsers */
projects: [
{
name: 'chromium',
use: { ...devices['Desktop Chrome'] },
},
{
name: 'firefox',
use: { ...devices['Desktop Firefox'] },
},
{
name: 'webkit',
use: { ...devices['Desktop Safari'] },
},
/* Test against mobile viewports. */
// {
// name: 'Mobile Chrome',
// use: { ...devices['Pixel 5'] },
// },
// {
// name: 'Mobile Safari',
// use: { ...devices['iPhone 12'] },
// },
/* Test against branded browsers. */
// {
// name: 'Microsoft Edge',
// use: { ...devices['Desktop Edge'], channel: 'msedge' },
// },
// {
// name: 'Google Chrome',
// use: { ...devices['Desktop Chrome'], channel: 'chrome' },
// },
],
/* Run your local dev server before starting the tests */
// webServer: {
// command: 'npm run start',
// url: 'http://127.0.0.1:3000',
// reuseExistingServer: !process.env.CI,
// },
});
import { test, expect } from '@playwright/test';
test('has title', async ({ page }) => {
await page.goto('https://playwright.dev/');
// Expect a title "to contain" a substring.
await expect(page).toHaveTitle(/Playwright/);
});
test('get started link', async ({ page }) => {
await page.goto('https://playwright.dev/');
// Click the get started link.
await page.getByRole('link', { name: 'Get started' }).click();
// Expects page to have a heading with the name of Installation.
await expect(page.getByRole('heading', { name: 'Installation' })).toBeVisible();
});
import { defineConfig, devices } from '@playwright/test';
/**
* Read environment variables from file.
* https://github.com/motdotla/dotenv
*/
// require('dotenv').config();
/**
* See https://playwright.dev/docs/test-configuration.
*/
export default defineConfig({
testDir: './src/__tests__/e2e',
/* Run tests in files in parallel */
fullyParallel: true,
/* Fail the build on CI if you accidentally left test.only in the source code. */
forbidOnly: !!process.env.CI,
/* Retry on CI only */
retries: process.env.CI ? 2 : 0,
/* Opt out of parallel tests on CI. */
workers: process.env.CI ? 1 : undefined,
/* Reporter to use. See https://playwright.dev/docs/test-reporters */
reporter: 'html',
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
use: {
/* Base URL to use in actions like `await page.goto('/')`. */
// baseURL: 'http://127.0.0.1:3000',
/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
trace: 'on-first-retry',
},
/* Configure projects for major browsers */
projects: [
{
name: 'chromium',
use: { ...devices['Desktop Chrome'] },
},
{
name: 'firefox',
use: { ...devices['Desktop Firefox'] },
},
{
name: 'webkit',
use: { ...devices['Desktop Safari'] },
},
/* Test against mobile viewports. */
// {
// name: 'Mobile Chrome',
// use: { ...devices['Pixel 5'] },
// },
// {
// name: 'Mobile Safari',
// use: { ...devices['iPhone 12'] },
// },
/* Test against branded browsers. */
// {
// name: 'Microsoft Edge',
// use: { ...devices['Desktop Edge'], channel: 'msedge' },
// },
// {
// name: 'Google Chrome',
// use: { ...devices['Desktop Chrome'], channel: 'chrome' },
// },
],
/* Run your local dev server before starting the tests */
// webServer: {
// command: 'npm run start',
// url: 'http://127.0.0.1:3000',
// reuseExistingServer: !process.env.CI,
// },
});
また、.gitignore
へ以下の設定が追加されます。
/test-results/
/playwright-report/
/blob-report/
/playwright/.cache/
テストの実行
テストは以下のコマンドで実行できます。
npx playwright test
テストレポートの確認
テスト完了後、HTML でテストのレポートが作成されます。
以下のコマンドを実行をするとレポートを開くことができます。
npx playwright show-report
UIモードでのテスト実行
Playwright には、UIモードというテストの実行方法もあります、
以下のコマンドを実行すると、UIモードの画面が表示されます。
npx playwright test --ui
UIモードの画面では、テストファイルの一覧が表示されます。各ファイルごとのテストの実行、タグによる絞り込み、テストコードの確認など様々な希望が提供されています。
VSCode でのテスト実行
Microsoft が提供している VSCode 用の拡張機能を利用して、VSCode 上から直接テストを実行することもできます。
まとめ
この記事では、E2E テストツールの Playwright の概要や基本的な操作方法について記載しました。動作が速く、便利な機能もたくさんありました。
まだ取り上げることができていない機能もあるので、今後試していきたいです。