Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

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

Posted on 02/08/2025 By admin

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.

package TestNGExamples;

import org.testng.ITestContext;
import org.testng.annotations.Test;

public class SharingStateBetweenTestsUsingITestContext {
        // ITestContext reference is created once for the duration of test run.
        // Passing ITestContext as a parameter to @Test method to use it. 
        @Test
        public void generateData(ITestContext context)
        {
                String firstName = "Amod";
                // Setting an attribute with name and its value
                context.setAttribute("FirstName", firstName);
        }
        
        
        @Test
        public void useData(ITestContext context)
        {
                String lastName = "Mahajan";
                context.setAttribute("LastName", lastName);
                // Retrieving attribute value set in ITestContext
                String FN = (String) context.getAttribute("FirstName");
                String fullName = FN +" "+lastName;
                System.out.println("Full Name is : "+fullName);
                context.setAttribute("FullName", fullName);
        }

}

Output:-

Full Name is : Amod Mahajan

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

package TestNGExamples;

import org.testng.ITestContext;
import org.testng.annotations.Test;

public class SharingStateBetweenTestsUsingITestContext2 {
        
        @Test
        public void useDataInOtherClass(ITestContext context)
        {
                String profession = "Blogger";
                String fullName = (String) context.getAttribute("FullName");
                String nameWithProfession = fullName +" "+profession;
                System.out.println("Full Name is : "+fullName);
                System.out.println("Full Name with profession is "+ nameWithProfession);
        }

}

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



        

Output:-

Full Name is : Amod Mahajan
Full Name is : Amod Mahajan
Full Name with profession is Amod Mahajan Blogger

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

Uncategorized

Post navigation

Previous Post: Frequently Asked Java Programs: 33 – Insert a String into Another String at given index
Next Post: Part 5: Waits In Selenium: What Happens When We Mix Sleep With Other Types Of Waits ?

Related Posts

SelectedGroup – Make Selenium Easy Uncategorized
Using Implicit Wait in Selenium WebDriver Uncategorized
Introduction Of Page Object Model In Selenium WebDriver: Advantages And disadvatages Uncategorized
Cucumber Tutorial 1 – Setup a Basic Maven-Cucumber 5 -Junit Project in Eclipse Uncategorized
Git Tutorial 3 – Install GIT On Windows – Step By Step Uncategorized
Using Explicit Wait As WebDriverWait in Selenium WebDriver Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark