Does cypress support api automation testing also?

SeleniumAutomation TestingTesting Tools

Yes Cypress supports API automating testing also. XHR is known as the XML HTTP Request. It is an API which is used as an object. Its methods pass data between a server and browser. An XHR object can obtain data from a server in the form of a Response.

Cypress can not only be used for UI automation, but can also be used to supervise the network traffic by directly acquiring the XHR objects. It is capable of mocking or stubbing a Response. An XHR information is obatined in the Network tab in the browser.

XHR Header

Response

To trigger an XHR request, the command cy.request() is used. The method cy.intercept() redirects the Responses to the matching requests.

Implementation XHR request

cy.request('https://jsonplaceholder.cypress.io/comments').as('cn')
//request aliasing with @cn
cy.get('@cn').should((response) => {
   expect(response.body).to.have.length(500)
   expect(response).to.have.property('headers')
})
raja
Published on 25-Jun-2021 13:19:57
Advertisements