LoginSignup
0
0

Playwright の前処理と後処理

Last updated at Posted at 2023-10-06

メモレベルの記事ですが、ご容赦ください。

Playwright の前処理と後処理

Playwright でテスト実行する際の前処理 (setup) と後処理 (teardown) を実装する方法には、調べた限り以下の4つがある。

  • Hooks
  • Project Dependencies
  • Configure globalSetup and globalTeardown
  • Fixture

以下ではそれぞれの特徴を簡単にまとめる。

hooks

Project Dependencies

  • 公式ドキュメント
  • メモ
    • 全テストケースの実行前/実行後に一度だけ実行できる (Global Setup/Teardown)
  • ユースケース
    • 公式ドキュメントのAuthentication のように認証情報を持った Cookie の保存する。

Configure globalSetup and globalTeardown

  • 公式ドキュメント
  • メモ
    • playwright.config.tsglobalSetupglobalTeardown のプロパティを設定することで、全テストケースの実行前/実行後に一度だけ実行できる (Global Setup/Teardown)

Fixture

  • 公式ドキュメント
    • Fixtures の Execution order のセクションが実行順序の参考になる。
  • メモ
    • await use() の前後がそれぞれ前処理と後処理になる。

    • Fixture が test と hooks に呼び出された場合にのみに実行される。

    • Fixturestring[] などとすると test() 内で変数を追加して、後処理でその値を参照することができる。以下実装例。

      import { test, expect } from "@playwright/test";
      
      type MyTestFixture = {
        sample: string[];
      };
      
      test.describe("Teardown with the Fixture", () => {
        test.use({
          sample: async(use) => {
            const sample: string[] = [];
            await use(sample);
            console.log(sample);
            // ["foo", "bar"]
          }
        })
      
        test("push value to the fixture", async ({ sample }) => {
          sample.push("foo");
          sample.push("bar");
        });
      });
      
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0