Playwright · Middle · Question 20 of 75
How do you wait for a specific API response after a click?
const responsePromise = page.waitForResponse("**/api/orders");
await page.getByRole("button", { name: "Place order" }).click();
const response = await responsePromise;- 1Start
waitForResponsebefore the click, then await it - 2Use
page.waitForTimeoutlong enough for the request - 3Click first, then call
waitForResponseon the next line
Show the answer
Correct answer: Start waitForResponse before the click, then await it
Explanation
Starting to wait before the action avoids missing a fast response. You can then assert on its status or body.