question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Question] Wait for response.ok()

See original GitHub issue

I’m not sure if this already exist. I tried waitForResponse, but didn’t get the desired result.

For my tests I need to run a dev-server, which takes up to 15 seconds to start. It would be great if there was a native way to poll a server for response.ok() to be truthy within a set interval.

As a workaround, I’m using the following code

const response = await waitForServer(page, url)

...

async function waitForServer(page, url, timeout = 20000) {
  const startTime = Date.now()
  while (Date.now() - startTime < timeout) {
    try {
      const response = await page.goto(url)
      if (response.ok())
        return response
      await new Promise(resolve => setTimeout(resolve, 100))
    } catch (err) { }
  }
  throw new Error('Server never started')
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
jakobrosenbergcommented, Jul 2, 2020

I think there might be a misunderstanding.

I’m looking for a Playwright native function like page.waitForResponse, which waits for x seconds for a 2xx response.

0reactions
aslushnikovcommented, Aug 21, 2020

For my tests I need to run a dev-server, which takes up to 15 seconds to start. It would be great if there was a native way to poll a server for response.ok() to be truthy within a set interval.

@jakobrosenberg In ideal world, server would notify clients when it’s up and running - but sometimes there’s no way to get perfect behavior…

The workaround that you use is not that bad for what it does. Alternatively, I’d consider firing HTTP requests from node.js itself since it’s way more lightweight than browser page navigation.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to wait for requests and validate responses using ...
What you need to do is first start waiting for the response and then click, so the waitForResponse() can catch the actual response...
Read more >
Wait for serial response 'OK' after each line in while loop
I'm trying to send this Serial.write command in a while loop, but I can't figure out how to wait for a response after...
Read more >
How to make JavaScript wait for a API request to return?
The await keyword marks the point where the program has to wait for the promise to return. Hence, the program waits for the...
Read more >
How to wait for an api request to return a response?
The problem with this solution is that it will repeat POST Request multiple times (maximually TIMEOUT times). Awaitlity works in a similar way....
Read more >
How to use promises - Learn web development | MDN
The response shows the 200 (OK) status code, meaning that our request ... With the fetch() API, once you get a Response object,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found