LoginSignup
0
0

Playwrightで実際にPOSTされる値を観測する

Last updated at Posted at 2023-12-21
import { test } from "@playwright/test";

test("実際にPOSTされたデータの中身が見たい", async ({ page }) => {

  // フォームを開く処理などを行う

  const requestPromise = page.waitForRequest(actionPath);

  // リクエストが来るのを待つ
  const requestPromise = page.waitForRequest(actionPath);

  // interceptして実際にはデータが飛ばないようにしておく
  page.route(actionPath, (route) => route.abort());

  // 登録ボタンのクリック
  await page
    .getByRole("link", { name: /^登録$/ })
    .first()
    .click();

  // リクエストを待機する
  const request = await requestPromise;

  // interceptを解除しておく
  page.unroute(actionPath);

  // requestを使ってPOSTされたデータを確認できる
  const json = request.postDataJson(); // request.postData(), request.postDataBuffer()もある
  const postData = JSON.parse(json);
  console.log(postData);

  // Assertなどを行う

});
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