Open3

[playwright]使い方メモ

しんのすけしんのすけ

package.json

{
  "name": "playwright",
  "type": "module",
  "version": "1.0.0",
  "description": "",
  "main": "./src/index.js",
  "scripts": {
    "test:e2e": "playwright test"
  },
  "dependencies": {
    "playwright": "^1.30.0"
  },
  "devDependencies": {
    "@playwright/test": "^1.30.0"
  }
}

設定ファイル

// playwright.config.js
import { devices } from '@playwright/test';

const config = {
  testDir: 'tests/e2e',
  forbidOnly: !!process.env.CI,
  retries: process.env.CI ? 2 : 0,
  use: {
    // trueならブラウザを立ち上げずに起動する
    headless: false,
    trace: 'on-first-retry',
    httpCredentials: {
      username: 'username', // Basic認証がある場合
      password: 'password', // Basic認証がある場合
    },
    // 動作を遅くして内容を確認したいとき
    launchOptions: {
      slowMo: 1000,
    },
  },
  projects: [
    {
      name: 'chromium',
      use: { ...devices['Desktop Chrome'] },
    },
  ],
};

export default config;

テストコード

// tests/example.test.js
import { test, expect } from "@playwright/test";

test("title test", async ({ page }) => {
    await page.goto("https://example.com/");
    const title = await page.title();
    await expect(title).toBe("Example Domain");
});
しんのすけしんのすけ

ページの一番下までスクロールさせる

await page.evaluate(() => window.scrollTo(0, document.body.scrollHeight));