You are on page 1of 28

API Testing Workshop

Manjyot Singh
Ruchika Rawat
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
Request Response

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");
contd...

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);
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

You might also like