Rest Assured Flashcards

1
Q

REST Assured

A

(DSL) API used for writing automated tests for RESTful APIs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Advantages of using REST Assured

A

It provides a lot of helper methods and abstraction layers that remove the need for writing a lot of boilerplate code for connections, sending a request, and parsing a response.

The DSL provided by it, increases the readability of the code.

There is a seamless integration with testing frameworks like Junit or TestNG and other continuous integration tools.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

JsonPath and XmlPath

A

JsonPath and XmlPath are the query languages which help to extract (parse server response for validation) data from the JSON and XML document

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Sample Maven dependency

A

<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>4.3.0</version>
</dependency>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

a DSL expression request using the Given/When/Then

A

given()
when()
then()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

given()

A

used in building the DSL expression request with any additional information like

headers,
params,
message body,
authentication, etc.,

before making any HTTP Request

RestAssured.given()
.header(“header1”,”value1”)
.header(“header2”, “value2”)
.param(“param1”,”paramValue”)
.body(body)
.post(url);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

example, no extra information is added to the request builder before making a GET request.

A

RestAssured.given()
.get(url);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

when()

A

when() can be used with given() or independently in the DSL expression.

RestAssured.given()
.param(“firstName”, “John”)
.param(“lastName”, “Doe”)
.when()
.get(“/greet”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

when() is used with given() in the DSL expression to pass some parameters with the request.

A

RestAssured.given()
.param(“firstName”, “John”)
.param(“lastName”, “Doe”)
.when()
.get(“/greet”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

then()

A

It is always used with either given(), when(), or with both methods in the DSL expression. It returns a validatable response.

RestAssured.given().
.param(“firstName”, “John”)
.param(“lastName”, “Doe”)
.when()
.get(“/greet”)
.then()
.body(“greeting”, equalTo(“John Doe”));

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Example 1 – Fetch all student records

A

import static org.hamcrest.Matchers.equalTo;
import org.testng.annotations.Test;
import static org.testng.Assert.*
import io.restassured.response.Response;
import io.restassured.RestAssured;

String url = “http://ezifyautomationlabs.com:6565/educative-rest/students”;

Response response = RestAssured.given().get(url).andReturn();
response.getBody().prettyPrint();
assertEquals(response.getStatusCode(), 200, “http status code”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

makes a GET request and returns a Response object

A

Response response = RestAssured.given().get(url).andReturn();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

prints the response body in the JSON format

A

response.getBody().prettyPrint();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

asserts the response status code as 200

A

assertTrue(response.getStatusCode()==200);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

gets the list of all Student Ids from the response body and assert that one of the Id is 101

A

assertTrue(response.getBody().jsonPath().getList(“id”).contains(101));

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

makes a GET request using {id} in the Path param to get the Response from the server.

then verifies that the response’s body contains the requested {id}

A

String url1 = url + “/” + “101”;
Response response1 = RestAssured.given()
.get(url1)
.andReturn();
LOG.info(response1.getStatusLine());
assertTrue(response1.getStatusCode()==200);
Long id = response1.getBody().jsonPath().getLong(“id”);
assertTrue(id==101);

17
Q

GET request using Query Params like Student’s gender to get the matching Student’s data from the server.

A

Response response = RestAssured
.given()
.queryParam(“gender”, “male”)
.get(url).andReturn();

response.getBody().prettyPrint();

18
Q

the request message body as a String

A

String body = “{"first_name": "Jack", "last_name": "Preacher", "gender": "Male" }”;

19
Q

makes a POST Request with accept and content-type headers along with a message body and returns a Response object

A

Response response = RestAssured.given()
.header(“accept”, “application/json”)
.header(“content-type”, “application/json”)
.body(body)
.post(url)
.andReturn();

20
Q

logs the response body in JSON format and assert response status code as 201

A

response.getBody().prettyPrint();
assertTrue(response.getStatusCode()==201);

21
Q

the request message body as a Java object (Student class object)

A

Student body = new Student(“Katherine”, “AK”, “Female”);

.body(body)

22
Q

makes a POST Request using post(url) method with accept and content-type headers using header(key,value) method along with message body using body(body) method and returns a Response object using andReturn()

A

Response response = RestAssured.given()
.header(“accept”, “application/json”)
.header(“content-type”, “application/json”)
.body(body)
.post(url)
.andReturn();

23
Q

a Student class that has a few fields annotated with @JsonProperty; a marker annotation to define logical property to be used in serialization and deserialization of JSON.

A

class Student {

public Student(String firstName, String lastName, String gender) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.gender = gender;
}

@JsonProperty("id")
Long id;

@JsonProperty("first_name")
String firstName;

@JsonProperty("last_name")
String lastName;

@JsonProperty("gender")
String gender; }

Student bodyUpdate = new Student(“John”, “LP”, “Male”);

24
Q

makes a PUT request using {id} in the path param to update the data of the particular Student. We are also sending headers and a new message body in the request, which returns a Response object using the andReturn() method.

A

Student bodyUpdate = new Student(“John”, “LP”, “Male”);
bodyUpdate.id = Long.parseLong(id);
String url1 = url+ “/”+id;
Response response1 = RestAssured.given()
.header(“accept”, “application/json”)
.header(“content-type”, “application/json”)
.body(bodyUpdate)
.put(url1)
.andReturn();

25
Q

verifies that the Student’s record is updated successfully

A

LOG.info(“Step - 3 : Print the response message and assert the status”);
response1.getBody().prettyPrint();
LOG.info(“Status “ +response.getStatusCode());
assertTrue(response.getStatusCode()==201);

26
Q

gets the created Student id and Delete that Student’s record using delete(url1) method and returns the Response object using andReturn() method

verifies that the Student’s record is deleted and the Response body is empty. response1.getBody().prettyPrint().isEmpty() and the response status code is 204

A

String id = response.getBody().jsonPath().getString(“id”);
LOG.info(“Get the created Student ID: “ + id);

LOG.info(“Step - 2 : Delete the created record. [DELETE ]”);
String url1 = url + “/” + id;
Response response1 = RestAssured.given().delete(url1).andReturn();

LOG.info(“Step - 3 : Print the response message and assert the status”);
LOG.info(“Response Status Code: “ + response.getStatusCode());
assertTrue(response1.getBody().prettyPrint().isEmpty());
assertTrue(response1.getStatusCode()==204);
LOG.info(“Student with id: “ +id+ “ is deleted”);