Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
9 views

API concepts.

Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

API concepts.

Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

* What is API?

API is a way of communication between two application where application may differ
in their platforms or in terms of technology.

e.g. API-restaurant analogy

* Types of API?

1. Simple object Access Protocol(SOAP)


2. Representational State Transfer(REST)

* API VS Webservice
--------------------
after the completion of development and testing process of APIs
when the API is moved to production environment (on internet for end user) then it
is as web service.(required internet).
All webservices are APIs but all APIs are not webservices.

* Rest API methods/http request


get--- retrieve the resource from database
post--- create new resource on database
put--- update existing resource on database
delete--- delete existing resource from database
patch--- update partial details of resource.

* Sample API:

https://reqres.in/api/users?page=2 URI/URL

https:// protocol
reqres.in host or domain
/api/users? Page=2 endpoints
/api/user? path parameter
page=2 query parameter
page key
2 value

*creating our own demo APIs


1. install node js
node --version
npm --version

2. json-server
npm install -g json-server

3. create .json file & run this json file by giving command
C:\Users\shubh\Downloads>json-server Student.Json
--------------------------
now your json file is running & you can run the apis

** Test and validation

JSON---Java Script Object notation

represented in form of key: value


key is always represented in " "

datatype is valid only for value

json array [ , ]

e.g.
{
"name": "john"' -string
"last name": null -null
"age": 30, -int
"mob no's": [78548,7888458] -array
"status": true -Boolean
} --json object

* Student data
---------------

{
"studens":[
{
"S_id": 101,
"S_name": "shubham",
"grad":"A"
},

{
"S_id": 102,
"S_name": "kartik",
"grad":"B"
},

{
"S_id": 103,
"S_name": "keshav",
"grad":"C"
}
]
}

*json path
----------

{
"students":[
{
"S_id": 101, -->students[0].S_id
"S_name": "shubham", -->students[0].S_name
"grad":"A"
},

{
"S_id": 102, -->students[1].S_id
"S_name": "kartik", -->students[1].S_name
"grad":"B"
},

{
"S_id": 103, -->students[2].S_id
"S_name": "keshav", -->student[2].S_name
"grad":"C"
}
]
}

* Capture & validate Json path

http://jsonpathfinder.com
http://jsonpath.com

*Responce validation

status code
headers
cookies
Response time
response body

* in postman we have pm-library which have certain no of functions/assertions.

*we have 2 function in chai assertion library

normal function
------------------
pm.test("test name", function()
{
//assertion;
}
);

arrow function
----------------
pm.test("test name", () =>
{
//assertion;
}

);

# Pre-request scripts

pre-request scripts--->Request-->Response-->post-request scripts

collection level
folder level
request level

*variables
what ?
why?
where?

scop of variable
-----------------
workspace-->collection-->request level

global - accessible in workspace


collection - accessible within collection
environment - accessible within particular environment
local - accessible within in request level
data - CREATE THIS IN EXTERNAL FILES CSV/TEXT.../Data driven testing

{{ VARIABLE }}

#Chaining of APIs

-to capture the response body into variable we use the library
JSON.parse(responebody); & store it into any variable
var jsondata=JSON.parse(responeBody);

-to extract particular data from response we use json path


e.g. jsondata.id, jsondata.name..

*API for testing

gorest.co.in
-------------

URL: https://gorest.co.in
endpoints:

POST /public/v2/users Create a new user


GET /public/v2/users/6940777 Get user details
PUT|PATCH /public/v2/users/6940777 Update user details
DELETE /public/v2/users/6940777 Delete user

58b5d1a7caba758f40dcc6e40a6bc616cab573a78cf15a9e615527d8c49fbca0

request body

{
"name": "Tenali Ramakrishna",
"gender": "male",
"email":"tenali.ramakrishna@15ce.com",
"status": "active"
}

> to automate the process of random name and email we have add pre-request script
math.random.tostring(36).substring(2);
---- ---------------- -----------
library method submethod

- this will create the the random string and we can store it in variable

var random= math.random.tostring(36).substring(2);

- now we can create variable like,

var name=random;
var email=random+"@gmail.com";
- and then store into env var

pm.environment.set.("name_env", name); //if value is variable then don't put


in " "
pm.environment.set.("email_env", email);

- now we can use request body as follow, which will auto generate name & email
every time when we run the request.

request body

{
"name": "{{name_env}}",
"gender": "male",
"email":"{{email_env}}",
"status": "active"
}

> now after the post request sent, we have to capture the "id" generated.

- so first we capture the response data & store the response date into variable

JSON.pasre(responceBody);
var jsondata=JSON.pasre(responceBody); // we can write in single statement

- now we create env var for id generated

pm.environment.set.("id_env",jsondata.id);

> post request is done. now for get request we can use the env var {{id_env}} in
url

- we have to validate this response to the the data we posted in the post request

for that we have have to do 2 steps


1. store response in variable &
2. compare that to the env var set during post method

var jasondata=pm.response.json(); //store response

pm.test("respone match ", ()=>{


pm.expect(jsondata.id).to.eql(pm.environment.get("id_env")); //get & validate id
pm.expect(jsondata.name).to.eql(pm.environment.get("name_env")); //get &
validate name
pm.expect(jsondata.email).to.eql(pm.environment.get("email_env")); //get &
validate email

}
);

> now in put request we have modify data every time we run the request, so we add
same pre-request to as added in post request.

var random= math.random.tostring(36).substring(2);

- now we can create variable like,


var name=random;
var email=random+"@gmail.com";

- and then store into env var

pm.environment.set.("name_env", name);
pm.environment.set.("email_env", email);

request body

{
"name": "{{name_env}}",
"gender": "male",
"email":"{{email_env}}",
"status": "active"
}

> in last delete if we want to delete the env var we declare then write script to
unset the var.

pm.environmenr.unset("id_env");
pm.environmenr.unset("name_env");
pm.environmenr.unset("email_env");

> this the complete flow of API chaining.

#Data driven API testing/Data based variable:

> in the post request we can use variable {{ }} and the value of this variable is
taken from the external file like csv,.json, text..

> we can upload the file at the time of collection run and at that time no of
itration we have to select(no of records from file)

# file uploading

> we have the file---- RestAPI.jar


- at the file location run cmd ----- java-jar file-upload-RestAPI.jar

post- httos//localhost:8080/uploadsinglefile
post- httos//localhost:8080/uploadmultiplefile

- so in body section we have to select form-data and in key section


we have to enter "file" (for single file upload) & "files" (for multiple file
upload) and in value we have to upload file

# Authentication types:

1. basic Auth.---- req username & password


2. digest Auth.--- req username & password, transfer data into encrypted form
3. bearer token--- we have to generate Auth token fom GitHub....
4. APIkey Auth.---
5. OAuth1.0 & OAuth2.0--- 3rd party Authentication more sequere
// Swagger-interactive document use for exploring the ApIs and not for testing. we
cant put any validation. we cant save the result and generate report of it. only
use for document purpose

// cURL- client url, which contain all the parameter with url like endpoints,
request body....

// to generate random number like string we have to use math library


const randomnum= math.floor((math.random()*100+1));

You might also like