Using the route handler function as the last argument to .intercept(), we can do whatever we want with the entire request or response session.
This enables us to modify the outgoing request, manipulate accurate responses, or make assertions.
cy.intercept('/users*', (req) => {
//Perform Something With Request/Response
})
- Routehandler takes the coming-in HTTP request as the first argument.
Logging The Response:
cy.intercept('/public/v2/users', (req) => {
cy.log(JSON.stringify(req))
console.log(JSON.stringify(req))
})
- With the route handler we can log the response from the interception using the route handler.
Assert:
cy.intercept('?endpoint=customerchat&page_id=2123438804574660&suppress_http_code=1', (req) => {
expect(req.statusCode).should('eq',200)
})
POST/Modify And Wait For Request:
cy.intercept({
method: 'POST',
url: 'https://reqres.in/api/users',
middleware: false
},(req) => {
req.continue((res) => {
res.send({ "name":"JohnWick",
"job":"Assasin"
})
})
}).as('successfulAction')
cy.get("li[data-id='post']").click({force:true})
cy.wait(2000)
cy.xpath('//span[@class="url"]').click({force: true})
cy.wait('@successfulAction').then(($resp) => {
const acknowledgedDateTime = $resp.response.body
cy.log(acknowledgedDateTime)
})
- We can give out POST requests, wait, and print the response body in the log.
- Log in to post comments