'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.
Solution 1:[1]
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
},
};
Solution 2:[2]
per-page timeouts:
You can set these "per page" default timeouts. This is not a global setting, but at least you can set it "per page" and won't have to set it in every method call:
page.setDefaultTimeout( timeout )
:
This setting will change the default maximum time for all the methods accepting timeout option
page.setDefaultNavigationTimeout( timeout )
:
This setting will change the default maximum navigation time for the following methods and related shortcuts:
page.goBack([options])
page.goForward([options])
page.goto(url[, options])
page.reload([options])
page.setContent(html[, options])
page.waitForNavigation([options])
page.waitForURL(url[, options])
global timeouts:
In the playwright.config.ts
file:
(remove Typescript syntax if you use plain Javascript)
import { PlaywrightTestConfig } from '@playwright/test';
const config: PlaywrightTestConfig = {
globalTimeout: 60000, // Maximum time the whole test suite can run,
timeout: 5000, // Timeout for each test
};
export default config;
Solution 3:[3]
According to the docs, you can set another timeout specifically for expect
:
const config: PlaywrightTestConfig = {
// General timeout per test
timeout: 60000,
// For browser actions
use: {
actionTimeout: 20000,
},
// For expect calls
expect: {
timeout: 10000, // <---------
},
}
https://playwright.dev/docs/test-assertions#locator-assertions-to-be-visible
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | |
Solution 3 |