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

[Feature] Add page.waitForRequest() and page.waitForResponse() #2362

Closed
matthaywardwebdesign opened this issue Apr 12, 2018 · 9 comments
Closed

Comments

@matthaywardwebdesign
Copy link

matthaywardwebdesign commented Apr 12, 2018

Hey!

After using Puppeteer for a little while now in a number of different scenarios (SSR, automated testing) a common piece of functionality that often came up as needed, or at very minimum a nice to have, was the ability to wait for a specific network request to be made, or alternatively for a specific network response to come back. As a result I propose the addition of the following two functions.

page.waitForRequest( match[, options])
  • match <string> A regular expression to match against the request URL
  • options <Object> Optional parameters
    • method <string|Array> Either a single HTTP request method (GET, POST, etc) as a string or an array of values ["GET", "POST"]. Defaults to all HTTP request methods.
    • timeout <number> Maximum time to wait in milliseconds

And additionally

page.waitForResponse( match[, options])

With a similar set of options. I'd probably also additionally add support for filtering by response code.

An example

await page.waitForRequest( 'https?://example.com/(users|companies)', { method: 'POST' });

Happy to submit a PR if you all think it's a worthwhile addition!

Thanks!

@dowsanjack
Copy link

I think this would be pretty helpful

@dowsanjack
Copy link

Kindly submit a Pull request 😄

@aslushnikov
Copy link
Contributor

This looks reasonable. Feel free to submit a PR.

Let's pass RegExp object as the first argument if you want a regex, and a string if you want an exact url match.

// Wait for regex to match
const request = await page.waitForRequest(new RegExp('https?://example.com/(users|companies)'), { method: 'POST' });
// Wait for exact url
const request = await page.waitForRequest('https://example.com/');

@matthaywardwebdesign
Copy link
Author

@aslushnikov @dowsanjack Thanks! Will get to it within the next couple of days :)

@StephanBijzitter
Copy link

Agreeing with @aslushnikov, but since there's some minor differences between RegExp objects and literals, please do test both :-)

@yanivefraim
Copy link
Contributor

@matthaywardwebdesign - are you working on this? Can I help? (:

@yanivefraim
Copy link
Contributor

@aslushnikov - I started implementing this feature, and I have a questions regarding the api:
Do we want to get an options object, with timeout, for example? (or is it enough to use default timeout)

@aslushnikov
Copy link
Contributor

Do we want to get an options object, with timeout, for example?

@yanivefraim yes, options object with timeout will come handy. It will also make API consistent with other waitFor* methods.

aslushnikov pushed a commit that referenced this issue Jul 12, 2018
This patch introduces `page.waitForRequest` and `page.waitForResponse` helper methods.

Fixes #2362
@rodoabad
Copy link

rodoabad commented Aug 2, 2019

@aslushnikov it looks like the second argument isn't really made for options like the issue was requesting?

const firstResponse = await page.waitForResponse('https://example.com/resource');
const finalResponse = await page.waitForResponse(response => response.url() === 'https://example.com' && response.status() === 200);
return finalResponse.ok();

Do we need to always return return finalResponse.ok();?

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

No branches or pull requests

6 participants