Rayrun
← Back to Discord Forum

Overwrite 30 sec default timeout when using WaitFor()

VinceAlvarezposted in #help-playwright
Open in Discord
VinceAlvarez

#timeout #WaitFor I am trying to extend the wait time for an element to get hidden when using WaitFor() but it is still picking up the default 30 sec timeout.

page.locator('#myID').waitFor({state: 'hidden', timeout: 60000});

Any one who can shed some light on this concern?

This thread is trying to answer question "How can I extend the wait time for an element to get hidden when using WaitFor()?"

1 reply

Looks correct 🤔 Post the snippet?

Answer

Extending Wait Time in Playwright

In Playwright, the waitFor() method's default timeout is 30 seconds. But, you can customize this duration. Here's how you can do it.

Code Example

// Set custom timeout
const customTimeout = 60000; // 60 seconds

try {
  await page.waitFor('element', { state: 'hidden', timeout: customTimeout });
} catch (error) {
  // Handle TimeoutError
}

In this example, we set a custom timeout of 60 seconds. We then pass this value to the waitFor() method. This tells Playwright to wait for the specified element to become hidden or until the custom timeout duration is reached.

If the element becomes hidden within the specified timeframe, the execution will continue. If it takes longer, a TimeoutError will be thrown. You can handle this error based on your application's logic.

A Word of Caution

Be careful when extending the wait time. Longer durations can slow down your tests or automation scripts. It's generally better to use other signals such as network events or selectors becoming visible instead of relying solely on timeout-based waits.

For more details, refer to the official Playwright documentation at https://playwright.dev/docs/next.

Related Discord Threads

TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email luc@ray.run.