Make Selenium Easy

REST Assured Tutorial 64 – How to pass value from one API to Another API using TestNG – ITestContext

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:-

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

Output

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.

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 6+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went through so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.

REST Assured Tutorial 64 – How to pass value from one API to Another API using TestNG – ITestContext

by Amod Mahajan time to read: 4 min
0
%d bloggers like this: