Mairaj Ali
Mairaj Ali

Reputation: 221

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.

Upvotes: 20

Views: 43230

Answers (4)

ruud
ruud

Reputation: 1013

It is possible too to configure the timeout for one single test. This looks like

test('nieuwe opportity met verplichte velden@nu', async ({ page }) => {
    //test.slow(); triples the default timeout
   test.setTimeout(60 * 1000);     // sets timeout to 60 seconds for this test
   await page.locator ('yourlocator').toContainText( 'yourtest');
   // more test with the same timeout
 });

Upvotes: 0

jorisw
jorisw

Reputation: 908

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

Upvotes: 7

sahmeepee
sahmeepee

Reputation: 532

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
  },
};

Upvotes: 12

kca
kca

Reputation: 6055

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;

Upvotes: 5

Related Questions