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]: The equivalent of cy.intercept, listening for requests, in order to use data from them #30740
Comments
Yes, you can. See this document: https://playwright.dev/docs/release-notes#response-interception You can fetch response, analyze it and then fulfill original request with it. Does this work for you? |
The example linked is a modification of the request, and in my case:
|
Can you explain then what is not working for your scenario in the following code? const responsePromise = page.waitForResponse('**/dataRequest.json');
await page.goto('/');
const response = await responsePromise;
const data = await response.json();
expect(data).toBeTruthy();
// analyze the data and interact with the page |
Assertion is unstable because I am checking a request that has quite a large body - And playwright sometimes pulls it out too “fast” as the JSON is unprocessed. |
I rewrote the function that listens for requests and noticed that the tests work a little better (the ones that wait for requests). But I have a feeling that I'm not doing it optimally. Just as I described
|
could you elaborate on that? We need a reproduction which we can run locally in order to act on issues like that. Most likely there is a race somewhere, so looking at code is essential for us. Would it be possible to share a small test, ideally with expected and actual outcome? |
Usually this error happens when you try to access a response body of a request which was from a previous navigation. Browsers like Chromium will clean them up from their memory in order to save memory and give you this error. What might work is the following: import { test, expect } from '@playwright/test';
for (let i = 0; i < 10; i++) {
test(`demo JSON + ${i}`, async ({ page }) => {
let resolveResponse = null;
const waitForSpecificResponse = new Promise(resolve => resolveResponse = resolve);
page.on('response', async response => {
if (response.url().includes('feed_content?'))
resolveResponse(await response.json());
});
await page.goto('https://dev.to/playwright');
const responseBody = await waitForSpecificResponse;
const totalCount = responseBody['result'].length;
expect(totalCount).toBeGreaterThan(0);
});
} Alternatively try to delay the second navigation if possible. |
This solution works, but requires some refactoring on the side of my tests. As if there would be a dedicated function which one handle request in expected way in playwright for this I would be happy. |
Closing as the problem has been resolved and there are no planned changes on playwright side. |
🚀 Feature Request
When rewriting the playwright tests from cypress, the biggest problem I had was the lack of cy.intercept.
I currently have a workaround in playwright but it works very un-expectantly
And example of use
I have more advanced uses of this function because I process json objects there, but the idea is the same -> intercept before the action that triggers the request -> action -> analyze data from the request.
I know about
page.route
-> but I don't know if I can use it in the context of listening and possibly returning data later onExample
*The
dataRequest.json
appears during the page loadMotivation
Easier process of rewriting automation from Cypress, more flexible automation
The text was updated successfully, but these errors were encountered: