Playwright Response Wait Not Working Fix
In this tutorial, you'll learn about Playwright Response Wait Not Working Fix. We cover key concepts, practical examples, and best practices.
Your test proceeds before the API response arrives — the UI updates after the response, but your assertions run too early.
The Problem
// WRONG — no wait for response
test('loads user data', async ({ page }) => {
await page.goto('https://example.com/profile');
// Data is loading via API
await expect(page.locator('.user-name')).toHaveText('Alice');
// May fail if API response hasn't arrived
});
The API request for user data is still in flight. The element either shows a loading state or doesn't exist yet.
Step-by-Step Fix
1. Wait for response before asserting
// RIGHT — wait for specific API response
import { test, expect } from '@playwright/test';
test('loads user data', async ({ page }) => {
// Start waiting before navigation
const responsePromise = page.waitForResponse('**/api/user*');
await page.goto('https://example.com/profile');
const response = await responsePromise;
expect(response.status()).toBe(200);
// Now data is loaded, assert on UI
await expect(page.locator('.user-name')).toHaveText('Alice');
});
2. Check response body
// RIGHT — validate response data
test('verify response data', async ({ page }) => {
const responsePromise = page.waitForResponse(
(response) =>
response.url().includes('/api/users') &&
response.request().method() === 'GET'
);
await page.goto('https://example.com/users');
const response = await responsePromise;
const data = await response.json();
expect(data).toHaveLength(10);
expect(data[0]).toHaveProperty('name');
});
3. Wait for multiple responses
// RIGHT — parallel response waiting
test('wait for parallel data loads', async ({ page }) => {
const [usersResp, settingsResp] = await Promise.all([
page.waitForResponse('**/api/users'),
page.waitForResponse('**/api/settings'),
page.goto('https://example.com/dashboard'),
]);
expect(usersResp.status()).toBe(200);
expect(settingsResp.status()).toBe(200);
});
4. Handle failed responses
// RIGHT — test error handling
test('handles API error', async ({ page }) => {
const responsePromise = page.waitForResponse('**/api/data');
await page.goto('https://example.com');
const response = await responsePromise;
if (!response.ok()) {
await expect(page.locator('.error-message')).toBeVisible();
}
});
Expected output:
✓ loads user data (1.5s)
✓ verify response data (1.3s)
✓ handles API error (1.0s)
Prevention Tips
- Start
waitForResponsebefore the action that triggers the request - Use URL glob patterns for simple matching
- Use predicate functions for complex conditions
- Check response status to validate success
- Use
Promise.allfor concurrent request/response waits
Common Mistakes with wait for response
- Forgetting
deriving (Show, Eq)on custom data types needed for debugging - Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
- Using
headandtailinstead of pattern matching, causing runtime errors on empty lists
These mistakes appear frequently in real-world PLAYWRIGHT code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro