playwright icon indicating copy to clipboard operation
playwright copied to clipboard

[Feature]: Timeout Differentials

Open graffhyrum opened this issue 1 year ago • 1 comments

🚀 Feature Request

Timeout Differentials in Playwright

Description:

I would like to request a feature enhancement for the @playwright/test framework to allow for timeout differentials in addition to the existing timeout overrides.

This feature would enable users to set a default timeout for locators and then specify a 'timeout offset' for specific calls, either as a percentage or a flat increase/decrease. Use

Case:

Currently, Playwright allows setting a default timeout for locators and overriding the timeout for specific calls. However, there are scenarios where it would be beneficial to adjust the timeout relative to the default timeout rather than specifying an absolute value.

For example, if the default timeout is set to 10 seconds, a user might want to set the timeout for a specific call to be 50% of the default (i.e., 5 seconds) or add an additional 2 seconds to the default (i.e., 12 seconds).

Proposed API:

Introduce a timeoutOffset option that can be used in conjunction with the existing timeout settings. The timeoutOffset can be specified as a percentage or a flat value.

Example

Assume the locator timeout is 10 seconds.

Percentage Decrease:

await page
  .getByRole('button', { name: 'Submit' })
  .click({ timeoutOffset: '50%' });
// This would set the timeout to 50% of the default timeout (e.g., 5 seconds if the default is 10 seconds).

Percentage Increase:

await page
  .getByRole('button', { name: 'Submit' })
  .click({ timeoutOffset: '150%' });
// This would set the timeout to 150% of the default timeout (e.g., 15 seconds if the default is 10 seconds).

Flat Increase:

await page
  .getByRole('button', { name: 'Submit' })
  .click({ timeoutOffset: '+2000' });
// This would add 2000 milliseconds (2 seconds) to the default timeout (e.g., 12 seconds if the default is 10 seconds).

Flat Decrease:

await page
  .getByRole('button', { name: 'Submit' })
  .click({ timeoutOffset: '-2000' });
// This would subtract 2000 milliseconds (2 seconds) from the default timeout (e.g., 8 seconds if the default is 10 seconds).

Motivation

  • Provides more flexibility in managing timeouts.
  • Reduces the need to hard-code timeout values, making tests more maintainable.
  • Allows for dynamic adjustment of timeouts based on the default setting, which can be useful in different environments or conditions.

graffhyrum avatar Sep 30 '24 16:09 graffhyrum

I was just about to create a new feature request to be able to retrieve the default timeout and do this "manually" (ie: something like click({ timeout: config.expect.timeout+2000 }), but this API would be a cleaner solution! This feature would make it much easier to make widespread changes (eg: if we improve our runner infrastructure and no longer expect things to take as long ) or to run experiments (eg: increasing timeouts by 2sec to see if it improves overall runtime by reducing the number of retries) without having to re-calculate all the overridden timeout values individually

Dan-DeAraujo avatar Jun 16 '25 12:06 Dan-DeAraujo