Motomichi Works Blog

モトミチワークスブログです。その日学習したことについて書いている日記みたいなものです。

PlaywrightのMock APIsでGETやPOSTなどのメソッド毎にレスポンスを分ける

参照したページ

サンプルコード

以下のように route.request().method() を呼び出すとメソッドが取得できます。

// Handle GET requests.
await page.route('**/*', async route => {
  if (route.request().method() !== 'GET') {
    await route.fallback();
    return;
  }
  // Handling GET only.
  // ...
});

// Handle POST requests.
await page.route('**/*', async route => {
  if (route.request().method() !== 'POST') {
    await route.fallback();
    return;
  }
  // Handling POST only.
  // ...
});