ITestContext TestNG Interface

ITestContext TestNG Interface: In this post, we will discuss one crucial ITestContext TestNG Interface in detail. Sometimes, in the test script, we need to share the objects with different test cases during the execution. To handle such scenarios, we can take the help of the ITestContext TestNG Interface.

So, using the TestNG framework, ITestContext stores and shares data across the tests in Selenium.

TestNG offers a means of storing and retrieving objects between tests through the ITestContext interface. This interface allows you to store using the inherited setAttribute() method and retrieve using getAttribute() objects.

ITestContext TestNG Interface

Since the ITestContext is created once and remains active for your test run, this is the perfect way to implement object sharing in your test suite. Making the ITestContext available in your test methods is easy: pass it as a parameter to your test method.

Let’s Go through one scenario and try to understand how you can use TestNG:

Suppose we have 10 Test Cases or the 10 @Test method in a Class, which covers the end-to-end scenario. Now, all 10 Test cases share some data, for example, Customer_id, which is unique, and the same value should be used for the end-to-end scenario.

We Can handle this in 2 ways:

  • If all the test cases are in a class, we can create and share a Class-level variable. But it requires high maintenance.
  • Use ITestContext

Let us understand how we can handle such a scenario using ITestContext:

As we mentioned above, we can use the ITestContext by passing as a parameter to any test method like below:

@Test
public void test1a(ITestContext context)
{

}

You can set the value for ITestContext TestNG Interface by using the setAttribute() method like below:

@Test
public void test1a(ITestContext context)
{
   String Customer_id = "C11012034";
   context.setAttribute("CustID", Customer_id);
}

Now we need to retrieve the value from the ITestContext by using the getAttribute() method and use it as per our requirement like below:

String Customer_id1 = (String) context.getAttribute("CustID");

Sample TestNG Class:

package com.softwaretestingo.testng.itestcontext;
import org.testng.ITestContext;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class ITestContextTest 
{
	@BeforeTest
	public void SetData(ITestContext context)
	{
		String Customer_id = "C11012034";
		context.setAttribute("CustID", Customer_id);
		System.out.println("Value is stored in ITestContext");
		System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++");
	}
	@Test
	public void Test1a(ITestContext context)
	{
		String Customer_id1 = (String) context.getAttribute("CustID");
		System.out.println("In Test1, Value stored in context is: "+Customer_id1);
		System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++");
	}
	@Test
	public void Test2a(ITestContext context)
	{
		String Customer_id1 = (String) context.getAttribute("CustID");
		System.out.println("In Test2, Value stored in context is: "+Customer_id1);
		System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++");
	}
}

Output:

Value is stored in ITestContext
+++++++++++++++++++++++++++++++++++++++++++++++++
In Test1, Value stored in context is: C11012034
+++++++++++++++++++++++++++++++++++++++++++++++++
In Test2, Value stored in context is: C11012034
+++++++++++++++++++++++++++++++++++++++++++++++++
===============================================
    Default test
    Tests run: 2, Failures: 0, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 2, Passes: 2, Failures: 0, Skips: 0
===============================================

Note: All the @Test methods we will run must be in one class only.

Avatar for Softwaretestingo Editorial Board

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

Leave a Comment