How can I get the current running suite name at run time in my test case? I am using the piece of code shown below for getting the current suite name.
Listeners class:
public class SuiteListener implements ISuiteListener{
private static ThreadLocal<ISuite> ACCESS = new ThreadLocal<ISuite>();
public static ISuite getAccess() {
return ACCESS.get();
}
@Override
public void onFinish(ISuite suite) {
ACCESS.set(null);
}
@Override
public void onStart(ISuite arg0) {
ACCESS.set(arg0);
}
}
Test Class:
@Listeners({ SuiteListener.class })
public class Practise {
@DataProvider(name = "getXlsPath")
public Object[][] createData() throws Exception {
String [][] testCasesPaths=null;
ISuite suiteListner = SuiteListener.getAccess();
String runningSuite=suiteListner.getName();
System.out.println(runningSuite);
testCasesPaths[0][0]="1.xls";
testCasesPaths[1][0]="2.xls";
return testCasesPaths;
}
@Test(dataProvider="getXlsPath")
public void test2(String xlsPath){
System.out.println(xlsPath);
}
}
Testng xml:
<suite name="tables" >
<test name="vendor" >
<classes>
<class name="Practise" ></class>
</classes>
</test>
The code works perfectly until I specify the parallel="tests" attribute in the suite:
<suite name="tables" parallel="tests" >
In this case I could not get the suite name--it's not executing. Can someone help me on this, to get the suite name?
You can access the Xml file and the current Xml test from the ITestContext, which can be injected in test methods:
@Test
public void f(ITestContext ctx) {
String suiteName = ctx.getCurrentXmlTest().getXmlSuite().getName();
You can read it from ITestContext
public static String getSuiteName(ITestContext context){
return context.getCurrentXmlTest().getSuite().getName();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With