How to Change Timeout Settings in Playwright

Better Stack Team
Updated on February 25, 2024

Playwright offers a variety of timeout configurations for different operations. The standard timeout for each test is set to 30 seconds, and for each assertion, it's 5 seconds.

Encountering errors like the ones below could indicate that your timeout settings may require adjustments:

 
example.spec.ts:3:1 › sample test ===========================

Timeout of 30000ms exceeded.
 
example.spec.ts:3:1 › sample test ===========================

Error: expect(received).toHaveText(expected)

Expected string: "my text"
Received string: ""
Call log:
  - expect.toHaveText with timeout 5000ms
  - waiting for "locator('button')"

To alter the default global timeout for all tests, do the following:

playwright.config.js
import { defineConfig } from '@playwright/test';

export default defineConfig({
  timeout: 60 * 1000, // 60 seconds
});

Similarly, to modify the default global timeout for expect assertions, use this approach:

 
import { defineConfig } from '@playwright/test';

export default defineConfig({
  expect: {
    timeout: 10 * 1000, // 10 seconds
  },
});

For adjusting the timeout of a single test, apply the following methods:

 
import { test, expect } from '@playwright/test';

test('slow test', async ({ page }) => {
  // Triples the test timeout (90 seconds by default)
  test.slow();
});

test('really slow test', async ({ page }) => {
  // set timeout to 120 seconds
  test.setTimeout(120 * 1000);
});

Likewise, you can change the timeout for a specific assertion like this:

 
import { test, expect } from '@playwright/test';

test('basic test', async ({ page }) => {
  await expect(page.getByRole('button')).toHaveText('Sign in', {
    timeout: 10000,
  });
});

Playwright also allows for customization of timeout settings for actions, navigation, test hooks, and the overall test suite. Comprehensive details can be found in the test timeout documentation.

Thanks for reading, and happy coding!

🔭 Want to automate and scale your Playwright end-to-end tests?

Head over to Better Stack and start monitoring in 5 minutes.