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.
Let us learn about the API Testing processes and then we can perform all the above-mentioned scenarios.
The reader does not want to depend on the third-party APIs, because of which we have created a dummy API.
This API has the following features,
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
GET all: https://chercher.tech/sample/api/product/readGET Specific: https://chercher.tech/sample/api/product/read?id=90PUT data: https://chercher.tech/sample/api/product/createPOST data:https://chercher.tech/sample/api/product/updateDELETE data:https://chercher.tech/sample/api/product/delete
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.
"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 :
When we visit the desired URL, we can notice that one record with id:3789 has been created.
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 :
When we visit the desired URL, we can notice that one record with id:3789 has been updated with a new body.
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.
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 :
If we visit the URL, we can't find the records for the id:3785, because it is deleted.
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 :
When we visit the desired URL, we can notice that one record with id:3787 has been updated.