简体   繁体   中英

How to get class name at run time in testng

I am having @BeforeClass annotated method in BaseClass. I am trying to get the name of current execution class(Child class to BaseClass) at runtime .

Do we have any mechanism to get the class name from its Baseclass at runtime?

I am able to get the class name when I am trying to run single class, but facing issue while running multiple classes. Getting same class name for all the classes in the report.

You can refer this link How can I get the classname at runtime, but only the classname?

If you want to get super class name you can use getSuperClass().

You can make use of TestNg ITestContext object here:

Class x = ctx.getSuite().getAllMethods().listIterator.next.getRealClass();
//Then just take it as a String

String name = x.getName().substring(x.getName().lastIndexOf(“.”)+1);
public String getClassName(ITestContext iTestContext) {

    return iTestContext.getCurrentXmlTest().getClasses().stream()
               .findFirst().get().getName();
}

Or you could retrieve the class itself by:

iTestContext.getAllTestMethods()[0].getInstance().getClass();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM