As a part of TestNG Tutorials, in this post we will see “How to share data or state among test methods in TestNG using ITestContext interface?”.

In previous post, We have learnt sharing data among test methods using static variables. But this is not optimal. I see below problem in using static variable approach:-

  1. You may need to create multiple static variables.
  2. Dynamic handling of variables is not easy.
  3. Problems in parallel execution
  4. Scope of variables

All above problems can be solved using ITestContext interface. As per TestNG Javadoc, ITestContext interface defines a test context which 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 which 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).

setAttribute(atttributeName, attributeValue) :- Set a custom attribute. It is similar to adding at 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 value.

getAttribute(attributeName) :- Get the value of given attribute name. Remember return type is an Object.

Above two methods belong to IAttributes interface. ITestContext interface extends IAttributes interface. In stead of going theoretically, let’s learn it using example. It will make more sense to you.

ITestContext reference is created once and can be used with @Test annotated method by just passing it as a parameter.

Output:-

You can see there is no need to create any variable in advanced. ITestContext gives a container in which we can store labeled values and use wherever required. It works fine as well when we need to use these variables in other TestNG class as below:-

Since we need to run multiple TestNG classes together, create a XML as below:-

Output:-

Only constraint is that you need to pass ITestContext reference in @Test annotated method if you want to use to store or retrieve variables. No changes are required in style of running TestNG class if you use ITestContext.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappyLearning

You can find all Selenium related post here.
You can find all API manual and automation related posts here.
You can find frequently asked Java Programs here.

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