API Testing Complete Notes
API Testing Complete Notes
What is an API?
API ( Application Program Interface ) that allows two applications to talk to each other.
Example:
Technically:
API is Collection of Pre-Written (Codes) like- Packages,Classes and Interfaces with respective
methods, fields and constructor.
Show the developer option and api call in fb or gmail - in Real time
If this API will be Uploaded into some server/Cloud i.e. API available on Web then We call this
WebServices---that’s all
So we can say that- All WebServices are API but all API are not WebServices.
Because all API will not be available over the web---very simple it is.
Test Automation: By- Pankaj Gupta
API’s are used to connect the two different platforms. And JSON and XML are the
schemas which are used to return the output from the REST and SOAP.
As opposed to SOAP, REST is not a protocol but an architectural style. ... It allows
different messaging formats, such as HTML, JSON, XML, and plain text, while SOAP
only allows XML. REST is also a more lightweight architecture, so RESTful web
services have a better performance
Test Automation: By- Pankaj Gupta
End Point:
The place that APIs send requests and where the resource lives, is called an endpoint.
Here- https://www.google.com is the end point where so many resources live like
map,news,video,image etc.
URI/HOST - https://www.google.com
Resource/Services- /maps/search/ (google offers multiple services map is one of the services)
Parameters- ?api=1¶meters
Both URL and URI is protocol to retrieve the resource. URI stands for uniform resource
identifier and URL stands for uniform resource locator. ... You see, a URI can be a name,
locator, or both for an online resource where a URL is just the locator. URLs are a subset of
URIs.
Test Automation: By- Pankaj Gupta
GET- The GET method is used to extract information from the given server using a given URI.
While using GET request, it should only extract data and should have no other effect on the
data. No Payload/Body required.
In case of get request -we need to pass parameters as part of URI/URL to retrieve the Data.
POST- A POST request is used to send data to the server, for example, customer information,
file upload, etc. using HTML forms.
In case of Post request -we need to pass Body/Payload as xml/json to add/manipulate the
Data.
Body/Payload:
PUT- Replaces all current representations of the target resource with the uploaded content.
In case of Put request -we need to pass Body/Payload as xml/json to Edit/manipulate the Data.
DELETE- Removes all current representations of the target resource given by a URI.
Resources can have multiple representations
In case of Delete request -we need to pass Body/Payload as xml/json to Delete/manipulate the
Data.
Manual Testing Using PostMan- What is PostMan ? What are the different things are
there?
--Install PostMan and Explanation
-Other tools also there but postMan is very easy to use
https://chrome.google.com/webstore/detail/postman/fhbjgbiflinjbdggehcddcbncdddomop//%40
PostMan For Chrome Add on Extension or You can download as well.--Do Signup free
Understand basic terms then will see google map API?
Test Automation: By- Pankaj Gupta
Explanation of Postman?
Header- to Authenticate the user, pass different input like json/xml/text etc.
https://developers.google.com/maps/documentation/javascript/tutorial
Go to API Library>>Click on Place API>>Enable it>>Go to Secure connection> get the API key
AIzaSyCLuzFNxbXlbSbXCDeRbZKUGjem0FpfQEs (This will be your same api Keys)
Save it now.
Lets See PostMan Tools First then we will go for the Search API.
All HTTP response status codes are separated into five classes or categories.
Status/Error Codes Series:
The first digit of the status code defines the class of response, while the last two digits
do not have any classifying or categorization role But gives you actual code when
merged with the first digit.
Test Automation: By- Pankaj Gupta
1. Retry the web page by selecting the refresh/reload button, pressing F5, or trying
the URL from the address bar again. ...
2. Restart all of your network devices. ...
3. Check the proxy server settings in your browser or application and make sure
they're correct.
405 Error:
Method Not Allowed response status code indicates that the request method is known
by the server but is not supported by the target resource.
Test Automation: By- Pankaj Gupta
HTTP:
HTTP is a protocol which allows the fetching of resources, such as HTML documents.
It's the foundation of any data exchange on the Web and it is a client-server protocol, which means
requests are initiated by the recipient, usually the Web browser and sent to the server and we
receive the response from the server.
REST, or REpresentational State Transfer, is an architectural style for providing standards between
computer systems on the web, making it easier for systems to communicate with each other.
SOAP is an acronym for Simple Object Access Protocol. It is an XML-based messaging protocol for
exchanging information among computers.
What is RestAssured?
REST Assured is the best open source tool for Automation Testing. REST Assured is a Java
library for validation of REST web services.
A REST API defines a set of operations where developers can perform requests and receive
responses via HTTP protocol.
By stateless it means that the server does not store any state about the client session
on the server side. The client session is stored on the client. The server is stateless
means that every server can service any client at any time, there is no session affinity or
sticky sessions
Test Automation: By- Pankaj Gupta
Stateless – No client data is stored on the server between requests and session state is stored
on the client.http is the transport protocol for REST.
Here Using RestAPI- instant representation and state of an object we can check.
Test Automation: By- Pankaj Gupta
RestAssured Setup:
REST Assured is a Java API/Library or expressive programming language(Custom
designed to perform specific tasks) for simplifying testing of REST based services built
on top of HTTP Builder. It supports POST, GET, PUT, DELETE, OPTIONS requests and
can be used to validate and verify the response of these requests.
Basic code:
Reff: https://github.com/rest-assured/rest-assured/wiki/Usage Official Website
Note:We can Download Rest Assured Jars from below if not using Maven
https://github.com/rest-assured/rest-assured/wiki/Downloads
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>json-path</artifactId>
<version>4.3.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>xml-path</artifactId>
<version>4.3.3</version>
Test Automation: By- Pankaj Gupta
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>json-schema-validator</artifactId>
<version>4.3.3</version>
<scope>test</scope>
</dependency>
Make sure RestAssured library is Available after setup:
To check use below:
import io.restassured.RestAssured.*;
import io.restassured.matcher.RestAssuredMatchers.*;
import org.hamcrest.Matchers.*;
Example:
Given().
BaseURL("www.google.com").
param("user", "password").
header("place", "Bangalore,india").
when().get/post/put/delete(endpoint)
Then().
statusCode(XXX).
body("all the details fetched”);
Code Explanation
Already Tried Dummy API Manual Testing with PostMan, cURL is given below for all:
curl -X GET \
http://dummy.restapiexample.com/api/v1/employee/2 \
-H 'cache-control: no-cache' \
-H 'postman-token: 0bab3c22-a90c-2a4d-ac24-5bd103012b0c'
curl -X POST \
http://dummy.restapiexample.com/api/v1/create \
-H 'cache-control: no-cache' \
-H 'content-type: application/json' \
-H 'postman-token: 14b49e79-69f3-0d95-1ed8-39a7c616a8e8' \
-d '{
"name":"test",
"salary":"123",
"age":"23"
}'
curl -X PUT \
https://reqres.in/api/users/2 \
-H 'cache-control: no-cache' \
-H 'content-type: application/json' \
-H 'postman-token: ae559197-2ac8-6d47-eab8-42d066ee0859' \
-d '{
"name": "Pankaj",
"job": "zion resident"
}'
curl -X DELETE \
http://dummy.restapiexample.com/api/v1/delete/2 \
-H 'cache-control: no-cache' \
-H 'postman-token: bd8028af-1a7a-7ad4-61cf-2e326acaacef'
package api.testing;
import io.restassured.RestAssured;
import io.restassured.RestAssured.*;
import io.restassured.matcher.RestAssuredMatchers.*;
import io.restassured.response.Response;
import org.hamcrest.Matchers.*;
import org.junit.Assert;
import org.junit.Test;
Test Automation: By- Pankaj Gupta
}
//DELETE Call Using RestAssured Automation
@Test
public void deleteEmployee()
{
//Passing Base URL
RestAssured.baseURI="http://dummy.restapiexample.com/api/v1";
Response res=given()
.when().delete("/delete/2")
.then().extract().response();
int STATUSCODE=res.getStatusCode();
System.out.println("Status code of the API is = "+STATUSCODE);
Assert.assertEquals(STATUSCODE, 200);//verifying the status code
}
}
Go to the official website of twitter for API Docs- Just Search Rest API for Twitter
Prerequisite:
1. Twitter Account
2. You need a website from which you want to connect to twitter. (will give dummy
Website)
Create Access Token & token Secret (We can Regenerate as well- For Security Purpose)
Post Twitte:https://developer.twitter.com/en/docs/twitter-api/v1/tweets/post-and-engage/overview
curl -X POST \
https://api.twitter.com/1.1/statuses/update.json \
-H 'authorization: OAuth
oauth_consumer_key=\"wgSOcQ40V4Pe40a2fePq5pi6n\",oauth_token=\"764067006-
dTTC3u4MMcCbdvE6BGILpYknbLsbmzKvaoOZnkcZ\",oauth_signature_method=\"HM
AC-
SHA1\",oauth_timestamp=\"1609076253\",oauth_nonce=\"bep5I9\",oauth_version=\"1.0
\",oauth_signature=\"6DjLf7U9l5B1pPdU63m36FuZ8gU%3D\"' \
-H 'cache-control: no-cache' \
-H 'content-type: multipart/form-data; boundary=----
WebKitFormBoundary7MA4YWxkTrZu0gW' \
-H 'postman-token: 94fb97a0-ce59-9b7f-a962-66731b5bd34f' \
-F 'status=Hello All'
curl -X GET \
https://api.twitter.com/1.1/statuses/home_timeline.json \
-H 'authorization: OAuth
oauth_consumer_key=\"wgSOcQ40V4Pe40a2fePq5pi6n\",oauth_token=\"764067006-
dTTC3u4MMcCbdvE6BGILpYknbLsbmzKvaoOZnkcZ\",oauth_signature_method=\"HM
AC-
SHA1\",oauth_timestamp=\"1609076205\",oauth_nonce=\"Suie7I\",oauth_version=\"1.0\
",oauth_signature=\"A9dUzpQnAeM%2FqvAfrtwNor2Ne9I%3D\"' \
-H 'cache-control: no-cache' \
-H 'postman-token: fc529b16-60b9-5e4a-77a9-e929dd1931a4'
Delete Tweet:
curl -X POST \
https://api.twitter.com/1.1/statuses/destroy/1343189310976081921.json \
-H 'authorization: OAuth
oauth_consumer_key=\"wgSOcQ40V4Pe40a2fePq5pi6n\",oauth_token=\"764067006-
dTTC3u4MMcCbdvE6BGILpYknbLsbmzKvaoOZnkcZ\",oauth_signature_method=\"HM
AC-
SHA1\",oauth_timestamp=\"1609076271\",oauth_nonce=\"fRcZ9w\",oauth_version=\"1.
0\",oauth_signature=\"AuEShG3PrCOSS8C1%2FH7qPoGtZT4%3D\"' \
-H 'cache-control: no-cache' \
Test Automation: By- Pankaj Gupta
-H 'postman-token: 3f246a34-977b-9011-ef15-a835ae8fe59c'
If we have multiple Tweets we can make the count as a parameter and use in our API
call, lets see
Add Parameter count=1 in API we will get only one updated tweet.
That's how the get() method works.
Let's see by Using our Automation- by RestAssured.
Example:
package RestAssuredTest;
import org.testng.Assert;
import org.testng.annotations.Test;
import io.restassured.RestAssured;
import io.restassured.path.json.JsonPath;
import io.restassured.response.Response;
import static io.restassured.RestAssured.given;
import java.util.ArrayList;
.queryParam("count", 1)
.when().get("/home_timeline.json")
.then().extract().response();
//Converting Response into String Object
String response=res.asString();
System.out.println(response);//All the response will be there
//Now we want to iterate the Response and since response is in Json so lets use
JsonPath
JsonPath j=new JsonPath(response);
//now by j reff call the method to get the text
ArrayList<String> t=j.get("text");//Hold in string- Refer the postman for Json Key
System.out.println("Text available into Tweeter is "+t);
}
@Test
public void getTweetTestValidate()
{
//Passing Base URL
RestAssured.baseURI="https://api.twitter.com/1.1/statuses";
Response res=given().auth().oauth(consumerKey, consumerSecrete, Tokens,
TokenSecret)
.queryParam("count", 1)
.when().get("/home_timeline.json")
.then().extract().response();
int Statuscode=res.getStatusCode();
Assert.assertEquals(Statuscode, 200);//verifying the status code
}
@Test
public void postTweet()
{
//Passing Base URL
RestAssured.baseURI="https://api.twitter.com/1.1/statuses";
Response res=given().auth().oauth(consumerKey, consumerSecrete, Tokens,
TokenSecret)
.queryParam("count", 1)
.when().post("/home_timeline.json")
.then().extract().response();
int Statuscode=res.getStatusCode();
Assert.assertEquals(Statuscode, 200);//verifying the status code
}
}
If its get request we can use Query parameter, But if its post we have to use form-Parameter in body:
package RestAssuredTest;
import org.testng.Assert;
import org.testng.annotations.Test;
import io.restassured.RestAssured;
import io.restassured.path.json.JsonPath;
import io.restassured.response.Response;
import static io.restassured.RestAssured.given;
import java.util.ArrayList;
}
//@Test
public void getTweetTestValidate()
{
Test Automation: By- Pankaj Gupta
Now in One Test we will see how to create a post tweet then verify it by Getting it and
then delete it--end to end.
https://jsonformatter.org/ -
That's All.
Test Automation: By- Pankaj Gupta
Hello Team,
I need your help to keep myself Motivated to create exciting stuff for you.
JAVA-SELENIUM:
https://www.udemy.com/course/selenium-webdriver-with-java-testng-maven-git-jenkins/
https://www.udemy.com/course/learn-element-locators-css-selector-and-xpath-from-scratch/
https://www.udemy.com/course/learn-git-from-basic/
TestNG
https://www.udemy.com/course/testng-framework/
JAVA Basics:
https://www.udemy.com/course/java-for-beginner/
Please email me your current course and the Discount Coupon code you need for the course at
given address: pankajonlinetutor@gmail.com
Learning Tips:
2. Everything looks very easy while watching, Once you go through then you will be more
cleared about the topic.
I need your support, Please help me with your Ratings and Review.
Thank you !