API Testing
API Testing
Types of API
API
Google FaceBook
REST API Methods / http request
1. get
2. post
3. put
4. delete
http://www.google.com/
https://reqres.in/api/users?page=2
https://reqres.in/api/users/2
Day 3
Installation
Workspace : Area where we maintain files and save
https requests
Sample API:
URI
https://reqres.in/api/users?page=2
Validations
1. Status code
2. Time
3. Size data
4. Response body (json/xml)
5. Cookies
6. Headers
3 levels
200
400
500
Day 4
Step1)
NodeJS
Step2)
j-son server
1) Install NodeJS
Download link: http://nodejs.org/en/download/
Environment Variable
2) Check node and npm versions (npm comes along with node.js)
node –version
npm --version
3) Install json server
npm install –g json-server
4) json-server student1.json
tests/validations
Client--------JSON---------- Server
JSON
Key and Value pairs
"sid":101,
"sname":"John",
"grad":"A",
},
{
"sid":102,
"sname":"Kim",
"grad":"B",
},
{
"sid":103,
"sname":"Scott",
“grad”:”C”
}
]
}
JSON Object and JSON Array
JSON ARRAYS
key values pairs
key: value
"firstname": "John",
"secondname": null
"age": 30,
"phone": [12345,678976 ]
"status": true
}
students data
-------------
"students": [100,200,300]
"students": ["A","B","C"]
------------------------------------------------
"students":[
"sid" : 101,
"sname" : "John",
"grad": "A"
},
"sid": 102,
"sname": "Kim",
"grad": "B"
},
"sid": 103,
"sname": "Scott",
"grad": "C"
Day 6
Response Validations
Status Code
Headers
Cookies
Response time
Response Body
Assertion-validation
pm-library
functions
javascript
function
Arrow function
//assertion;
);
//assertion;
);
Testing Headers
Testing Cookies
Day 7
Scripts
Pre-Request Scripts
Test-Scripts
Workflow
Collection
Folder
Request
Variables
What?
Why?
Where?
Scope
1. Global
2. Collection
3. Environment
4. Local
5. Data
pm.variables.set(“url_local”,”https://reqres.in”);
Data: external files csv/text
Global Variable
pm.globals.set(“userid_global”,”2”);
Environment Variable
pm.environment.set(“userid_qa_env”,”2”);
Collection Variable
pm.collectionVariables.set(“userid_collect”,”2”);
Global Variable
pm.globals.unset(“userid_global”);
Environment Variable
pm.environment.unset(“userid_qa_env”);
Collection Variable
pm.collectionVariables.unset(“userid_collect”);
console.log(pm.globals.get(“userid_global”));
console.log(pm.environment.get(“userid_qa_env”));
console.log(pm.collectionVariables.get(“userid_collect”));
console.log(pm.variables.get(“url_local”));
Methods used
set – global, env, collection, local
“name”:”{{name}}”,
“job”:”{{job}}”
Chaining of API’s
Student API
Gorest API
6581f6970670548ec92ff62a29401bd2442f3357e26c99175ad02e
56cbe4f894
Request Body
“name”: “scott”,
“gender”: “male”,
“email”: “abc@gmail.com”,
“status”: “inactive”
Chaining Process
Request Body
{
"name": "{{name_env}}",
"gender": "male",
"email": "{{email_env}}",
"status": "inactive",
"id": ""
}
Pre-Request script
var useremail="jim"+random+"@gmail.com";
var username="jim"+random;
pm.environment.set("email_env", useremail);
pm.environment.set("name_env", username);
To know/print values
console.log(useremail);
console.log(username);
{{URL}}/public/v2/users/{{UserID}}
Tests Tab
{{URL}}/public/v2/users/{{UserID}}
Request Body
{
"name": "{{name_env}}",
"gender": "male",
"email": "{{email_env}}",
"status": "active",
"id": ""
}
Pre-Request script
var useremail="jim"+random+"@gmail.com";
var username="jim"+random;
pm.environment.set("email_env", useremail);
pm.environment.set("name_env", username);
Delete Request
pm.environment.unset(“userid”);
pm.environment.unset(“email_env”);
pm.environment.unset(“name_env”);
Next (Parameterization)
Books API
This api allows you to reserve a book
API Authentication
POST /api-clients/
The request body needs to be in json format and include the following properties
clientName – String
clientEmail – String
Example
“clientName”: “Postman”,
“clientEmail”: “valentin@example.com”
The response body will contain access token. The access token is valid for 7 days.
Access Token:
“7ecf5a2fb972a29fffde48649f672812931732b49d68eb2080c65ae65fafaf19”
Possible errors
Status code 409 – “API client already registered.” Try changing the values for
clientName and clientEmail to something else.
Endpoints
GET /status
Returns the status of the API.
List of books
GET /books
type: fiction/non-fiction
limit: a number between 1 and 20
Submit an order
POST /orders
The request body needs to be in JSON format and include the following
properties.
Example
POST /orders/
“customerName”: “John”
}
GET /orders
Get an order
GET / orders/”orderId”
Update an order
The request body needs to be in JSON format and allows you to update the
following properties.
customerName – String
Example
PATCH /orders/PF6MF1PDcuhwobZcgm3y5
{
“customerName”: “John”
Delete an oder
DELETE /orders/ :ordered
Example
DELETE /orders/PF6MF1PDcuhwobZcgm3y5
URL: https://simple-books-api.glitch.me/orders
Request Body
{
"bookId":"{{BookId}}",
"customerName":"{{CustomerName}}"
}
Tests
BookI
D CustomerName
1 John
1 Kim
3 Scott
4 David
6 Mary
URL: https://simple-books-api.glitch.me/orders/{{orderid_env}}
Tests
URL: https://simple-books-api.glitch.me/orders/{{orderid_env}}
Tests
Authorizations
Client ID: 08886c538486fbb8212f
Swagger documents
https://fakerestapi.azurewebsites.net/index.html
https://petstore.swagger.io/
https://httpbin.org/#/