Tests:
GET REQUEST:
============
to validate response code:
==========================
tests["message here"]=[Link]==200;
Eg:
tests["validating response code"]=[Link]==200;
to validate response body:
==========================
For eg if we need to find "karthic" in the response body, we can write the test as,
tests["validating response body"]=[Link]("karthic");
But, if the response body has"karthic" which is present in multiple location, and
we need to validate the exact location of the word, this code will not work, in
that case we can go for javascript.
Eg:
"data":{
{
name:karthic;
"total":12;
}
{
name:karthicSeltos;
"total":13;
}
}
var response = [Link](responseBody);
tests["validating response body"] = [Link] == "karthic";
tests["validating response body"] = [Link][1].name == "karthicSeltos";
//double quotes for string
var response=[Link](responseBody);
tests["validate res body"] = [Link] == 12;
//dont need DQ for int
//add "json path finder" extension in chrome to find complex path
********************************************************************
POST REQUEST:
============
select POST req > enter URL > Body > Raw > Text> JSON
Here we need to write data which we need to POST.
Eg:
{
"name":"karthic",
"job" : "QA"
}
Now click Send.
op:
{
"name": "karthic",
"Job": "QA",
"id": "177",
"createdAt": "2022-06-26T03:46:09.128Z"
}
Now we can also validate the response by writing codes in "Tests".
Eg:
var response=[Link](responseBody);
tests["validating name"]=[Link]=="karthic";
tests["validating response code"]=[Link]==201;
Login POST request VALID USERNAME AND PASSWORD:
================================================
post:
{
"email": "[Link]@[Link]",
"password": "cityslcka"
}
op response::
{
"token": "QpwL5tke4Pnpja7X4"
}
TEsts:
tests["validating token presence"]=[Link]("token");
tests["validating response code"]=[Link]==200;
pass: validating token presence
pass: validating response code
Login POST request with INVALID USERNAME AND PASSWORD:
======================================================
post:
{
"email": "asdfghjkl@[Link]",
"password": "cityslcka"
}
op response:
{
"error": "user not found"
}
TEsts:
tests["validating token"]=[Link]("token");
tests["validating response code"]=[Link]==400;
fail: validating token | AssertionError: expected false to be truthy
pass: validating response code
***********************************************************************************
****************
PUT REQUEST:
===========
PUT:
{
"name":"karhtic",
"salary":"1500000"
}
Response:
{
"status": "success",
"data": {
"name": "karhtic",
"salary": "1500000"
},
"message": "Successfully! Record has been updated."
}
***********************************************************************************
*******
Validating headers:
======
============
[Link]("validating content type header ", function()
{
[Link]("Content-type", "application/json")
} )
***********************************************************************************
**************
DATA DRIVEN TESTING:
=====================
1. create test data in excel sheet and save it as .CSV file.
Eg:
name salary age
A 12L 25
B 13L 26
C 14L 27
D 15L 28
2. POST payload: (Hardcore the data)
{
"name": "{{name}}",
"salary": "{{salary}}",
"age" : "{{age}}"
}
3. write tests
tests["validate response code"]=[Link]==200;
4. save
5. File > New runner tab > select collection that we want to execute > enter
iteration (as per data eg: 4) > Data file upload ([Link]) > preview > Run data
driven testing
//convert .CSV file to .json by using online converter.
we can also run all requests in one collection at a single execution by "Runner
tab".
or
through COMMAND PROMPT:
======================
[Link] & Install [Link] (npm)
to check the version....> cmd : node -v
to check npm....> cmd : npm -v
[Link] newman
install directly throu cmd: .....> npm install -g newman
3. to genearte the html reporter.,
cmd....> npm install newman-reporter-html
[Link] collections and then run it throu cmd
Commands........
==============
Method 1:
cmd...> newman run [Link]
Method 2: To get the html report also means...
cmd...> newman run [Link] -r html
Method 3: Execute collection remotely...
RC collection > share > get JSON link > fetch link > copy link
cmd...> newman run [Link] -r
html