Playwright Test Timeout

Playwright Test Timeout

The Playwright Test framework provides a range of timeouts. These include the Test Timeout, Expect Timeout, Global Timeout, and several low-level timeouts such as actions, navigation, fixtures, and before and after all timeout.

In this article, we will only discuss a few essential aspects of test timeout.

Table of Contents

  1. Default Test Timeout
  2. Set Test Timeout in Config
  3. Set Test Timeout for a Single Test
  4. Set Test Timeout for Multiple Tests
  5. Get Timeout Value
  6. Importance of Timeout
  7. Video Tutorial

Default Test Timeout

The default timeout for Playwright Test is 30 seconds. That means each test in your project will wait for at least 30 seconds before throwing a timeout error. 

  1. This timeout includes time spent by beforeEach and afterEach hooks.
  2. beforeAll and afterAll hooks do not share time with any Test; they have a separate timeout.
  3. We can override this timeout at the config, single, and multiple test levels.
  4. It is important to note that even for correctly written scripts that take more than 30 seconds, you will get a timeout error, and your script will be marked as a fail.

Set Test Timeout in Config

We can change the default timeout by updating it at the config level. We can increase or decrease this timeout. For example:

export default defineConfig({
  timeout: 120000,
});  

Here test will wait for 120 seconds before throwing a timeout.

You can specify also specify it as:

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

Here, 1000 is for milliseconds, and 60 is for seconds. So it will wait for 2 minutes.

Set Test Timeout for a Single Test

You can also specify a timeout for a specific test. It will override global timeout.

test('Slow Test', async ({ page }) => {
  test.setTimeout(120000);
});

We can also use test.slow(), which will triple the default timeout; we will discuss this function in more detail later.

Set Test Timeout for Multiple Tests

If a set of test cases related to particular functionality or modules have a longer execution time, you can extend it using beforeEach hook.

test.beforeEach(async ({ page }) => {
  testInfo.setTimeout(120000);
});

You can call this beforeEach at file level scope and inside describe which group couple of test cases.

Get Timeout Value

Test

test('has title', async ({ page }, testInfo) => {
  console.log("Default timeout is:"+testInfo.timeout);
  test.setTimeout(testInfo.timeout+2000);
});  

BeforeEach

test.beforeEach(async ({ page }, testInfo) => {
  testInfo.setTimeout(testInfo.timeout + 30000);
});

Importance of Timeout

  • While increasing or decreasing timeout be very careful; decreasing timeout less than the actual script execution time will lead to unnecessary failure. 
  • Also, increasing timeout can increase the overall execution time of your script if there are failures. So it is essential to understand your application and implement a timeout strategy accordingly.
  • For example, if out of 100 scripts, 70 scripts are taking approximately 60 seconds, it is better to keep 60 as a default time. 
  • Or, if checkout modules test cases take 90 seconds, it is better to place it in before each of that spec file. 
  • You can also group test cases in describe and use timeout in before each for them.
  • If a specific test takes 120 seconds, it is better not to implement it at default or module or group level.

Video Tutorial: Playwright Test Timeout

Managed ad


Ashwin R
Ashwin possesses over 7 years of experience in the Quality Assurance industry. His expertise encompasses a broad range of technologies, including Cypress, Rest Assured, Selenium, Cucumber, JavaScript and TypeScript.