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] Intercepting responses #1774

Closed
Insensitivity opened this issue Apr 14, 2020 · 24 comments
Closed

[Feature] Intercepting responses #1774

Insensitivity opened this issue Apr 14, 2020 · 24 comments
Assignees

Comments

@Insensitivity
Copy link

Insensitivity commented Apr 14, 2020


Note from maintainers: request interception and response mocking work in Playwright. See here for details.

Hey, I was wondering whether it is possible to intercept the response to a network request. I did notice the possibility of using routes to intercept the requests, but is it possible to get the response without re-sending the request manually? If so how? and would it work for HTTP data streams?

I've skimmed through the API docs but couldn't really find any specifics regarding intercepting the response.

@pavelfeldman
Copy link
Member

Response interception is not currently available. This is something we can add in the future - no fundamental problems with having it. Could you share more about your use case?

@hollligan
Copy link

it would be useful for mock.
puppeteer example:

// Intercept API response and pass mock data for Puppeteer
await page.setRequestInterception(true);
page.on('request', request => {
    if (request.url() === constants.API) {
        request.respond({
            content: 'application/json',
            headers: {"Access-Control-Allow-Origin": "*"},
            body: JSON.stringify(constants.biddersMock)
        });
    }
    else {
        request.continue();
    }
});

@Nisthar
Copy link

Nisthar commented Sep 27, 2020

@hollligan does that code work in puppeteer?

@hollligan
Copy link

@Nisthar I didn't check it, I took it from the puppeteer documentation. for a playwright, here's the working code -
https://playwright.dev/#version=v1.4.2&path=docs%2Fnetwork.md&q=handle-requests

@lironzizva
Copy link

It will be useful to troubleshoot scenario that pages are not fully loaded while running multiple tests in parallel in headless mode.

@emil45
Copy link

emil45 commented Nov 15, 2020

Is it still not implemented ?

@dgozman dgozman changed the title [Question] Intercepting responses [Feature] Intercepting responses Nov 23, 2020
@onno-amsterdam
Copy link

Can't you just use: page.waitForResponse? I'm able to intercept responses for XHR requests (and parse it to JSON) like so:
const response = await page.waitForResponse('**/my-endpoint');
JSON.parse(response.body().toString());

@ua2028
Copy link

ua2028 commented Jan 1, 2021

@onno-amsterdam correct me if I'm wrong but response = await page.waitForResponse will return the finalized response, while the question refers to modifying the response prior to it's consumption by the web app.

@biiishal
Copy link

This would definitely be helpful to use fixtures for repeating network requests. ⬆️ upvote from me.

@ThompsonJonM
Copy link

ThompsonJonM commented Jan 28, 2021

Playwright allows for route interception and mocking/stubbing.

const mock = { books: [] }

await page.route('**/Bookstore/v1/books', (route) => 
    route.fulfill({
        status: 304,
        body: JSON.stringify(mock),
    })),
);

await page.goto('https://www.demoqa/books');

Playwright Network Handling docs

@biiishal
Copy link

Wow thanks @ThompsonJonM , think I'm pretty much sold on choosing Playwright over Cypress now.

@idxn
Copy link

idxn commented Feb 5, 2021

Upvote for this!

@AnkitKSinha
Copy link

Happened to meet a use case for this requirement. Our app makes a network call to get user settings, and certain other information as well. We need to modify only one setting out of the many returned by this call. We don't want to mock the entire response because It'll leave the other account details (returned as part of response) to be stale, over a period of time.

So we were curious if there could be a way in which we can intercept the response and modify parts of it, before the web app consumes it.

@Altneuj
Copy link

Altneuj commented Mar 3, 2021

Any idea if this will be able to intercept responses from redirected requests? Hard blocked in my transition from puppeteer to playwright as I need to be able to intercept the request or response from a redirect.

Use case is: Hit login screen => redirected to main entry page => I need to intercept that entry bundle to change some entry points to test different branches of code

@aslushnikov aslushnikov added v1.11 and removed v1.10 labels Mar 18, 2021
@RFC2109
Copy link

RFC2109 commented Apr 13, 2021

This feature is very useful ,hope to see it soon.

@pavelfeldman pavelfeldman added v1.12 and removed v1.11 labels Apr 19, 2021
@baskeville
Copy link

I have a similar use case to @AnkitKSinha. Our use case would be to intercept that response and return only a few of the desired configs differently so we can automate different user scenarios. This would be a very useful feature for us.

@sergioariveros
Copy link

sergioariveros commented May 22, 2021

Same case as @AnkitKSinha mention, I would like to have the ability to intercept the response and just modify parts of that response

@pavelfeldman pavelfeldman added v1.13 and removed v1.12 labels Jun 1, 2021
@mindplay-dk
Copy link

Been poking through these commits - is there anything usable I can try out yet?

I need to modify responses for the project I'm working on - if this is not available yet (or anytime soon) I might go ahead with a temporary solution, e.g. launching a built-in proxy server and connecting to that, which is kind of yuck. 🙂

@yury-s
Copy link
Member

yury-s commented Sep 2, 2021

You can give it try, see these tests for examples. The API is not final yet, so expect it to change but the functionality should be there.

@aslushnikov
Copy link
Collaborator

aslushnikov commented Oct 21, 2021

Fixed with the Fetch API that'll be released in 1.16

Docs:
https://playwright.dev/docs/next/network#modify-responses

@DuyTrinh94
Copy link

Fixed with the Fetch API that'll be released in 1.16

Docs: https://playwright.dev/docs/next/network#modify-responses

Hi, Did Modify Response feature support Java yet? I wonder that because the documentation isn't available for Java now.

@ofqwx
Copy link

ofqwx commented Nov 25, 2022

Does anybody know (or can point me in the right direction) on how to mock server side requests. I'm working on a next JS app and I would like to mock getServerSideProps requests.
Thanks.

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