API Testing
API Testing
API Testing
Get Request: Sending a request to get some data from the server is called get request.
Post Request: Sending a request to store some data into the database/server is called post
request.
Put Request: Sending a request to update the existing data in the server.
Note: To create your own API these two software must be installed.
til
Pa
g
Install: 1. Nodejs software with npm
ra
Check the software is available or not from the cmd : Nodejs: node –version and npm: npm –
version
Pa
Install : 2. Json-server.
Create an Own/Dummy REST API “ Students.Json “ file with the following data :
Parag Patil | paragpatil.rcpit@gmail.com | 7666170317 | https://topmate.io/parag_patil02
{
"students":[
{
"id": 1,
"name": "John Doe",
"age": 18,
"grade": "12th",
"subjects": [
"Math",
"Physics"
,
til
"English"
]
},
{
Pa
"id": 2,
"name": "Jane Smith",
"age": 17,
"grade": "11th",
"subjects": [
"Biology",
"Chemistry",
"History"
g
]
},
{
ra
"id": 3,
"name": "David Johnson",
"age": 16,
"grade": "10th",
"subjects": [
Pa
"Computer Science",
"Spanish",
"Art"
]
}
]
}
Step-1
til
Pa
Step-2
g
ra
Step-3
Pa
Link: http://localhost:3000/students
How to get single student data from the above URL? Task No- 2
til
How to get all students data from the above URL? Task No- 3
Pa
g
ra
Pa
Ans: Add a new collection “ Post “ then give the url. Then insert a new request in the “Request Payload “
til
Step-2 (Response Payload)
Pa
g
ra
Pa
How to update the earlier “New student data” data? Update = Put Task No- 5
Ans : Make sure to set the “Id Number” beside the link (http://localhost:3000/students/2f05)
til
How to delete any data from the request ?
Validation :
1. Response Body
2. Headers
3. Cookies
4. Status Code
5. Time
til
Pa
g
ra
Pa
Note: In JSON format, we have to represent the data in the form of KEY: Value Pair
til
String
Pa
Example
"name": "John",
Here { “name” : “ John “ }------name is included in double quotation But John included in double
quotation here because John is string.
When we input multiple inputs in one variable then we use [ ] this is called JSON Array.
Pa
Example:
"name": "John",
"age": 30,
"phone": [12345,6789]
til
Pa
Example :
{
"Firstname": "John",
"Lastname": Null,
"age": 30,
"phone": [12345,6789],
"Status": true
}
g
ra
Pa
"Student":[
{
"SID":101,
"SName":"Kamran Tusher",
"Grade":A
},
til
{
"SID":102,
"SName":"Lamisha Rahman",
"Grade":A+
Pa
},
{
"SID":103,
"SName":"Zenith Chowdhury",
"Grade":A
},
g
{
"SID":104,
"SName":"Liyana Lio",
"Grade":A
ra
}
]
}
Pa
Explanation
til
Pa
g
ra
Pa
til
Ans: I want to extract the red-marked data from the file.
Extract Procedure— JSON Path : Student[1].SName
Pa
How to extract any data from the above JSON file by using JSON path ?
Lamisha Rahman
g
Note: For complex JSON file we need to use tools to extract the data
Tools: 1)JSON Pathfinder. 2) JSON .com
ra
Example: I want get the 2nd object’s first name data JSON path Site Link : Link
Pa
til
1. Status Code:
Response Validation Pa
g
Step-1
ra
Pa
Step-2
pm.response.to.have.status(200);
});
til
Pa
g
For Multiple Status code
});
Pa
til
Pa
How to validate All the “KEY” response header is present :
pm.test("Content-Type is present", function () {
g
pm.response.to.have.header("Content-Type");
});
ra
pm.response.to.have.header("Content-Length");
});
Pa
pm.response.to.have.header("X-Powered-By");
});
pm.response.to.have.header("Access-Control-Allow-Origin");
});
pm.response.to.have.header("Access-Control-Allow-Methods");
});
pm.response.to.have.header("Access-Control-Allow-Headerss");
});
pm.response.to.have.header("Date");
til
});
pm.response.to.have.header("Connection");
Pa
});
pm.response.to.have.header("Keep-Alive");
});
til
Code is given below
});
Pa
pm.expect(pm.response.headers.get('Content-Type')).to.eql('application/json');
g
ra
Pa
const headers = [
til
{ key: "Connection", value: "keep-alive" },
Pa
];
// Loop through headers and validate each using a basic for loop
pm.expect(pm.response.headers.get(header.key)).to.eql(header.value);
g
});
}
ra
Ans:
3. Response Time validation :
How to check the response time ?
Ans: pm.test("Response time is less than 200ms", function () {
pm.expect(pm.response.responseTime).to.be.below(200);
});
til
Pa
g
ra
Validate the “Type” of the value: Validate all the “data type” of these data from every
assertion in this Response body.
How to validate the ‘Data type ‘ of the value from these assertions in the response body?
Here is the response body: (For single object in the response body)
{
til
"age": 18, //verify the data type of id
"grade": "12th", //verify the data type of id
Pa
"subjects": [ //verify the data type of subject
"Math",
"Physics",
"English"
]
}
g
CODE:
ra
pm.expect(jsonData.name).to.be.an("String");
pm.expect(jsonData.id).to.be.an("String");
pm.expect(jsonData.age).to.be.an("number");
pm.expect(jsonData.subjects).to.be.an("Array");
});
til
Pa
g
ra
Pa
jsonData.forEach((item) => {
til
pm.test(`Test the data for item with id: ${item.id}`, () => {
pm.expect(item).to.be.an("object"); // Ensure each item is an object
pm.expect(item.name).to.be.a("string"); // Validate 'name' is a string
Pa
pm.expect(item.id).to.be.a("string"); // Validate 'id' is a string
pm.expect(item.age).to.be.a("number"); // Validate 'age' is a number
til
});
Input:
Pa
g
ra
Pa
til
-(Validate a Multiple property in the Array from the response body)
CODE:
Pa
const jsonData = pm.response.json();
pm.test("Varify the multiple property in the Array ",() =>{
pm.expect(jsonData.subjects).to.have.members(["Math","Physics","English"]);
});
Input:
g
ra
Pa
til
3. Validate the Data of the fields are matched/Correct or Not :
Pa
Validate the “value “ Of the response body
How to validate the value of of every field/assertion is match or not from the response body?
Ans:
CODE:
pm.expect(jsonData.subjects[1]).to.eql("Physics");
pm.expect(jsonData.subjects[2]).to.eql("English");
});
til
Pa
g
ra
Pa
til
Pa
g
ra
Pa
pm.expect(tv4.validate(jsonData,schema)).to.be.true;
});
PostMan variables
What is a variable?
til
Ans: Variable is something which contains some data.
Why is variable need in postman?
Ans: Variable Is used to avoid the duplicate value
Where is use variable in Postman?
Scope?
Ans : where we can create the variables
Scope to set up the variables:
Pa
Ans : Variable is used in multiple level like Collection, and Environment. Request level.
g
1.Global variable ---Set the variable in global level
2.Collection variable Set the variable in collection level
ra
Step-1
Create a Global variable on the collection and save.
Step-2
til
Step-3 (Get Request)
Pa
g
ra
Pa
til
Step-5 ----(Post Request) Pa
Put this “ {{url_global}} ” variable in every collection accordingly to change the value
g
ra
Pa
til
Step-7 (Delete Request) Pa
g
ra
Pa
Step-8
til
Pa
s
Step-9
g
ra
Pa
Collection variable : Collection variable is accessible within the Collection among multiple
requests.
Step-1
til
Step-2
Pa
g
ra
Pa
Environment Variable: Accessible in all collections but we use it for a specific environment
til
Pa
Run the whole collection of Environment Variable:
Step-1
g
ra
Pa
Step-2
til
Console result
Pa
g
ra
Pa
til
Pa
Chaining of API : The response of one API becomes the request of another API is called Chaing
of API.
g
Task:
When I pass a body it will create an ‘ID’ in response to request from the body..Then this ‘ ID ‘
ra
til
Pa
Step-2
Write a test script in the “ post-response “ body.
Script
var JsonData= JSON.parse(ResponseBody);
pm.environment("id"."JsonData.id");
g
Here this particular script will set an environment and give a id for the particular body which is
given on the top.
Another Sample API from internet:
ra
Note: Most of the time , whatever API is accessing through internet, those API would have some
authorization
How to get Access token?
Sign up for Github/Google and click on anyone then get the access.
Sample API:
URL: https://gorest.co.in/
End Point
til
"gender":"male",
"email":"ckh123@gmail.com",
Pa
"status":"active"
}
Note: This Response Body is required for Post and Put requests only.
Note : Then Use the token in the Authorization section in the Collection level to cover all the
request
GorestApi_Chaining example :
g
Process:
Step-1: Create a Collection called “GoRestAPI_Chaining
ra
Step-2: Create Post , Get, Put and Delete Request along with the given Url
Step-3: Set the Token in the collection Level for authorization
Step-4 : Then Execute all the request
Pa
til
Pa
After getting the result I have updated some informations aginst the ID which is generated.
g
ra
Pa
The request will not be executed further with the same records. Below you can see the result
Image:
til
Pa
g
ra
That is why we need to change the information of the record simultaneously.But it is difficult
to change the data manually in everytime, that is why we need to make it as automated.
Pa
var username="Chowdhury"+random;
var useremail="chowdhury"+random+"@gmail.com";
Input:
til
Pa
g
ra
Secondly, same script should write in the body of the post request
Pa
Same script is written in the body of the post request here is that code
Code:
{
"name":"{{name_env}}",
"gender":"male",
"email":"{{email_env}}",
"status":"active"
til
Output:
Pa
g
ra
Pa
After execute the post request then an ID will generate . This ID will need for other request .so,
we need to extract this ID from the response .
So write this script in the Post- Request section.
Code:
til
URL: http://simple-books-api.glitch.me/
BooksAPI: Endpoint
Types of request:
Pa
Status: Check the books are available or not .
GET /Status
List of Books:
GET /books
Get a single Book:
g
GET /books/ :bookid
Task: Now we are going to perform Data Driven testing on this Particular API.
ra
Step-1: First, we need to execute a Post request to generate a Toke for Authentication through
the given link and body.
Link/Url: http://simple-books-api.glitch.me/api-clients/
Body:
Pa
Generated Token:
Step-2 : Perform Get request using
this Url :
http://simple-books-api.glitch.me And
End Point: /Status
Get Request : http://simple-books-api.glitch.me/Status
Image:
til
Pa
Step -3 : Perform get request usng the given url for getting BookList from the BookAPI.
URL: http://simple-books-api.glitch.me/books
g
ra
Pa
Step -4: Perform get request for getting single book/specific book from the BookAPI.
Url: http://simple-books-api.glitch.me/books/1
til
Swagger:
Petstore: It’s a free API .
Pa
g
Petstore support two types of response json and xml
Here will see how to validate JSON and xml response. for this, we need to access a API called
ra
Petstore.
How to access the Petstore documentations?
Url of the Petsore documentation: https://petstore.swagger.io/
User model provides the responses in the Json format.Different API request in User model
Pa
Now click on Post request, which is in the given screenshot. Then this page will appear
Now click on the Try it out to insert the data in the body.
til
Pa
After inserting the data the click on execute and get the output which is in below
g
ra
Pa
til
"username": "Kamra Hossain Chowdhury",
"firstName": "Kamran Hossain",
Pa
"lastName": "Chowdhury",
"email": "khc123@gmail.com",
"password": "string",
"phone": "01738824389",
"userStatus": 0
}'
g
Then Create a collection in the postman and import the curl
ra
Pa