Skip to content

Playwright Request Wait Not Working Fix

DodaTech Updated 2026-06-24 3 min read

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.waitForResponse for full request/response tracking
  • Use Promise.all for concurrent waits

Common Mistakes with wait for request

  1. Misunderstanding that String is [Char] with poor performance for large text operations
  2. Using foldl instead of foldl' causing stack overflow on large lists
  3. 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

### Should I use url string or predicate for waitForRequest?

Use URL strings for simple matching. Use predicate functions (taking a Request object) when you need to check method, headers, post data, or other request properties.

Can I wait for a request that might not happen?

Use page.waitForRequest() with a timeout. If the request isn't made within the timeout, it throws. Catch the error to test scenarios where a request should NOT be made: await expect(page.waitForRequest('**/api/*', { timeout: 3000 })).rejects.toThrow().

What's the difference between waitForRequest and waitForResponse?

waitForRequest resolves when the request is sent. waitForResponse resolves when the full response is received. Use waitForResponse when you need to verify response status or body.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro