Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] Wait for response.ok() #2821

Closed
jakobrosenberg opened this issue Jul 2, 2020 · 9 comments
Closed

[Question] Wait for response.ok() #2821

jakobrosenberg opened this issue Jul 2, 2020 · 9 comments

Comments

@jakobrosenberg
Copy link

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')
}
@mxschmitt
Copy link
Member

mxschmitt commented Jul 2, 2020

(thought first, this issue was open in a jest-playwright repo, then saw its the Playwright repo itself)

From the first thoughts, it seems that this might be the right feature for you: https://github.com/playwright-community/jest-process-manager

Also supported for jest-playwright: https://github.com/playwright-community/jest-playwright#configuration

@jakobrosenberg
Copy link
Author

Thanks @mxschmitt. In my case I'm working on a new framework. Testing the CLI and dev-server is part of the tests, rather than being the environment for the tests. I hope that makes sense.

Is it possible to check if an address returns status 2xx within a given timeframe with Playwright?

@mxschmitt
Copy link
Member

Thanks @mxschmitt. In my case I'm working on a new framework. Testing the CLI and dev-server is part of the tests, rather than being the environment for the tests. I hope that makes sense.

Is it possible to check if an address returns status 2xx within a given timeframe with Playwright?

Yes, it supports http/https based applications and will start the Jest tests once a 2xx status test will be returned. Useful for dev servers like create-react-app has.

@jakobrosenberg
Copy link
Author

How would I do this with Playwright?

@mxschmitt
Copy link
Member

How would I do this with Playwright?

See this repo for jest-playwright examples including React: https://github.com/playwright-community/playwright-jest-examples

@jakobrosenberg
Copy link
Author

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.

@mxschmitt
Copy link
Member

mxschmitt commented Jul 2, 2020

Ah gotcha. If you want to use this feature directly, you can use the wait-on package.
If you want that a certain XHR/Fetch request of the page is completed, you can use the Page.waitForResponse function.

Is this enough for your needs? From my understanding integrating wait-on in your wrapper would solve this issue.

@jakobrosenberg
Copy link
Author

Thanks, didn't know about the wait-on package. The wrapper is already working, but had hoped for a cleaner solution. I'll have a look at wait-on and see if it's worth replacing the wrapper.

@aslushnikov
Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants