0% found this document useful (0 votes)
32 views6 pages

RestAssured-SDET QA

Uploaded by

shashwats.rai3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views6 pages

RestAssured-SDET QA

Uploaded by

shashwats.rai3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

Pre-Requisites:

-----------------
1. Java9 + & Eclispe
2. TestNG
3. Maven (comes with Eclipse)

Dependencies
-----------------
rest-assured
json-path
json
gson
testng
scribejave-apis
json-schema-validator
xml-schema-validator

HTTP Requests
-----------------
GET
POST
PUT
DELETE
PATCH

Supports BDD(Behavioural Data Driven)


Gherkin
import static packages

GET Users:
https://reqres.in/api/users?page=2

POST Users:
https://reqres.in/api/users

{
"name": "morpheus",
"job": "leader"
}

UPDATE User:
https://reqres.in/api/users/2

{
"name": "morpheus",
"job": "zion resident"
}

DELETE User:
https://reqres.in/api/users/2

204
4 Different Ways to Create Request Body
--------------------------------------------
1. HashMap
2. Using org.json
3. POJO (Plain Old Java Object)
Create a new POJO class.
Create a class getters and setters.
4. External JSON File
Create a new 'body.json' file.
File f = new File(".\\body.json");
FileReader fr = new FileReader(f);
JSONTokener jt = new JSONTokener(fr);
JSONObject data = new JSONObject(jt);

Path & Query Parameters


------------------------
http://reqres.in/api/users?page=2

http://reqres.in/api/users?page=2&id=5

Cookies and Headers


-----------------------
The value of cookie shall change every time.
Map<String,String> cookies_values = res.getCookies();
System.out.println(cookies_values.keySet());
String cookie_value;
for (String j:cookies_values.keySet())
{
cookie_value = res.getCookie(j);
System.out.println(j+" "+cookie_value);
}

Header

.header("Content-Type", "")
Headers myheaders = res.getHeaders();
for(Header k:myheaders)
{
System.out.println(k.getName()+" "+k.getValue());
}

Parsing JSON Response Data


---------------------------

Response res=given()
.contentType(ContentType.JSON)
.when()
.get("http://localhost:3000/store");

Assert.assertEquals(res.getStatusCode(), 200);
Assert.assertEquals(res.header("Content-Type"), "application/json");

String bookname = res.jsonPath().get("book[3].title").toString();


Assert.assertEquals(bookname, "The Lord of the Rings");

