API testing in CypressIO

API testing is not a big deal, as everyone says you have to test the URL and check the Response body and response status code. Testing a URL is the first part of API Testing.

API is tested in multiple stages, but all these stages are solely dependent on your team and organization.

  1. Hit the API URL for the given data (unique id or something)
  2. Check the database for the same data with that unique id.
  3. Navigate to the UI of the API and verify the details of the product
  4. Hit the API URL with required Operation; Operations could be Post, Put, Delete
  5. Hit the GET URL of the API and verify the changes
  6. Test the database, whether API operation is successful, and data is changed or removed.
  7. Navigate to the UI of the API and verify the product details

Let us learn about the API Testing processes and then we can perform all the above-mentioned scenarios.

Our API URL for API Testing

The reader does not want to depend on the third-party APIs, because of which we have created a dummy API.
my-api-design

This API has the following features,

  • This API is built using PHP and angular with react.
  • You can create products using this API
  • You can delete the created Product or existing Products.
  • You can edit new/existing products.
  • This API is not behind authentication.
  • You can view all the products or specific products based on id, name, price, but you cannot mix them.
  • You are not restricted to perform any operation on this API, and it doesn't restrict sharing or performing changes
  • This API has a UI at https://chercher.tech/sample/api-ui

The UI and the API both point to the same place in the Database, and the only change is how these calls the target.

Below are few Endpoints of our API, these endpoints change from product to product, and sometimes some products will have only one endpoint for all the URLs

The status codes indicate that our request was received and processed correctly. The most common success codes are 200-OK, 201-Created, and 204-No Content.

Commands in cypress

GET method in API with CypressIO

The GET method is used to extract information from the given server using a given URL. While using the GET request, it should only extract data and should have no other effect on the data.

Example program :

describe("API testing", ()=>{
it("test GET method",()=>{
cy.request("https://chercher.tech/sample/api/product/read?id=90")
     .its("body")
     .should("have.property","id");
})
})

[OR]

it("test GET", () => {
  cy.request("GET", "https://chercher.tech/sample/api/product/read?id=90", {
  }).then((response) => {
    // response.body is automatically serialized into JSON
    cy.log(response.body);
  });
});

Output: Both outputs fetch the same data. The usage of the method alone differs.
get-output-cypressio

Fixtures in cypress

POST method in CypressIO

"POST" means "Send"; if you have a collection of entities and if you add a new one onto its end; you have posted to the collection.

Example program :

describe("API testing", () => {
  it("test post", () => {
    cy.request({
      method: "POST",
      url: "https://chercher.tech/sample/api/product/update",
      body: {
        name: "cherchertech",
        description: "postmethod",
        price: "200000",
      },
    });
  });
});

[OR] We can use either of the example programs, however, the output is the same in both cases.

it("test post", () => {
  cy.request("POST", "https://chercher.tech/sample/api/product/update", {
    name: "cherchertech",
    description: "postmethod",
    price: "200000",
  }).then((response) => {
    // response.body is automatically serialized into JSON
    cy.log(response.body);
  });
});

Output :
post-output-cypress

When we visit the desired URL, we can notice that one record with id:3789 has been created.
post-method-output-cypressio

Automation Framework in CypressIO

PUT method in CypressIO

The PUTmethod is used to update the data in the database. ID or primary key column is passed to identify the target details.

Example program :

describe("API testing", () => {
  it("test PUT", () => {
    cy.request({
      method: "PUT",
      url: "https://chercher.tech/sample/api/product/update",
      body: {
        id:"3789",
        name: "cherchertech",
        description: "putmethod",
        price: "200000",
      },
    });
  });
});

[OR] We can use either of the example programs, however, the output is the same in both cases.

it("test put", () => {
  cy.request("PUT", "https://chercher.tech/sample/api/product/update", {
    id: "3789",
    name: "cherchertech12",
    description: "putmethod1",
    price: "4000",
  }).then((response) => {
    // response.body is automatically serialized into JSON
    cy.log(response.body);
  });
});

Output :
put-output-cypress

When we visit the desired URL, we can notice that one record with id:3789 has been updated with a new body.
put-output-cypressio

It seems like the PUT method and the POST method does the same function. But there is a difference. Calling the PUT method repeatedly does not affect the database. But when aPOST method is called repeatedly there is a possibility of creating data multiple times in the resource. So it is advised to use the POST method only once.

Page object models in CypressIO

DELETE method in CypressIO

The DELETE method Removes data from the target resource/ database given by a URL.

it("test delete", () => {
  cy.request("DELETE", "https://chercher.tech/sample/api/product/delete", {
    id: "3785",
  }).then((response) => {
    // response.body is automatically serialized into JSON
    cy.log(response.body);
  });
});

[OR]We can use either of the example programs, however, the output is the same in both cases.

describe("API testing", () => {
  it("test DELETE", () => {
    cy.request({
      method: "DELETE",
      url: "https://chercher.tech/sample/api/product/delete",
      body: {
        id: "3785",
      },
    });
  });
});

Output :

delete-output-cypressio

If we visit the URL, we can't find the records for the id:3785, because it is deleted.

Hooks and Tags in CypressIO

PATCH method in CypressIO

PATCH is used to update an existing entity with new information partially. You can’t patch an entity that doesn’t exist. You would use this when you have a simple update to perform, e.g., changing a user’s name.

The main difference between the PUT method and the PATCH method is when we want to use the PUT method, the whole format of the data should be passed. But in the PATCH method, the parameters which we wish to change alone can be passed.

Example program :

it("test PATCH", () => {
  cy.request("PATCH", "https://chercher.tech/sample/api/product/update", {
    id: "3787",
    description: "PATCHmethod",
  }).then((response) => {
    // response.body is automatically serialized into JSON
    cy.log(response.body);
  });
});

[OR]We can use either of the example programs, however, the output is the same in both cases.

describe("API testing", () => {
  it("test PATCH", () => {
    cy.request({
      method: "PATCH",
      url: "https://chercher.tech/sample/api/product/update",
      body: {
     id: "3787",
     description: "PATCHmethod",
      },
    });
  });
});

Output :

patch-api-cypressio

When we visit the desired URL, we can notice that one record with id:3787 has been updated.
patch-method-cypressio

0 results
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions