Closed4

Playwright導入(Next.js, TypeScript)

mimimimi

https://playwright.dev/docs/intro

install

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

config

playwright.config.tsを作る
testMatchでテストファイルを指定する

ex. chromiumとiPhone12の場合

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

const config: PlaywrightTestConfig = {
    testMatch: '/e2e/**/*.spec.ts',
    use: {
        baseURL: process.env.BASE_URL || 'http://localhost:3000',
        headless: true,
        ignoreHTTPSErrors: true,
        actionTimeout: 10_000
    },
    projects: [
        {
            name: 'chromium',
            use: {...devices['Desktop Chrome']},
        },
        {
            name: 'iphone12',
            use: {...devices["iPhone 12"]}
        }
    ]
}
export default config;

Test

npx playwright test

debug mode

npx playwright test --debug
mimimimi

.env 使いたい時

dotenvをインストール
https://www.npmjs.com/package/dotenv

/e2e/global-setup.ts を作る

import { FullConfig } from '@playwright/test';
import dotenv from 'dotenv'
import path from 'path'

dotenv.config({ path: path.resolve(process.cwd(), '.env.local') })

async function globalSetup(config: FullConfig) {
    console.log('begin global setup!')
    //   Globalな設定があれば設定する
    console.log('end global setup!')
}

export default globalSetup;

playwright.config.tsに追記

const config: PlaywrightTestConfig = {
    globalSetup: require.resolve('./e2e/global-setup.ts'),
    // 中略
}
export default config;

env.localに設定した環境変数がplaywrightで以下みたいに使える

await page.fill("#username_or_email", process.env.LOGIN_ID);
await page.fill("#password", process.env.LOGIN_PASS );

参照

https://playwright.dev/docs/test-advanced#global-setup-and-teardown
https://dev.to/kouts/getting-started-with-playwright-e2e-testing-mfd

このスクラップは2023/02/15にクローズされました