🔭 Want to automate and scale your Playwright end-to-end tests?
Head over to Better Stack and start monitoring in 5 minutes.
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:
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!
Head over to Better Stack and start monitoring in 5 minutes.
Checking or asserting on the value of any element is straightforward in Playwright
Retrieving the current page's URL is straightforward in Playwright using `page.url()`
Retrieving attributes on web page elements is really straightforward through the `getAttribute()` method
Manually pausing Playwright tests is only recommended when troubleshooting a test. Here's how to do with with the `waitForTimeout()` method
Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.
Write for usWrite a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.
community@betterstack.comor submit a pull request and help us build better products for everyone.
See the full list of amazing projects on github