[Question] Wait for response.ok()
See original GitHub issueI’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:
- Created 3 years ago
- Comments:9 (5 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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.@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.