As a part of TestNG Tutorials, in this post we will see “How to share data or state among test in a suite?”.
We have covered below topics as of now:-
Sharing data between @Test annotated method using static variable concept
Sharing data between @Test annotated method using ITestContext
Let’s understand what actually I want to cover in this post. I will create two TestNG classes. In TestNG XML, I will create two tests and will add one TestNG class to each test. I would like to use data created in first test into second test. We will use concept of ITestContext. See example below:-
First TestNG Class:-
package TestNGExamples; import org.testng.ITestContext; import org.testng.annotations.Test; public class SharingDataSuiteLevelFirstTest { // 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); } }
Second TestNG Class:-
package TestNGExamples; import org.testng.ITestContext; import org.testng.annotations.Test; public class SharingDataSuiteLevelSecondTest { // Passing ITestContext as a parameter to @Test method to use it. @Test public void useData(ITestContext context) { String lastName = "Mahajan"; // Retrieving attribute value set in ITestContext String FN = (String) context.getAttribute("FirstName"); String fullName = FN +" "+lastName; System.out.println("Full Name is : "+fullName); } }
TestNG XML ( Note here that I have created two tests) :-
Output:-
Full Name is : null Mahajan
Observe output now. We were expecting output as “Amod Mahajan” but it printed “null Mahajan”. Actually scope of added variable in ITestContext is within current test. Outside of test, it will give you null.
What is solution of above problem?
Solution is again ITestContext but this time we need to store data at suite level in stead of a test level. ITestContext interface provides a method named getSuite() which returns a Suite object that was passed to the runner at start-up i.e. ISuite. Since ISuite interface extends IAttribute interface, you get setAttribute() and getAttribute() methods. So we need to get suite first and then set and get values.
Modified TestNG classes are as below:-
Modified first TestNG class:-
package TestNGExamples; import org.testng.ISuite; import org.testng.ITestContext; import org.testng.annotations.Test; public class SharingDataSuiteLevelISuiteFirstTest { // Passing ITestContext as a parameter to @Test method to use it. @Test public void generateData(ITestContext context) { String firstName = "Amod"; // Get the suite ISuite suite = context.getSuite(); // Setting an attribute with name and its value at suite level in stead of test level suite.setAttribute("FirstName", firstName); } }
Modified second TestNG class:-
package TestNGExamples; import org.testng.ISuite; import org.testng.ITestContext; import org.testng.annotations.Test; public class SharingDataSuiteLevelISuiteSecondTest { // Passing ITestContext as a parameter to @Test method to use it. @Test public void useData(ITestContext context) { String lastName = "Mahajan"; // Retrieving attribute value set in ITestContext ISuite suite = context.getSuite(); String FN = (String) suite.getAttribute("FirstName"); String fullName = FN +" "+lastName; System.out.println("Full Name is : "+fullName); } }
TestNG XML:-
Output:-
Full Name is : Amod Mahajan
Note:-
- You can not pass ISuite reference directly to @Test annotated method like ITestContext. If you do so, you get a TestNG exception stating Cannot inject @Test annotated Method [generateData] with [interface org.testng.ISuite].
- You can store attributes at test and suite level together as required.
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.
Thank you very much. This was so helpful.
Thanks for this tutorial. Is there a possibility to pass the data between suite files? Say if I have a ‘Suite A’ running a few tests and ‘Suite B’ wants to use the data from ‘Suite A’, would it be possible?