Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Playwright JS - How to globally define/change timeout if element/selector not found?

Basically I want playwright to wait for each element 5 seconds if element not found.

There is a way to change timeout individually as given below:

await page.waitForSelector('h1', { timeout: 5000 });

But I want to define it globally only one time, not in each and every element.

Thank in advance.

like image 272
Mairaj Ali Avatar asked Nov 28 '25 12:11

Mairaj Ali


1 Answers

It is possible to set the timeout for every method that accepts the timeout setting using:

browserContext.setDefaultTimeout(timeout)

"browserContext" may be slightly preferable to the "page" equivalent as it's set across the whole browserContext. You can always use the page setting if you want to override your broader browserContext setting.

If you want a different timeout for navigations than other methods, perhaps when simulating slow connection speeds, you can also set:

browserContext.setDefaultNavigationTimeout(timeout)

and that will take priority for navigations.

When using the Playwright Test Runner you can set the timeout globally for whole tests by creating a playwright.config.js file.

There is documentation on the specific timeout setting here:

https://playwright.dev/docs/test-intro#use-test-hooks

but a simplified version of their example is:

// playwright.config.js
module.exports = {

  // Each test is given 30 seconds
  timeout: 30000,

  use: {
    // Configure browser and context here
  },
};
like image 87
sahmeepee Avatar answered Dec 01 '25 01:12

sahmeepee



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!