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

[Docs]: Update docs on waitForResponse to include request method #30731

Closed
mkendallmcintosh opened this issue May 9, 2024 · 0 comments · Fixed by #30739
Closed

[Docs]: Update docs on waitForResponse to include request method #30731

mkendallmcintosh opened this issue May 9, 2024 · 0 comments · Fixed by #30739

Comments

@mkendallmcintosh
Copy link

mkendallmcintosh commented May 9, 2024

Page(s)

https://playwright.dev/docs/api/class-page#page-wait-for-response

Description

I never knew if it was waiting for the correct response, so I added code to ensure that it was receiving the correct response. This was not included anywhere in the documentation nor was it in the community. I'm hoping this will save some other people a lot of time.

I included the code on the page that needs updating, but my addition is one comment, and the code

response.request().method() === 'GET | POST | PUT | etc.' &&

which is in the waitForResponse() function.

Enjoy!

// Start waiting for response before clicking. Note no await.
const responsePromise = page.waitForResponse('https://example.com/resource');
await page.getByText('trigger response').click();
const response = await responsePromise;

// Alternative way with a predicate. Note no await.
// Specify the expected request method: GET, HEAD, POST, PUT, DELETE, CONNECT, OPTIONS, TRACE, PATCH, or custom method
const responsePromise = page.waitForResponse(response =>
  response.request().method() === 'GET | POST | PUT | etc.' && response.url() === 'https://example.com' && response.status() === 200
);
await page.getByText('trigger response').click();
const response = await responsePromise;
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

Successfully merging a pull request may close this issue.

1 participant