最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Playwright JS - How to globally definechange timeout if elementselector not found? - Stack Overflow

programmeradmin1浏览0评论

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.

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.

Share Improve this question asked Jun 1, 2021 at 10:49 Mairaj AliMairaj Ali 2211 gold badge3 silver badges6 bronze badges 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. – Sushil Commented Jun 1, 2021 at 11:12
Add a comment  | 

4 Answers 4

Reset to default 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
  },
};

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

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;

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
 });
发布评论

评论列表(0)

  1. 暂无评论