20

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.

1
  • 4
    Hi, I checkout the documentation and I found the "page.setDefaultTimeout(timeout)" method, which can overwrite the default 30 Sec timeout. checkout the documentation : playwright.dev/docs/api/class-page/…. Hope it will help you. Commented Jun 1, 2021 at 11:12

4 Answers 4

12

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
  },
};
Sign up to request clarification or add additional context in comments.

3 Comments

This doesn't appear to answer the OP's question. They are asking how to globally define the timeout for individual function calls such as goto() and waitForSelector(), not the timeout for the whole test.
Thanks @Hades I hadn't spotted that. I've updated my answer and I think it covers the specific request now.I can't see anything in the documentation (yet) about setting setDefaultTimeout in playwright.config.js, so this isn't global in the sense of "set it and forget it" but it's much better than doing it per method call.
ReferenceError: browserContext is not defined. Where to import from? Is it on page?
8

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

Comments

5

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;

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.