Testing Error and Timeout Scenarios

πŸ“‹ Table of Contents β–Ύ
  1. Simulating Errors and Timeouts
  2. Common Mistakes

Real users often experience network problems such as timeouts, slow responses or server errors. Testing how your application behaves under these conditions is crucial for resilience, and Playwright’s network controls make this practical.

Simulating Errors and Timeouts

By intercepting requests, you can force error responses, delays or dropped connections. This lets you verify that the UI shows appropriate messages and that retry logic works as intended.

// error-timeout-scenarios.spec.ts
import { test, expect } from '@playwright/test';

test('shows error when orders API fails', async ({ page }) => {
  await page.route('**/api/orders', async route => {
    await route.fulfill({
      status: 500,
      contentType: 'application/json',
      body: JSON.stringify({ message: 'Internal server error' }),
    });
  });

  await page.goto('https://demo.myshop.com/orders');
  await expect(page.getByText('Something went wrong loading your orders')).toBeVisible();
});
Note: You can combine setDefaultTimeout or per-assertion timeouts with delayed responses to simulate slow backends.
Tip: Maintain a small library of common failure-mode stubs (500 errors, 404s, timeouts) that you can reuse across suites.
Warning: Forgetting to remove or scope failure stubs can make later tests fail in confusing ways; keep routes local to the tests that need them.

Testing error scenarios early helps you catch missing error messages, broken retry logic, or states where the UI silently gets stuck loading forever.

Common Mistakes

Mistake 1 β€” Only testing happy-path network behaviour

This leaves resilience untested.

❌ Wrong: Assuming backends will always respond quickly and successfully.

βœ… Correct: Explicitly test how the UI reacts to slow, failing or partial responses.

Mistake 2 β€” Hard-coding unrealistic failure scenarios

This reduces value.

❌ Wrong: Simulating errors that could never occur given the system design.

βœ… Correct: Base failure scenarios on real outage reports or agreed risk models.

🧠 Test Yourself

Why simulate network failures in Playwright tests?