0% found this document useful (0 votes)
33 views

API Testing With Rest Assured

Uploaded by

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

API Testing With Rest Assured

Uploaded by

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

API Automation

with Rest Assured


REST IN BDD SYNTAX
Introduction

 1. REST Assured is a Java library for validation of REST web


services. It offers a friendly DSL (Domain specific Languages)
that describes a connection to an HTTP endpoint and expected
results.

 Prerequisites:

 A sample Java project that already has imported HTTP/REST/JSON


API,
Dependencies

 Sel 3.0.1 [If validating data from UI to API endpoint]


 Io.rest-assured – 3.0.0
 Io.rest-assured-common
 Json-path, json-simple
 Hamcrest-core, hamcrest-library [optional]
 Httpclient
 Gson
 Jackson[optional]
 JDBC Driver[ If validating data frm API to Database]
 Testng/ Junit
Get Requests

 Apiurl : api.openweathermap.org/data/2.5/weather?q=London

 1. given().header(“Authorization”,
Key).contentType(“application/json”).when.get(apiurl).then.assert
That().statuscode(200);

 2. Int response = given().header(“Authorization”,


key).contentType(“application/json”).when().get(apiurl).getStatus
code();

 Assert.assertEquals(response, 200);
Validate Json Data: method 1

 Response response = given().header("Authorization",


KEY).contentType("application/json"). when().get(url);

 String actual =
response.then().contentType(ContentType.JSON).extract().path(“weather.
description ");

 Assert.assertEquals(actual, expected); // Here actual value frm Json ,


expected can be frm UI/DB

 Refer below links for extracting json path


 http://jsonviewer.stack.hu/
 https://jsonpath.curiousconcept.com/
Validate Json data: method 2

 1. given().when().get(apiurl).then().body(“Title”,
equalTo(“Selenium”).body(“language”,
equalTo(“Java”).statuscode(200);

 2.
given().when().get(apiurl).then().assertThat().body(constainsStrin
g(“Selenium”);
Post Request: Methd 1

 Map<String, String> body = new HashMap<>();


 body.put("CountryCode", "MY");
 body.put("Page", "1");
 body.put(“Name", “Test");

 Response response = given().header("Authorization",


KEY).contentType("application/json")
 .body(body).when().post(url);

 response.then().assertThat.statuscode(201);
Post Request: Method 2

 String APIUrl = URL;

 String APIBody = "{ \"CountryCode\": \"MY\", \“ID\": \"4133\", \“Name\": \“Test\", };


 // Building request using requestbuilder
 RequestSpecBuilder builder = new RequestSpecBuilder();
 builder.setBody(APIBody);

 // setting contenttype as appl/json


 builder.setContentType("application/json; charset= UTF-8");

 RequestSpecification requestSpec = builder.build();

 requestSpec.post(APIUrl).getStatusCode();
Post: method 3

 Response response = given().header("Authorization",


KEY).contentType("application/json")
 .param(“Country”, “MY”).param(“Name”,
“Test”).when().post(url);

 Int status= response.then().getStatuscode();

 String message =
response.then().body()containsString(“Success”);

 Assert status code: 201 & Success message


Authentication methods

 1. given().auth().basic(username, password).

 2. given().header(“Authentication”, Key)

You might also like