0
有没有办法从不通过xml文件执行时获取脚本从@BeforeSuite注释中启动的类的名称?在@BeforeSuite注解中获取类名称 - TestNG - Java
这样做:
reportName = new Exception().getStackTrace()[0].getClassName();
返回包含@BeforeSuite注释和这个类本身:
reportName = new Exception().getStackTrace()[1].getClassName();
回报sun.reflect.NativeMethodAccessorlmpl
如果我直接执行脚本从一个单独的课程中,我想获取该信息,因为我正在使用它来命名我的Extent报告文件名。
如果你想知道的@BeforeSuite注释内部的代码如下所示:
// Set Extent Report file name from the global properties file
String reportName = ctx.getCurrentXmlTest().getSuite().getName();
if (reportName.equals("Default suite"))
{
reportName = new Exception().getStackTrace()[0].getClassName();
}
String timeStamp = new SimpleDateFormat("HH.mm.ss").format(new Date());
// Initialize Extent Reports and modify the looks/output
String extentReportPath = "";
if (extent == null) {
if (os.equals("Mac"))
{
extentReportPath = reportPath + "/" + project + "-" + reportName + "-" + environment + "-" + browser + "-" + timeStamp + ".html";
}
else if (os.equals("Windows"))
{
extentReportPath = reportPath + "\\" + project + "-" + reportName + "-" + environment + "-" + browser + "-" + timeStamp + ".html";
}
// Start new report
extent = new ExtentReports(extentReportPath, true);
}
还有更多的它,但这是我的问题的部分相关。
--- UPDATE ---
这是我的解决方案:
// Set Extent Report file name from the global properties file
String reportName = ctx.getCurrentXmlTest().getSuite().getName();
if (reportName.equals("Default suite"))
{
List<ITestNGMethod> allMethods = ctx.getSuite().getAllMethods();
for (ITestNGMethod method : allMethods)
{
String fullMethod = method.toString();
int indexOf = fullMethod.indexOf(".");
reportName = fullMethod.replace(fullMethod.substring(indexOf), ""); }
}
我可能已经把它复杂化了,但至少指出了我需要去的地方。我的解决方案在我的问题的底部。 –