Playwright Request Wait Not Working Fix
In this tutorial, you'll learn about Playwright Request Wait Not Working Fix. We cover key concepts, practical examples, and best practices.
Your test doesn't wait for an API request to complete — the assertion runs before the data arrives and the test fails.
The Problem
// WRONG — no wait for API response
test('search returns results', async ({ page }) => {
await page.goto('https://example.com');
await page.getByRole('textbox').fill('playwright');
await page.getByRole('button', { name: 'Search' }).click();
// Results might not be loaded yet
await expect(page.locator('.result')).toHaveCount(10);
// Fails if API hasn't responded yet
});
The search API request is in flight. The element count assertion runs before the response arrives and populates the results.
Step-by-Step Fix
1. Wait for specific request URL
// RIGHT — wait for request
import { test, expect } from '@playwright/test';
test('search returns results', async ({ page }) => {
await page.goto('https://example.com');
// Start waiting for request before triggering it
const requestPromise = page.waitForRequest('**/api/search*');
await page.getByRole('textbox').fill('playwright');
await page.getByRole('button', { name: 'Search' }).click();
const request = await requestPromise;
console.log('Request sent:', request.url());
await expect(page.locator('.result')).toHaveCount(10);
});
2. Wait for request with conditions
// RIGHT — conditional request wait
test('wait for POST request', async ({ page }) => {
const requestPromise = page.waitForRequest(
(request) =>
request.url().includes('/api/data') &&
request.method() === 'POST'
);
await page.getByRole('button', { name: 'Save' }).click();
const request = await requestPromise;
const postData = request.postDataJSON();
expect(postData.title).toBe('New Item');
});
3. Wait for multiple requests
// RIGHT — wait for all requests to complete
test('wait for parallel requests', async ({ page }) => {
const request1 = page.waitForRequest('**/api/users');
const request2 = page.waitForRequest('**/api/settings');
await page.goto('https://example.com/dashboard');
await Promise.all([request1, request2]);
// Both requests were sent
});
4. Combine with response waiting
// RIGHT — wait for both request and response
test('wait for request and response', async ({ page }) => {
const [request, response] = await Promise.all([
page.waitForRequest('**/api/search*'),
page.waitForResponse('**/api/search*'),
page.getByRole('button', { name: 'Search' }).click(),
]);
expect(response.status()).toBe(200);
expect(request.method()).toBe('GET');
});
Expected output:
✓ search returns results (1.8s)
✓ wait for POST request (1.2s)
✓ wait for request and response (2.0s)
Prevention Tips
- Start waiting for requests before triggering the action
- Use URL patterns with glob matching
- Use predicate functions for conditional matching
- Combine with
page.waitForResponsefor full request/response tracking - Use
Promise.allfor concurrent waits
Common Mistakes with wait for request
- Misunderstanding that
Stringis[Char]with poor performance for large text operations - Using
foldlinstead offoldl'causing stack overflow on large lists - Forgetting
deriving (Show, Eq)on custom data types needed for debugging
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