Introduction
As a part of the End to End REST Assured Tutorial, in this post, we will learn to pass data from one API to another API.
Data is key in API testing and we may require to pass the output of one API as an input to another API. For example:- We get a booking id from Create Booking API. If we need to retrieve, update, partial update, or delete booking then we must need to pass the booking id to these APIs.
We will use TestNG as a testing framework so that we can easily share data among test methods.
I have already covered Sharing Data Among Test Methods In TestNG Using ITestContext. In this post, I will explain the same concept with respect to Rest Assured.
Required Dependencies
I have used Rest Assured and TestNG of the below versions:-
org.testng testng 7.3.0 test
io.rest-assured rest-assured 4.3.3 test
Interface ITestContext
As per TestNG Javadoc, ITestContext is an interface that defines a test context that contains all the information for a given test run. An instance of this context is passed to the test listeners so they can query information about their environment.
ITestContext is a powerful interface that provides many useful methods. In this post, we will see two important methods setAttribute(java.lang.String name, java.lang.Object value) and getAttribute(java.lang.String name) provided by ITestContext interface.
setAttribute()
setAttribute(atttributeName, attributeValue) – Set a custom attribute. It is similar to adding an element in a Map as key-value pair. Kindly pay attention here that attribute value can be of any type. This is the reason this method accepts Object type as a value.
getAttribute()
getAttribute(attributeName) – Get the value of given attribute name. Remember return type is an Object.
How to use ITestContext?
ITestContext interface extends IAttributes interface. Instead of going theoretically, let’s learn it using examples. It will make more sense to you.
ITestContext reference is created once and can be used with the @Test annotated method by just passing it as a parameter. Whatever data you would like to use in other tests you just need to store them in Test Context using setAttribute() method. To retrieve stored data use getAttribute() method.
We will use Restful – Booker APIs for demo purposes. I have already covered many examples of these APIs in my RestAssured series.
Example Program
package SharingData; import org.testng.ITestContext; import org.testng.annotations.Test; import io.restassured.RestAssured; import io.restassured.http.ContentType; public class ShareDataUsingITestContext { @Test public void createBooking(ITestContext context) { int bookingId = RestAssured .given() .log() .all() .baseUri("https://restful-booker.herokuapp.com/") .basePath("booking") .contentType(ContentType.JSON) .body("{\r\n" + " \"firstname\" : \"Jim\",\r\n" + " \"lastname\" : \"Brown\",\r\n" + " \"totalprice\" : 111,\r\n" + " \"depositpaid\" : true,\r\n" + " \"bookingdates\" : {\r\n" + " \"checkin\" : \"2018-01-01\",\r\n" + " \"checkout\" : \"2019-01-01\"\r\n" + " },\r\n" + " \"additionalneeds\" : \"Breakfast\"\r\n" + "}") .when() .post() .then() .log() .all() .extract() .jsonPath() .get("bookingid"); // Storing data in a context to use for other tests context.setAttribute("bookingId", bookingId); } @Test public void updateBooking(ITestContext context) { // Retrieving required data from context int bookingId = (int) context.getAttribute("bookingId"); RestAssured .given() .log() .all() .baseUri("https://restful-booker.herokuapp.com/") .basePath("booking/"+bookingId) .header("Authorization","Basic YWRtaW46cGFzc3dvcmQxMjM=") .contentType(ContentType.JSON) .body("{\r\n" + " \"firstname\" : \"Amod\",\r\n" + " \"lastname\" : \"Mahajan\",\r\n" + " \"totalprice\" : 222,\r\n" + " \"depositpaid\" : true,\r\n" + " \"bookingdates\" : {\r\n" + " \"checkin\" : \"2022-01-01\",\r\n" + " \"checkout\" : \"2022-01-01\"\r\n" + " },\r\n" + " \"additionalneeds\" : \"Breakfast\"\r\n" + "}") .when() .put() .then() .log() .all(); } }
Output
[RemoteTestNG] detected TestNG version 7.0.1 Request method: POST Request URI: https://restful-booker.herokuapp.com/booking Proxy:Request params: Query params: Form params: Path params: Headers: Accept=*/* Content-Type=application/json; charset=UTF-8 Cookies: Multiparts: Body: { "firstname": "Jim", "lastname": "Brown", "totalprice": 111, "depositpaid": true, "bookingdates": { "checkin": "2018-01-01", "checkout": "2019-01-01" }, "additionalneeds": "Breakfast" } HTTP/1.1 200 OK Server: Cowboy Connection: keep-alive X-Powered-By: Express Content-Type: application/json; charset=utf-8 Content-Length: 195 Etag: W/"c3-mr+ToVU3dK7bNqxVpsnhD1SC7cM" Date: Wed, 27 Jan 2021 06:23:23 GMT Via: 1.1 vegur { "bookingid": 11, "booking": { "firstname": "Jim", "lastname": "Brown", "totalprice": 111, "depositpaid": true, "bookingdates": { "checkin": "2018-01-01", "checkout": "2019-01-01" }, "additionalneeds": "Breakfast" } } Request method: PUT Request URI: https://restful-booker.herokuapp.com/booking/11 Proxy: Request params: Query params: Form params: Path params: Headers: Authorization=Basic YWRtaW46cGFzc3dvcmQxMjM= Accept=*/* Content-Type=application/json; charset=UTF-8 Cookies: Multiparts: Body: { "firstname": "Amod", "lastname": "Mahajan", "totalprice": 222, "depositpaid": true, "bookingdates": { "checkin": "2022-01-01", "checkout": "2022-01-01" }, "additionalneeds": "Breakfast" } HTTP/1.1 200 OK Server: Cowboy Connection: keep-alive X-Powered-By: Express Content-Type: application/json; charset=utf-8 Content-Length: 171 Etag: W/"ab-+iqLT0fOvVL3GfV0ed6NlH849m8" Date: Wed, 27 Jan 2021 06:23:25 GMT Via: 1.1 vegur { "firstname": "Amod", "lastname": "Mahajan", "totalprice": 222, "depositpaid": true, "bookingdates": { "checkin": "2022-01-01", "checkout": "2022-01-01" }, "additionalneeds": "Breakfast" } PASSED: createBooking(org.testng.TestRunner@1990a65e) PASSED: updateBooking(org.testng.TestRunner@1990a65e) =============================================== Default test Tests run: 2, Failures: 0, Skips: 0 =============================================== =============================================== Default suite Total tests run: 2, Passes: 2, Failures: 0, Skips: 0 ===============================================
You can download/clone the above sample project from here.
You can subscribe to my YouTube channel RetargetCommon to learn from video tutorials.
If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappyLearning
Find all Selenium related posts here, all API manual and automation related posts here, and find frequently asked Java Programs here.
Many other topics you can navigate through the menu.