Chirag Mittal
Chirag Mittal

Reputation: 1

How to avoid extent report to not to overwrite the html file name

I am using extent reports in appium with testng and its working fine for me.whenver my tests run is completed then extent report generates html file in my project folder and that is what expected.

Issue is that when I again run my tests then extent report generate new html report file by overwrting the name of previously created html file.

I want extent report to generate html file with unique names or name with date in in, each time when I run my tests

Upvotes: 0

Views: 10054

Answers (5)

Juhi Agarwal
Juhi Agarwal

Reputation: 46

I also faced a similar issue. As in the real-world, we need old reports as well. Below is the solution in Java for Extent PDF report

I added an event listener method. Event used- TestRunStarted. We further need to register for this event too. The solution can be done for HTML report too.

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

Upvotes: 0

Sobhit Sharma
Sobhit Sharma

Reputation: 713

I have done it like this, simple and crisp.

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

So here ExecutionConfig.FileOutname is called from the class ExecutionConfig where i am reading the values from the config.properties file. and then here assigning it to the output-file.

Also it worked for me.

Upvotes: 0

Neagu V
Neagu V

Reputation: 480

I use:

private static String timestamp = new SimpleDateFormat("HH:mm:ss").format(Calendar.getInstance().getTime()).replaceAll(":", "-");
public static String reportFullPath = getReportsPath() + "\\AutomationReport_" + timestamp + ".html";

Upvotes: 0

Kovacic
Kovacic

Reputation: 1481

You can do it by setting unique name:

String reportFile = resultDirectory + fileName + ".html";

than method for saving report to certain folder:

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

...and utility for setting dateTime:

public static String dateTimeGenerate(){
    Format formatter = new SimpleDateFormat("YYYYMMdd_HHmmssSSS");
    Date date = new Date(System.currentTimeMillis());
   return formatter.format(date);
}

Or simply use klov reports start server and have everything in database (MongoDb), it is more elegant way to go.

Hope this helps,

Upvotes: 0

Shivam Mishra
Shivam Mishra

Reputation: 1439

You can create your file name to be the current timestamp. This way, it will be easy to have a unique name for your report file -

String timeStamp = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Date());
extent = new ExtentReports (userDir +"\\test-output\\" + timeStamp + ".html", true);

Upvotes: 0

Related Questions