Wait for a network response
Trigger a request, wait for its response, then assert on both the payload and the UI.
A button starts an API call and the page updates when it returns. Wait for that specific response so you can check the status and the rendered result together.
Couple the action to the wait
When the request can fire immediately, register the wait and the action together. The PHP API accepts an action option for this race-sensitive case.
use function Playwright\Testing\expect;
$page->goto('https://app.example.test/orders');
$response = $page->waitForResponse('**/api/orders', [
'action' => 'document.querySelector("[data-testid=refresh-orders]").click()',
]);
self::assertTrue($response->ok());
expect($page->getByRole('row'))->toHaveCount(2);
The URL argument is a glob pattern, not a regular expression. **/api/orders matches the endpoint on any host.
The action string is a bridge for this PHP wait API. It is not a general recommendation to replace locator actions with DOM scripts. Use it only where the network response itself is the contract and a split click/wait can race.
Split the action and wait only when they cannot race
The split form is safe only when the response is triggered later by something independent, such as a timer that has not started yet:
$page->goto('https://app.example.test/live-orders');
$response = $page->waitForResponse('**/api/orders');
self::assertTrue($response->ok());
Do not use this order for a response triggered by a click: the response can arrive before waitForResponse() starts. Use the action option for that case. For most product tests, the final UI assertion is more important than the response assertion.
Read the payload
The response exposes the parsed body, so you can assert on data the UI does not show yet:
$response = $page->waitForResponse('**/api/orders');
$orders = $response->json();
self::assertCount(2, $orders);
Give a slow call more room
The only option besides the action is the timeout, in milliseconds:
$response = $page->waitForResponse('**/api/orders', ['timeout' => 20000]);
The sleep trap
sleep(3) guesses how long the request takes. waitForResponse() returns the instant the matching response lands, and fails clearly if it never does.
Expected result
The wait returns the matching Response object. Assert the response status when the network result matters, then assert the visible UI state that should follow from it.
Go next
- Network and API testing: interception, mocking, and traffic inspection
- Page and Browser events: passive request and response logging
- Responses: response status, headers, and payload checks