pavan raju
pavan raju

Reputation: 103

Get Suite Name at Runtime in TestNG

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?

Upvotes: 8

Views: 18335

Answers (5)

user1787641
user1787641

Reputation: 331

In onStart(), you can get the suite name of the current running suite, you can use loggers or sysouts to print.

public void onStart(ISuite suite) {
    suite.getName();
}

Upvotes: 0

Garima
Garima

Reputation: 1

This can also be used to get the test class name with test method name.

public static String generateFileName (ITestResult result, ITestContext ctx)
{
    String[] suiteName = result.getMethod().toString().split("\\[");

    String filename = "Script_" + suiteName[0] + "_" + dateFormat.format(new Date());
    return filename;
}

Upvotes: 0

Garima
Garima

Reputation: 1

public static String generateFileName (ITestResult result, ITestContext ctx)
{
    String suiteName = result.getTestClass().getName();
    //String suiteName = ctx.getCurrentXmlTest().getClasses().get(0).getName(); this is removed since the index after class was not getting updated to hold current test name
    String filename = "Script_" + suiteName + "_" + dateFormat.format(new Date());
    return filename;
}

This will give you current class name when there is multiple test ng execution. The ctx.getCurrentXmlTest().getClasses().get(0).getName() will not give you the current class name as the index has to be updated.

Upvotes: -1

Sandeep Chaudhary
Sandeep Chaudhary

Reputation: 11

You can read it from ITestContext

public static String getSuiteName(ITestContext context){
    return context.getCurrentXmlTest().getSuite().getName();
}

Upvotes: 1

Cedric Beust
Cedric Beust

Reputation: 15608

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();

Upvotes: 20

Related Questions