robotframework-browser icon indicating copy to clipboard operation
robotframework-browser copied to clipboard

Intercepting and modifying responses (playwright page.route)

Open JanDubcak opened this issue 4 years ago • 4 comments

Modifying responses As a tester I need to intercept and modify response to a request, so that I can mock various error states handling on UI.

(eg.: How will application frontend handle error state 500? - it should show specific message to user, but in fully working application it is not easy to cause backend to fail)

Solution: New Page Route keyword Based on playwright documentation it should be possible to intercept and modify HTTP and HTTPS requests/responses.

Playwright-python: page.route : Documentation

Playwright page.route for this.

This would be of great value for testing.

Thanks a lot for considering this, regards, Jan.

JanDubcak avatar Nov 27 '20 10:11 JanDubcak

Thank you for the issue! I believe currently you can achieve this only by extending our library with a JS module. https://marketsquare.github.io/robotframework-browser/Browser.html#Extending%20Browser%20library%20with%20a%20JavaScript%20module

async function error500route(page, args) {
  await page.route(args[0], route => {
  route.fulfill({
    status: 500,
    contentType: 'text/plain',
    body: 'Internal Server Error'
  });
});
}
exports.__esModule = true;
exports.error500route = error500route;

And using it in RF when you have an open page

*** Test Cases ***
Failing server
   New Page   someurl
   error500route   yoururlpattern
   Do actions to make a call to route

mkorpela avatar Nov 27 '20 13:11 mkorpela

Thanks for the response and suggestion, I will try this next sprint :).

JanDubcak avatar Dec 08 '20 08:12 JanDubcak

@mkorpela Is there any plan to integrate this to built in feature? I would like to intercept request and response with recorded HAR file on staging serve.

aquadrehz avatar Aug 22 '22 10:08 aquadrehz

Just to raise up a discussion regarding this feature - https://www.scrapingbee.com/webscraping-questions/playwright/how-to-block-resources-in-playwright/ has python example of this. Dunno if this approach would work with browserlibrary thought "for reasons" ..

rasjani avatar May 20 '24 12:05 rasjani