API Notes-2
API Notes-2
What is API?
API stands for Application Programming Interface.
Whenever we want to communicate between two or more applications, we use API.
Or
Whenever we want to communicate between two or more, same or different technologies,
languages or platform, we’ll use API.
Advantages of doing API testing/ Why do we need to do API
testing
1. To verify the dataflow is happening properly or not, we do API testing.
2. It is an early stage of testing, so we need to do API testing.
3. Developer will get sufficient time to fix the defect, since we are finding the defects in
the early stage.
4. Downward flow of defect is less, since we do the testing on the middleware or since
we since we find the defects in the middleware.
5. API testing is light weight since there is no UI.
6. API supports all the 3 types of application that is., Web based application, Client
server application, Standalone application.
What is API testing?
Testing the data flow between two or more application is called as API testing.
Or
Testing the middleware is called as API testing.
Or
Testing the application in the business layer without UI is called as API testing.
When to do API testing?
Once after doing WBT, we do API testing. After that we do BBT.
Types of API
1. Internal API
2. External API
Internal API
The APIs which are developed for our own project is called as Internal API
Example 1:
External API
The APIs which are purchased from other companies for business purpose is called as
External API.
Example 2:
Example 3:
Example 4:
Why the name Middleware testing?
Since API testing comes in between WBT and BBT we refer it as Middleware testing.
WBT ---> API Testing (Middleware testing) ---> BBT
When we use API in real time?
Case 1: Whenever we want to interact between 2 or more different technologies(language)
we use API’s.
Case 2: Whenever we want to interact between 2 or more application we use API’s.
Note: Both Entry and Exit Criteria of API testing around 90% pass testcases.
Webservices
API’s that are supported only for web-based application then that API’s are called as web
services. It is mandatory to have internet to test web-based API’s
Note: All the webservice are APIs but all the APIs are not webservices.
Classifications of API
1. SOAP API (Simple Object Access Protocol)
2. REST API (Representational State Transfer)
SOAP API
It is also one of the API where we send the request in the form of XML and receive
the response in the form of XML.
For a tester it is difficult to understand XML language and also it is not any kind of
programming language. It is the major drawback of API.
Client----------------------->Server
Request XML
Client<----------------------Server
Response XML
REST API
It is also one of the API where we send the request in the form of URL (Uniform Resource
Locator) (Http or Https) and fetch the response in the form of Json, image, text, image, XML,
html, JavaScript, etc.
Client----------------------->Server
Request URL (http/https)
Client<----------------------Server
Response Json, XML, image, text, html, JavaScript, etc.
Testing Pyramid
WBT
API Testing
BBT
It describes the stages of testing in the project. Every company follows the Test Pyramid
process.
How to download Soap UI tool?
Open the browser and type SoapUI download for Windows.
Step 1: SoapUI download for Windows.
Step 2: SoapUI latest releases.
Step 3: Download for 64-bit windows.
Step 4: Click on Next for everything.
Step 5: Click on Finish.
Developer is going to give the Soap API document in the form of .wsdl (Web Services
Description Language).
Limitations or Drawbacks of API Testing
1. Since there is no UI, understanding the dataflow is very difficult.
2. Understanding the Swagger document is not so easy until and unless you get a
proper KT (Knowledge Transfer).
3. Hand-in-Hand interaction is needed between the developers and test engineers.
Http Method or Request Method
Http methods are used to perform CRUD operation on the server. There are few http
methods:
Post – To create or add the data inside the server.
Get – To read/retrieve/fetch/view the data inside the server.
Put – To update/modify/edit the data inside the server.
Patch – For partial updation.
Delete – To delete the data from the server.
Status Codes
Status Codes are used to validate the response given by the server.
Or
They are sent by the server to the browser to indicate whether the request has been
processed or not.
There are few levels in the Status Code:
Level 100 – Continue (Partial Execution)
Example: If you open Amazon sometimes partially it will be loaded so at this time server will
throw 100 status code.
Level 200 – Successful
200 – Ok
201 – Create
202 – Accepted
203 – Non-Authoritative Information
204 – No content
205 – Reset Content
206 – Partial Content
Level 300 – Redirected (To another server)
Example: You will try to download a movie sometimes you will be suddenly navigated to
other page this is what redirected is.
301 – Moved Permanently
302 – Found (Moved Temporarily)
304 – Not Modified
Level 400 – Client-Side Error (You have done some mistake in the request)
400 – Bad Request
401 – Unauthorized
402 – Payment Required
403 – Forbidden
404 – Not Found
405 – Method Not Allowed
409 – Conflict
413 – Payload too large
415 – Unsupported Media Type
422 – Un-processable Entity
429 – Too Many Requests
451 – Unavailable For Legal Reasons
Level 500 – Server-Side Error (Server is not working properly)
500 – Internal Server Error
501 – Not Implemented
502 – Bad Gateway
503 – Service Unavailable
504 – Gateway Timeout
599 – Network Timeout
Assertion
Validating the expected result or the actual result is called as Assertion.
or
Validating what customer asked for and what company has developed is called as Assertion.
In Postman by using snippets, we’ll be adding the Assertions.
Note: Snippets is nothing but piece of code.
Authorization
Level or permission given to access the server.
XML (Extensible Markup Language)
Xml is a markup language and platform independent language which is used to store
and transport the data.
Different applications which are developed using different technologies or same
technologies can transfer the data among themselves with the help of xml.
Xml has rules and syntaxes almost similar to html and it is an advanced version of
html.
The tags inside xml are always case sensitive but html tags are not case sensitive.
The tags in xml are defined by users but in html only inbuilt tags are there user
cannot define the tags.
Syntax:
<root>
<child>
<subchild>
</subchild>
</child>
</root>
Example:
<BookStore>
<Book name=”Automation”>
<title>Webdriver</title>
<Author>Json</Author>
</Book>
</BookStore>
Soap UI
It is a tool which is used to interact with XML API’s. Here, we use WSDL (WebServices
Description Language) documentation to perform the action through API’s.
Steps to add WSDL file to Soap UI:
Open Soap UI tool
Soap Major Tab
Specify the Project name
Specify the URL of WSDL
Click on Ok
Steps to create TestSuite:
Right click on the project
New TestSuite
Specify Suit name
Ok
Steps to create Test Case:
Right click on the TestSuite
New Test Case
Specify Test Case name
Ok
Steps to create TestStep:
Right click on the Test Case
Add step
Select Soap request
Specify request name
Select the request
Specify the name of the request
Ok
Note: For a project we can have ‘n’ number of TestSuites, for a TestSuite we can have ‘n’ no
of Test Case, for a Test Case we can have only one TestStep and inside that we can perform
‘n’ no of operations.
Variables or Properties in Soap UI
In SoapUI properties are nothing but variables which are used to store the data.
In Soap UI we can develop the properties in different levels such as:
1. Global Level
2. Project Level
3. Test Suite Level
4. Test Case Level
5. Test Step Level
Global Level
The properties created in global level can be used in any of the projects, suits, test cases &
test steps.
Steps: Click on preferences major tab > global properties > click on the plus icon > specify
the key and value.
Project Level
Any properties that are created inside the project level can be accessed throughout the
project.
Steps: Select the project > double click on the project > click on the plus icon > specify the
key and value > works on mapping concept.
Test Suite Level
Any properties that are created inside the test suite level can be accessed throughout the
test suits.
Steps: Select the test suite > double click on the test suite > click on the plus icon > specify
the key and value.
Test Case Level
Any properties that are created inside the test case level can be accessed throughout the
test case.
Steps: Select the test case > double click on the test case > click on the plus icon > specify
the key and value.
Test Step Level
The properties created in test step level can be accessed in all the test steps.
Steps: Right click on the test step > add step > properties > specify the property name > click
on the plus icon > specify the key and value.
Groovy Script
It is a scripting language developed with the help of Java.
It uses the rules and syntaxes of Java
In Groovy Script to print the message we have an inbuilt snippet called “log.info”
Example: log.info(“Hi”);
log.info(“Hello”);
log.info(“world”);
In Groovy Script we don’t have separate datatypes instead we use “def(define)” for every
type of value.
Example: def a=10;
def b=20;
def c=a+b;
log.info(c);
Steps to create Groovy Script inside SoapUI
Right click on the TestStep.
Add step
Groovy Script
Specify the Groovy Script name
Ok
Steps to fetch the properties by using Groovy Script
//Project level
def pro_lev=context.expand(‘${#Project#variablename}’)
log.info(pro_lev)
//TestSuite level
def suite_lev=context.expand(‘${#TestSuite#variablename}’)
log.info(suite_lev)
//TestCase level
def case_lev=context.expand(‘${#TestCase#variablename}’)
log.info(case_lev)
//TestStep level
def step_lev=context.expand(‘${#variablename}’)
log.info(step_lev)
//Global level
Def glo_lev=context.expand(‘${#Global#variablename}’)
Log.info(glo_lev)
context.expand is a snippet.
Snippet is nothing but piece of code which has a predefined meaning.
$ search in all the directions.
# represents levels.
//Project level
def p1= testRunner.testCase.testSuite.Project.getPropertyValue(‘name’)
log.info(p1)
//Suite level
def s1= testRunner.testCase.testSuite.getPropertyValue(‘name’)
log.info(s1)
//Case level
def c1= testRunner.testCase.getPropertyValue(‘name’)
log.info(c1)
//Step level
def ss1= testRunner.testCase.getTestStepByName(‘Properties’).getPropertyValue(‘name’)
log.info(ss1)
//Global level
def g1= com.eviware.soapui.SoapUI.globalProperties.getPropertyValue(‘name’)
log.info(g1)
Re-initialization of properties by Runner class
To Re-initialize or to create new property in SoapUI we use “setPropertyValue()” which takes
two parameters as inputs i.e., key and value.
Write a Groovy script to re-assign the property values
//Project level
def p1= testRunner.testCase.testSuite.Project.setPropertyValue(‘name’,’value’)
log.info(p1)
//Suite level
def s1= testRunner.testCase.testSuite.setPropertyValue(‘name’,’value’)
log.info(s1)
//Case level
def c1= testRunner.testCase.setPropertyValue(‘name’,’value’)
log.info(c1)
//Step level
def ss1=
testRunner.testCase.getTestStepByName(‘Properties’).setPropertyValue(‘name’,’value’)
log.info(ss1)
//Global level
def g1= com.eviware.soapui.SoapUI.globalProperties.setPropertyValue(‘name’,’value’)
log.info(g1)
//Project level
testRunner.testCase.testSuite.Project.removeProperty(‘name’)
//Suite level
testRunner.testCase.testSuite.removeProperty(‘name’)
//Case level
testRunner.testCase.removeProperty(‘name’)
//Step level
testRunner.testCase.getTestStepByName(‘Properties’).removeProperty(‘name’)
//Global level
com.eviware.soapui.SoapUI.globalProperties.removeProperty(‘name’)
Property Transfer (Response Chaining)
In SoapUI if you want to send response of one TestCase as a request of another TestCase we
use Property Transfer.
Here, we first create a step for property transfer and we transfer the data between 2
TestCases.
Steps:
1. Right click on testStep > Add Step > PropertyTransfer > Specify the name > Ok
2. Click on the Addition symbol > Specify the PropertyTransfer Name and click on Ok.
3. Property transfer consists of 2 fields
a. Source
b. Target
Source – It represents from where we are fetching the response.
Target – Here, we specify the response of one testcase as request to another.
Click on NS in both Source and Target.
After Type //*:
Assertions in SoapUI
The process of validating what customer asks for and what company has developed is called
as Assertion.
In SoapUI we have inbuilt options to perform the assertions.
Validation Process
Contains Validation: IT will return true when the specified value is present or else it will
return false.
Not Contains Validation: IT will return true when the specified value is not present or else it
will return false.
Valid Http Status Code: True when specified status code is present or else return false.
Invalid Http Status Code: True when specified status code is not present or else return false.
Soap Fault: Whenever we want to check the reason why we got the wrong response or
incorrect response we use Soap Fault. This will tell us whether the issue is from SoapUI end
or use end.
Response SLA:
It is used to check for the response time from the server.
Here, we can also validate are we getting the response in specified time or not.
Response SLA refers to the time taken to hit the server + process that request inside
the server + fetch the response from the server.
Rest
Rest stands for representational state transfer.
Here, we send the request in the form of URL and fetch the response in the form of
Json, image, xml, text, etc.
URI/ URL/ RequestURL
1. Protocols ----> http/https
2. Project, Domain ----> .com or .in or .org
3. Resources (where exactly request needs to be hit inside the server)
4. Parameters
a. Path Parameter
b. Query Parameter
Example: (Flipkart application)
https://www.flipkart.com/grocery/personal-baby-care/hair-care/shampoo/
Protocols
It is classified into two types:
1. Http
2. Https
Http (Hyper text transfer protocol)
It sets some rules and standards which governs how much data to be transferred or what
data to be transferred between the client and the server.
Https (Hyper text transfer protocol secured)
It does the same job as that of Http but by providing the security. Here, we are going to
install Firewalls at the server end for the data security purpose.
Project, Domain
This consists of companies project name followed by domain such as .in, .us, .uk, .com, .org.
Resources
It represents what we are searching inside the server.
Parameters
In the URI structure parameters are classified into 2 types:
1. Path Parameter
2. Query Parameter
Path Parameter (/): This parameter are returned to go to the specific point inside the URI
Example: https://www.amazon.in/order/bangalore/11436
Query Parameter (?): It sorts or filters the resource.
Example: https://www.amazon.in/orders?data=20-02-2023
Endpoint
It is a place inside the server where our request is going to hit.
In RestAPI we have to send the Endpoint along with the base URL.
In Rest we use http methods to perform CRUD operations.
Example: https://www.amazon.in/orders?data=20-02-2024
Http Structure
Http Request
Base URL
Endpoint
Http Method
Request Body
Http Method: It performs CRUD operations – Post, Get, Put, Patch, Delete
Request Body: It is the data sent to the server.
Http Response
Status Codes
Response Body
Response Time
Status Codes: They are sent by the server to the browser to indicate whether the request
has been processed or not.
Response Body: It is the data received from the server.
Response Time: Time taken to hit the request to the server, process the request and send
back the response to the client.
Postman
Postman is a collaborative tool used for many purposes by Developers to develop the APIs
and to maintain the API code and by API test engineers to test the APIs.
By using this tool we can perform CRUD operations.
In Postman the projects are represented as collections.
Steps to create collection
Click on the Addition symbol on the top left
Specify the collection name
Create a folder inside the collection
Start adding the request
In order to perform the Get operation where we retrieve the data
from the server
Click on Add Request
Specify the Request Name
Specify the http method
Paste the URI (Uniform Resource Identifier)
Save
Send
As soon as we click on Send, the API request will hit the server and in return fetches the
response from the server along with status code and duration.
Steps to perform Post action
Post refers to creating a new record in the server. Here, we send the payload usually in the
form of Json.
Click on Add Request
Specify the Request Name
Specify the http method
Paste the URI
Click on Body Tab
Select Raw
Select the format to Json
Specify the payload
Save
Send
Steps to perform Put action
Put refers to updating the already present data in the server.
Same as Post action but change the HTTP method to Put.
Steps to perform Delete action
Delete refers to removing an existing data from the server.
Steps:
Add Request > Specify the Request name > Specify the Http method > Paste the URI > Save >
Send
Note:
We usually don’t send any payloads for the delete action but we can specify it if is present in
the documentation.
Difference between Put and Patch
Put Patch
It is used to update the complete entity. It is used to update the partial entity.
Variables in Postman
In Postman we can develop the variables in different stages:
1. Global Level
2. Environment Level
3. Collection Level
Global Level
The variables that are created inside the Global level can be accessed in any of the
collections or environments.
Steps:
Click on quick look on the top right of the tool > Under Globals click on edit > Specify the
variable name(key) > Specify the variable value > Save
The variable takes 2 parameters as an input i.e., initial value and current value.
Initial Value: It is a value specified to the key for the first time.
Current Value: It is a value that reinitializes throughout the testing process.
Environment Level
The variables that are developed in the Environment level will be limited only within that
environment and also in different collections.
Steps:
Click on quick look on the top right of the tool > Add > Specify the Environment name >
Under that specify the key followed by value > Save
Collection/Project Level
That variables that are created in Project level is limited only within that project.
Steps:
Click on the project > Select the variables tab > Specify the key and value > Save
Authorization
It is nothing but permission or authentication given to access the server.
Different Levels of Authorization
1. No Authorization
2. Basic Authorization
3. Bearer Token
4. Jwt Bearer
5. 0auth 1.0
6. 0auth 2.0
7. Digest Auth
8. Hawk Authentication
9. Aws Signature
Steps:
Click on Test Case/Request > Click on Authorization > Select Authorization Type > Provide
Authorization
Components of Http Request
Authentication Authorization
Authentication means verifying a user’s or Authorization usually happens after
applications identity when accessing in API. authentication and is the process of
It tries to answer the questions, “Who are granting or denying access based on users
you?”. or application permissions.
Common Authentication methods include It tries to answer the question, “What are
Basic Auth, Digest Auth, 0auth, API keys, you allowed to do inside the server?”
Bearer Tokens and more.
Headers
Headers are nothing but additional information.
There are 2 types of Headers:
1. Request Headers: Additional information sent to the server.
Few request headers are:
Content-type
Cache-control
server
2. Response Headers: Additional information received from the server.
Few response headers are:
Date
Content-type
Note: In Headers data will be stored in the form of key and value.
Data Driven Testing
Testing the software for multiple test data is called Data Driver Testing.
In case of any UI automation tools, we need separate jar files.
In case of postman tool there is no need of any extra files we can directly import the data by
using “Microsoft Excel”.
Pre-requisite
Create an excel sheet with all the test data and save it in CSV type.
Steps:
Specify the collection > Select … dots > Run collection > Under the data click on select file >
Select the excel sheet > Select preview > Run the collection.
Newman
Why we go for Newman
Newman is still more light weight than Postman.
We can generate the reports in Newman.
We are going to run all the collections.
Steps:
Download Node JS first
Select pre built installer
Install Node JS
Open Command Prompt
node -v (Need to check that Node JS is installed)
npm -v (Node Package Manager) (It is used to fetch the package details)
npm install -g newman (Install Newman. Newman is the library in Node JS)
npm install -g newman-reporter-htmlextra (Install Newman reporter)
newman run (drag and drop the collection path) -e (drag and drop the Environment path)
(To run the collection)
newman run (drag and drop the collection path) -e (drag and drop the Environment path) -
n 5 (To run the collection for n number of times)
newman run (drag and drop the collection path) -e (drag and drop the Environment path) -
r htmlextra (To download the execution result)
newman run (drag and drop the collection path) -e (drag and drop the Environment path) -
n 5 -r htmlextra (To download the n number of execution result)
Steps to generate the report in Newman
1. Using the command line, we can also generate the report by using Newman html
extra.
2. Once after running the collection in command prompt specify the statement npm
install -g -newman-reporter-htmlextra.
3. It downloads the libraries necessary for generating the reports.
4. Later run the statement newman run collection path -r htmlextra.
5. It generates the report in the local repository with .html extensions.
Jenkins using Postman tool
1. It is a CI/CD tool (Continuous Integration or Continuous Deployment)
2. It is a continuous integration tool. In real time Jenkins are used to uninstall the old
build, install the new build and run the frameworks in the regular durations.
3. In postman we can use Jenkins to run the collections for every specified interval.
4. So, we can integrate with many tools like Github, Selenium, Postman, Restassured,
Appium, etc. where we are going to run the framework, collections, etc.
5. Jenkins is used for many purposes like scheduling the build and pollscm and can
integrate with any platform etc.
Steps to download Jenkins
1. Jenkins works as a local host in every server with the standard id as 8080.
2. To download this go to Jenkins official site and download .war file.
3. To first setup the Jenkins open the command prompt > specify the statement > java -
jar path of Jenkins.war > it downloads the necessary libraries > open the browser >
trigger localhost:8080 > install the suggested plugin > create an account and finish.
To run the collection
Click on New Item > Select free style project > specify the project name > give the
description to the project > specify the collection path from github or local repo > specify
the duration > select build step > click on execute windows batch command > specify
newman run path of the collection > save
For the first time click on buildnow > console output > this will run the collection > for every
regular duration that we have specified.
Any changed made in the Postman tool will not reflect in Newman, export the latest file and
then perform the action.
JDK 11 or 17 or 21 version should be there.
Var body = pm.response.json();
Var shop = body.data.userid;
Var email = body.data.email;
RestAssured
RestAssured is used to verify the RestAPIs by using java libraries.
It was initially owned by Jayways. But now the IO company owns it.
Using RestAssured we can validate the http responses from the server.
RestAssured has all the libraries which is necessary to perform the CRUD operations on the
server. Also, we can handle the Json structures(payloads) no matter how much ever complex
it is.
we can also validate the status codes, status lines, headers, response body from the server.
RestAssured is a very flexible java library for API testers.
It is preferable that we integrate the RestAssured libraries into Maven project.
RestAssured is continued to be an Automation process of API testing.
****************WRITE NOTES**************************
How to install TestNG for Eclipse?
Go to help
Eclipse Marketplace
Search for Testing
Install
Select all checkbox
Yes, I accept the risk
Trust selected
Restart the Eclipse
How to add TestNG for a particular project
Right click on the project
Build path
Add Libraries
TestNG
Gherkin
Gherkin language is used to perform CRUD operations on the browser.
In Gherkin language we use:
a. Given() -> To specify the baseurl, ContentType, Payload, Auth, QueryParams
and Headers, etc.,
b. When() -> To perform the http method and endpoint.
c. Then() -> After performing the Action, to add the assertions.
d. And() -> Extra additional verification.
Json Parsing
It is a process of navigating through the Json body to fetch the corresponding values.
In RestAssured to navigate through the Json body we use a class called Jsonpath and invoke
a method called get() to fetch the value of the corresponding Key.
JsonPath constructor is parameterized with string value of json body.
Example:
JsonPath jp = new JsonPath(body);
String FN = jp.get(“data[0].fn”);
To convert from object to Json:
Link is https://jsonformatter.org/json-parser
Assertions (to add to script)
.statusCode(200)
.contentType(“application/json”)
.statusLine(“HTTP/1.1 200”)
.time(Matchers.lessThanOrEqualTo(4000l))
.body(“message”, Mathcers.equaTo(“Created”))
*********Complete Notes!*****************
Advantages
1. For better readability
2. Reusability
3. Easy access to data
4. Platform independent
Some libraries are needed to achieve this
Jackson Databind
Jackson Core
Jackson Annotation
Json
Steps to pr
SpecBuilder
SpecBuilder is used to set the request specification and response specifications.
Types of SpecBuilder:
1. RequestSpecBuilder
2. ResponseSpecBuilder