有可能用Puppeteer刺穿回应者的body



使用Puppeter,我们可以截获XHR响应并读取内容。我的问题是,是否可以将响应操作为我们想要的任何形式,并在页面呈现时使用更改后的数据。

使用Puppeteer拦截响应的示例:

page.on("response", (response) => {
});

使用Cypress,我们可以通过向传递给cy.route((的对象提供自己的数据来修改响应:

cy.server()           // enable response stubbing
cy.route({
method: 'GET',      // Route all GET requests
url: '/users/*',    // that have a URL that matches '/users/*'
response: []        // and force the response to be: []
})

《木偶师》也能做到同样的事情吗?

https://docs.cypress.io/guides/guides/network-requests.html#Routing

我在这里找到了答案:

await page.setRequestInterception(true);
page.on('request', request => {
request.respond({
status: 404,
contentType: 'text/plain',
body: "Here we can provide our own mock"
});
});

最新更新