0% found this document useful (0 votes)
56 views7 pages

Rest Assured API Automation 1737266358

This document provides a step-by-step guide to building a simple API using Rest Assured in a Maven project. It covers setting up the project, sending GET requests, validating response status codes and body, and including custom headers in requests. The document includes code snippets for each step to facilitate understanding and implementation.

Uploaded by

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

Rest Assured API Automation 1737266358

This document provides a step-by-step guide to building a simple API using Rest Assured in a Maven project. It covers setting up the project, sending GET requests, validating response status codes and body, and including custom headers in requests. The document includes code snippets for each step to facilitate understanding and implementation.

Uploaded by

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

Build Your First API

Automation Demo
Script with Rest
Assured in Just
15 Minutes
Create Maven Project and Add Dependency

• In this section, you will set up a Maven project to manage dependencies. Open your
preferred IDE and create a new Maven project. Add the Rest Assured & TestNG
dependency to your pom.xml file.

• <!-- https://mvnrepository.com/artifact/io.rest-assured/rest-assured -->


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

<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.0.0</version>
</dependency>
Write Script to Send GET Request & Print response
Create a new class in the src/test directory. Write a script to send a GET request to the 'reqres.in' API.

import io.restassured.RestAssured;
import io.restassured.response.Response;
import org.testng.annotations.Test;

public class APIDemo {

@Test
public void sendGETRequest() {
// Specify the base URI
RestAssured.baseURI = "https://reqres.in/api";

// Send GET request


Response response = RestAssured.given()
.when()
.get("/users/1");

// Print response body


System.out.println(response.getBody().asString());
}
}
Validate the Response Status Code

Extend your script to validate the response status code.


import static org.testng.Assert.assertEquals;

@Test
public void validateStatusCode() {
// ... (previous code)

// Validate status code


int statusCode = response.getStatusCode();
assertEquals(statusCode, 200, "Unexpected status code");
}
Validate the Response Body

Enhance your script to validate specific elements in the response body.

import static org.testng.Assert.assertTrue;

@Test
public void validateResponseBody() {
// ... (previous code)

// Validate response body


String email = response.path("data.email");
assertTrue(email.endsWith("@reqres.in"), "Unexpected email address");
}
Send Headers with the Request

Modify your script to include custom headers in the GET request.

@Test
public void sendGETRequestWithHeaders() {
// ... (previous code)

// Send GET request with headers


Response responseWithHeaders = RestAssured.given()
.header("Content-Type", "application/json")
.header("Authorization", "Bearer your_token")
.when()
.get("/users/1");

// Print response body with headers


System.out.println(responseWithHeaders.getBody().asString());
}
Thank You

You might also like