boolean status=false;
for(int i=0; i<jo.getJSONArray("book").length(); i++)
{
String bookTitle =
jo.getJSONArray("book").getJSONObject(i).get("title").toString();

if(bookTitle.equals("Moby Dick"))
{
status=true;
break;
}

Assert.assertEquals(status, true);

//Validate the Total Price of the books

double totalprice = 0;
for(int k=0; k<jo.getJSONArray("book").length(); k++)
{
String bookPrice =
jo.getJSONArray("book").getJSONObject(k).get("price").toString();
totalprice = totalprice+Double.parseDouble(bookPrice);
}

System.out.println("The Total Price of the Books is:"+totalprice);

Parsing XML Response


---------------------

http://restapi.adequateshop.com/swagger/ui/index

https://fakerestapi.azurewebsites.net/index.html

https://thetestrequest.com/authors.xml

JSON/XML Schema Validations


----------------------------
Response validations--validate data
Schema Validation --validate type of data

1. Create a JSON Schema file and store it in the src/test/resources.


2. import io.restassured.module.jsv.JsonSchemaValidator;
3. .assertThat().body(JsonSchemaValidator.matchesJsonSchemaInClasspath("storeJsonSc
hema.json"));

JSON Response(.json) --> JSON schema(.json) https://jsonformatter.org/json-to-


jsonschema
XML response (.xml) --> xml schema (.xsd) https://www.convertsimple.com/convert-
xml-to-xsd-xml-schema/

Serialization: It is an inbuilt function in RestAssured. POJO--->json


Deserialization: json---->pojo
Body(json) ----------> Request ---------->Response(json)

Authorization
-------------
It is like the user is valid. But, whether the user has the permission or access.

Authentication
----------------
It will check whether the user is valid or not. Valid credentials or not.

Authentication Supported in RestAssured:


1. Basic: just we provide username and password.
2. Digest
3. Bearer Token: also called as token authentication- is an HTTP authentication
scheme that involves security tokens called bearer tokens. It is crypted string.
https://postman-echo.com/basic-auth

4. OAuth 1.0, 2.0


OAuth 2.0: Whenever a 3rd-party client sends request to fetch data, during
this time for the authentication of the third party client used is OAuth 2.0
protocol.
Example: At first, the resource owner authorises permission to the client
application and using the same authorization, the client application takes a token
from the authorization server. After taking the token to the resource server,
whatever protected resource is/are available, the resource server gives it to the
client application.

Using PayPal(Client Id): AZqJYB96SjO4JVdr3sNofZf-


WwnI6HWESaIyP5XStq1M18jynDI45zkk19wZQmDkAGGbO851xp3Th9GY

Secret key 1:
EGoMwONbJCjARU_FsX5rPry4rKd7juKAUFGj6YGEl4iKoOUFdiYQSppTcO_g4LP8EiJyifAnRO1dmpua

5. API Key
6. Preemptive

Faker library

JSON Object starts and ends with {}.


JSON Array starts and ends with [].
JSON Element can be either a JSON Object or a JSON Array.

Chaining
------------
It is a process wherein we send a request and the data or response that we receive
now can be used to create another request to fetch further data or response.

GoRest
Create User --> Get User --> Update User --> Delete User

Separate classes for the Create, Get, Update, Delete request.


Then, create an xml file for all the above methods to be tested. The Test Suite
created will have only one test wherein all the classes are tested.
However, we can separate the tests for each class and then run the suite.

context.getSuite().getAttribute()
Once the suite is defined globally. It is anyway accessible by the local method.

Framework Development
------------------------
When we try to automate many test cases and maintain all project related files.
Objective of Designing an Automation Framework:
Reusability, Maintainability, Readability

Hybrid Driven Framework:


--------------------------
Data Driven +

Phases of Framework Development


-------------------------------
1. Understanding requirement
- Functional Specifications
- Swagger
2. Choose automation tool //RestAssured and Selenium are not exactly tools.
They are libraries.

3. Design Framework

4. Development

5. Execution + CI

Rest Assured Framework Design----->


3 phases

1. Development

2. Execution

3. CI

EndPoints--> We'll maintain all the endpoints like Routes, UserEndPoints,


StoreEndPoints, PetEndPo
For every module, there will be separate endpoints. And all of them will be
stored in a package.

Routes: It is like a single class where all the URLs are maintained for all
the modules.
We'll create class for each and every module.

Test Cases: Creating test cases for the modules. User Tests, Store Tests, Pet
Tests. Once the Test cases are ready, they'll be executed by the TestNG file. And
then later we'll run through pom.xml.

Payload--> Creating the required data. (POJO classes are required.)User, Store and
Pet.

Utilities--> DataProviders
Extent Report
XL Utility

Test Data--> UserData.xlxs


Store Data
Pet Store
Reports --

---------------------------------------
Pre-Requisites
Step 1: Create a Maven project.

Step 2: Update pom.xml with the required dependencies.

Step 3: Create Folder Structure


-----------------------------------------

Step 4: Create Routes.java--> contains URL's


We've to use the routes to implement different types of requests. Using the
routes we've to define the EndPoints.

Step 5: Create UserEndPoints.java ---> CRUD methods implementation

Step 6: Create Test Cases

Step 7: Create Data Driven Tests


Excel Sheet
Use of Apache POI
Excel Utility File
Data providers methods will get the data from the excel sheets.
In Selenium, data provider is an annotation method and whichever method
we specify as data provider that method is responsible for providing data to
another test method.

You might also like