• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

SoftwareTestingo - Jira Selenium Protractor Testing SDLC Agile Methodology

Java Selenium Tutorial & Testing Interview Questions

  • Home
  • Interview Questions
  • Java
  • Java Programs
  • Test Cases
  • Selenium
  • Manual Testing
  • Difference
  • Search
SoftwareTestingo » Selenium » Selenium Tutorial » ITestContext TestNG Interface

ITestContext TestNG Interface

Last Updated on: November 30, 2019 By Softwaretestingo Editorial Board

What We Are Learn On This Post

  • ITestContext TestNG Interface

ITestContext TestNG Interface: This post, we are going to discuss one crucial ITestContext TestNG Interface in detail. Some times 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 ITestContext TestNG Interface.

So ITestContext is used to store and share data across the tests in selenium by using the TestNG framework.

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 the duration of 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 with TestNG:

Suppose in a Class we have 10 Test Cases or @Test method, which are covering the end to end scenario. Now all 10 Test cases are sharing 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 present in a class, then we can create a Class level variable and share it. But it requires high maintenance.
  • Use ITestContext

Let us understand how we can handle such 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 as per our requirement like below:

String Customer_id1 = (String) context.getAttribute(“CustID”);

Sample TestNG Class:

package iTestContextLearn;

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

public class Test1 
{

   @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("+++++++++++++++++++++++++++++++++++++++++++++++++");

   }
}

Note: All the @Test methods that we are going to run must be in one class only.

Source: link

    Filed Under: Selenium Tutorial

    Reader Interactions

    Leave a Reply Cancel reply

    Your email address will not be published. Required fields are marked *

    Primary Sidebar

    Join SoftwareTestingo Telegram Group

    Tutorials Important Links

    • Software Testing Jobs
    • Manual Testing Tutorial
    • Selenium Tutorial
    • Core Java Tutorial
    • TestNG Tutorial
    • Java Programs
    • Selenium Programs
    • Manual Test Cases
    • Interview Tips
    • Contact US
    • www.softwaretestingo.com

    Important Links

    • Software Testing Interview Questions
    • Agile Interview Questions
    • Manual Testing Interview Questions
    • Testing Interview Questions
    • Selenium Interview Questions
    • Selenium Real Time Interview Questions
    • Selenium WebDriver Interview Questions
    • Automation Framework Interview Questions
    • Appium Interview Questions
    • TestNG Interview Questions
    • Java Interview Questions
    • Core Java Interview Questions

    Categories

    Copyright © 2021 SoftwareTestingo.com ~ Contact Us ~ Sitemap ~ Privacy Policy