Rayrun
← Back to Discord Forum

Python: Do not wait for timeout

silentsisyphusposted in #help-playwright
Open in Discord
silentsisyphus
silentsisyphus

Right now, I have a code like this:

try:
  async with page.expect_response(lambda x: someFragment in x.url and x.status == 200, timeout=45000) as api_res:
    res = await page.goto(hotel_url, timeout=45000, wait_until='domcontentloaded')
    if res.status > 299:
      print("Page not loaded!")
      return
except:
  do_something()
  pass

With this code, even if res.status > 299, the return is not hit, and it waits for 45 seconds, because that is the timeout limit, and it goes into except body. Is there any way to not wait for the timeout and just return if res.status > 299?

This thread is trying to answer question "Is there any way to not wait for the timeout and just return if timeout is less than 45 seconds?"

0 replies

Answer

Sure, you can avoid waiting for the full timeout duration and return earlier if the timeout is less than 45 seconds. In @playwright/test, you can adjust the maximum timeout value using the setDefaultTimeout() method. This method is available in various classes like androidDevice, browserContext, and page.

Here's how you can do it:

// Set maximum timeout to 45 seconds
androidDevice.setDefaultTimeout(45000);

This sets the maximum timeout to 45 seconds (45000 milliseconds). Any subsequent operations will use this value if their specified or default timeout is shorter. They won't wait for the full 45 seconds and will return earlier.

But remember, changing the default maximum timeout value globally can impact other parts of your code that rely on Playwright's timeouts. So, think carefully before making this change.

In short, you can avoid waiting for the full timeout and return earlier if it's less than 45 seconds by adjusting the maximum timeout with setDefaultTimeout().

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.