我在 appium 中使用带有 testng 的范围报告,它对我来说工作正常。当我的测试运行完成时,范围报告会在我的项目文件夹中生成 html 文件,这就是预期的。
问题是,当我再次运行测试时,extent 报告通过覆盖先前创建的 html 文件的名称来生成新的 html 报告文件。
每次运行测试时,我都希望范围报告生成具有唯一名称或带有日期的名称的 html 文件
我在 appium 中使用带有 testng 的范围报告,它对我来说工作正常。当我的测试运行完成时,范围报告会在我的项目文件夹中生成 html 文件,这就是预期的。
问题是,当我再次运行测试时,extent 报告通过覆盖先前创建的 html 文件的名称来生成新的 html 报告文件。
每次运行测试时,我都希望范围报告生成具有唯一名称或带有日期的名称的 html 文件
我也遇到了类似的问题。与现实世界一样,我们也需要旧报告。以下是Java for Extent PDF报告的解决方案
我添加了一个事件监听器方法。使用的事件 - TestRunStarted。我们还需要注册此活动。该解决方案也可以用于 HTML 报告。
public void setCustomReportName(TestRunStarted event)
{
final SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss");
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
String currenttimestamp =sdf.format(timestamp);
Properties prop=new Properties();
//extent.reporter.pdf.out is the name of property which tell the report path
prop.setProperty("extent.reporter.pdf.out", "test output/PdfReport/ExtentPdf_"+currenttimestamp+".pdf");
ExtentService e1 =new ExtentService();
//ExtentReportsLoader is the inner class of ExtentService and initPdf is its private method which takes the path for report
Class<?>[] a=e1.getClass().getDeclaredClasses();
Method met;
//Even there is exception test run wont fail and report will also be generated (ExtentPdf.pdf)
try {
met = a[0].getDeclaredMethod("initPdf", Properties.class);
met.setAccessible(true);
met.invoke(a[0], prop);
} catch (NoSuchMethodException e) {
System.out.println("There is no method with name initPdf");
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
System.out.println("Argument passed to method initPdf are not correct");
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
我用:
private static String timestamp = new SimpleDateFormat("HH:mm:ss").format(Calendar.getInstance().getTime()).replaceAll(":", "-");
public static String reportFullPath = getReportsPath() + "\\AutomationReport_" + timestamp + ".html";
您可以通过设置唯一名称来做到这一点:
String reportFile = resultDirectory + fileName + ".html";
比将报告保存到某个文件夹的方法:
public void saveReportFolder() throws IOException {
File srcDir = new
File(System.getProperty("user.home")+"/Automation/target");
File destDir = new File(System.getProperty("user.home") + "/reports/"+ System.getProperty("user.name")+"/"+dateTimeGenerator());
FileUtils.copyDirectory(srcDir, destDir);
}
...以及用于设置日期时间的实用程序:
public static String dateTimeGenerate(){
Format formatter = new SimpleDateFormat("YYYYMMdd_HHmmssSSS");
Date date = new Date(System.currentTimeMillis());
return formatter.format(date);
}
或者简单地使用klov 报告启动服务器并将所有内容都保存在数据库 (MongoDb) 中,这是一种更优雅的方式。
希望这可以帮助,
我是这样做的,简单明了。
String Outputfilename= ExecutionConfig.FileOutname;
System.err.close(); // written to remove JAVA 9 incompatibility.. continued below
System.setErr(System.out); // continue.. and remove the warnings
extent = new ExtentReports(System.getProperty("user.dir") + "/test-output/"+Outputfilename+".html", true);
所以这里 ExecutionConfig.FileOutname 是从 ExecutionConfig 类中调用的,我正在从 config.properties 文件中读取值。然后在这里将其分配给输出文件。
它也对我有用。
您可以将文件名创建为当前时间戳。这样,您的报告文件将很容易拥有一个唯一的名称 -
String timeStamp = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Date());
extent = new ExtentReports (userDir +"\\test-output\\" + timeStamp + ".html", true);