Cypress – Get and Post

The Get and Post methods are a part of the Application Programming Interface (API) testing, which can be performed by Cypress.

Get Method

To perform a Get operation, we shall make an HTTP request with the cy.request() and pass the method Get and URL as parameters to that method.

The status code reflects, if the request has been accepted and handled correctly. The code 200(means ok) and 201(means created).

Implementation of Get

The implementation of Get method in Cypress is explained below −

describe("Get Method", function(){
   it("Scenario 2", function(){
      cy.request("GET", "https://jsonplaceholder.cypress.io/comments", {
      }).then((r) => {
         expect(r.status).to.eq(200)
         expect(r).to.have.property('headers')
         expect(r).to.have.property('duration')
      });
   })
})

Execution Results

The output is as follows −

Post Method

While using the Post method, we are actually sending information. If we have a group of entities, we can append new ones at the end, with the help of Post.

To perform a Post operation, we shall make an HTTP request with the cy.request() and pass the method Post and URL as parameters to that method.

Implementation of Post

Given below is an implementation of the Post method in Cypress −

describe("Post Method", function(){
   it("Scenario 3", function(){
      cy.request('https://jsonplaceholder.cypress.io/users?_limit=1')
      .its('body.0') // yields the first element of the returned list
      // make a new post on behalf of the user
      cy.request('POST', 'https://jsonplaceholder.cypress.io/posts', {
         title: 'Cypress',
         body: 'Automation Tool',
      })
   })
});

Execution Results

The output is given below −

Leave a Reply