Make Selenium Easy

TestNG Tutorials 67 : Sharing Data Among Test Methods in TestNG Using ITestContext

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.

Table of Contents

Author: Amod Mahajan

A software Tester who is paid to judge products developed by others. Currently getting paid in American Dollars. Writing technical posts and creating YouTube videos are my hobbies.

1 thought on “TestNG Tutorials 67 : Sharing Data Among Test Methods in TestNG Using ITestContext

  1. ITestContext is an interface and it is implement by TestRunner,but in code we does not discuss on TestRunner class how it works or internal working

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.