How do I set and validate timeouts for each test in an end-to-end suite using Playwright Test?


Sure, you can validate the elapsed timeout for each test in your e2e suite with @playwright/test. It offers configurable timeouts for different tasks, including test timeouts. By default, the test timeout is 30 seconds.

You can use the test.setTimeout() function to set a custom timeout for a single test or modify the timeout within hooks like beforeAll and afterAll.

Here's how you can extend the timeout for all tests running within a specific hook by 30 seconds:

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

test.beforeEach(async ({}, testInfo) => {
  // Extend timeout for all tests running this hook by 30 seconds.
  testInfo.setTimeout(testInfo.timeout + 30000);

And here's how you can change the timeout specifically for a beforeAll or afterAll hook:

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

test.beforeAll(async () => {
  // Set custom timeout for this beforeAll hook.

Remember, assertion timeouts are separate from overall e2e suite timeouts. Assertion timeouts are related to web-first assertions like expect(locator).toHaveText(), which have their own default value of 5 seconds but can be customized in the Playwright Test configuration file (playwright.config.ts).

To set a global timeout for the entire test run, you can define it in the playwright.config.ts file using defineConfig. For example, you can set a global timeout of one hour (3600 seconds) as follows:

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

export default defineConfig({
  timeout: 3600000, // One hour in milliseconds

By modifying these timeouts and asserting their validity using appropriate conditions or assertions provided by your testing framework (such as Jest's expect), you can ensure that each individual e2e test adheres to its specified timeout duration.


Thank you!
Was this helpful?
Still have questions?

If you still have questions, please ask a question and I will try to answer it.

Related Discord Threads

Related Questions

AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email luc@ray.run.