Manjyot Singh
Ruchika Rawat
API Testing Workshop
Introduction
[
{
"speaker": {
"id": "007",
"name": "Manjyot Singh",
"role": "QA"
}
},
{
"speaker": {
"id": "001",
"name": "Ruchika Rawat",
"role": "QA"
}
}
]
What is a web service – QA point of view?
A method of communication between two web applications
Let’s play a video...
Example
Classifications
REST Vs SOAP
???
SOAP
â—Ź Simple object access protocol.
â—Ź Used for exchange of information on distributed platform using XML.
â—Ź Works mainly with HTTP, HTTPS.
â—Ź HTTP makes SOAP go around firewalls.
â—Ź Slower when using large XML messages.
???
REST
â—Ź Representational State Transfer.
â—Ź REST architectural style, data and functionality are considered
resources and are accessed using Uniform Resource Identifiers
(URIs).
â—Ź Resources are manipulated using a fixed set of four create, read,
update, delete operations: PUT, GET, POST, and DELETE.
â—Ź Formats - XML, plain text, PDF and JSON.
REST - Example
ResponseRequest
http://localhost:9000/users/1
Let’s talk about...
Why REST-assured ?
â—Ź Java Domain Specific Language (DSL) for testing web services
â—Ź Built on top of HTTPBuilder
â—Ź Supports response parsing
â—Ź Supports in-built assertions
â—Ź Supports BDD syntax
Setup (Requirement)
â—Ź Install JDK
â—Ź IDE (Eclipse/Intellij)
â—Ź Build Tool (gradle) *optional
â—Ź Rest-assured jars
â—Ź Hamcrest-matchers jars
â—Ź Junit jars
Understanding a Request
Simple GET Request
given().
contentType(“application/json”).
when().
get("/users").
then().
assertThat().
statusCode(HttpStatus.SC_OK);
GET Request
given().
contentType(“application/json”).
when().
get("/users/1").
then().
assertThat().
body("userId", equalTo(1)).
body("userName", equalTo("Robert")).
body("employer", equalTo("facebook")).
body("location.state", equalTo("California")).
body("location.city", equalTo("San Jose"));
POST Request
given().
contentType("application/json").
body("[{"userName":"Jayant2","employer":"Google","location":{"
state":"California","city":"Mountain View"}}]").
when().
post("/users").
then().
assertThat().
body("userName", hasItems("Jayant2"));
PUT Request
int userId = 1;
given().
contentType("application/json").
when().
body("{"userName":"Taylor"}").
put("/users/" + userId).
then().
statusCode(HttpStatus.SC_OK).
body("userName", equalTo("Taylor"));
DELETE Request
int userId = 9;
given().
when().
delete("/users/" + userId).
then().
statusCode(HttpStatus.SC_OK);
Response parsing
Response response =
given().
contentType(ContentType.JSON).
when().
get("/users/5").
then().
extract().response();
String userName = response.path("userName");
String userCity = response.path("location.city");
Assert.assertTrue(userName.equals("Steve"));
Assert.assertTrue(userCity.equals("San Francisco"));
Json parsing
String jsonResponse =
get("/users/5").
asString();
JsonPath jsonPath = new JsonPath(json).setRoot("location");
String state = jsonPath.getString("state");
String city = jsonPath.getString("city");
Assert.assertTrue(state.equals("California"));
Assert.assertTrue(city.equals("San Francisco"));
Authentication
String sessionToken =
given().
body("{"userName" : "ruchikar","password" : "P@ssW0rd"}").
when().
with().
header("Content-Type", "application/json").
header("X-Forwarded-Proto", "https").
post("/sessionTokens").
then().
statusCode(200).
contentType(ContentType.JSON).
extract().
response().path("response.sessionToken");
given().
when().
with().
header("X-Forwarded-Proto", "https").
header("Content-Type", "application/json").
header("X-Auth", sessionToken).
get(“/users”).
then().
statusCode(HttpStatus.SC_OK).
contentType(ContentType.JSON);
contd...
Other available tools/api
References
Rest-Assured: https://github.com/jayway/rest-assured
Github : https://github.com/jayway/rest-assured/wiki/Usage
Workshop Test framework: https://github.com/ruchikar/RestAssuredTest
Workshop WebService: https://github.com/syedatifakhtar/VodQABomb
Questions

Web_service_testing_finall