Does cypress support api automation testing also?


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')
})

Updated on: 25-Jun-2021

201 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements