如何生成testng报告而不覆盖以前的测试结果

时间:2018-01-31 12:58:33

标签: maven selenium testng pom.xml

我正在使用Selenium和Java自动化Web应用程序。

我正在并行执行多个testng xml文件,因此每次都会覆盖结果。

例如:我有两个xml文件(testng1.xml和testng 2.xml)。当我并行运行这两个文件时,来自testng2.xml的结果将在可通过电子邮件发送的报告中覆盖testng1.xml。

如何为每个xml文件生成单独的报告?

1 个答案:

答案 0 :(得分:0)

我已使用以下代码实现了此方案。

public String xmlString;

public int xmlIndex;

public String folderName;

public String locationName;

public static String reportName = "emailable-report.html";

    xmlString = <list of xml files>;
    xmlIndex = xmlString.lastIndexOf("/");
    locationName = xmlString.substring(xmlIndex+1);
    folderName = locationName.replace(".xml", "").toUpperCase();

File dir = new File("Result"); // Here I am creating a common folder and the sub folders will be created as per the xml file executions.
    if(dir.exists())
    {
        try {
            FileUtils.deleteDirectory(dir);
        } catch (IOException e) {
            e.printStackTrace();
        }
        dir.mkdir();
    }
    else
    {
        dir.mkdir();
    }
    String xmlFolderPath = System.getProperty("user.dir") +"/"+ dir + "/"+ folderName;
    testng.setOutputDirectory(xmlFolderPath);
    File resultFile = new File(System.getProperty("user.dir") +"/"+ dir + "/" +"ResultFiles"); // In this folder only testng result (.html)files are moved here.
    resultFile.mkdir();


File folder = new File(xmlFolderPath); // In this folder, separate sub folders were created according to the xml files and the sub folder will be the xml file names. 
     File[] listOfFiles = folder.listFiles();

        for (int i = 0; i < listOfFiles.length; i++)
        {
          if (listOfFiles[i].isFile())
          {                   
              if(reportName.equalsIgnoreCase("emailable-report.html"))       
              {                   
                  Path src = Paths.get(xmlFolderPath + "/" + reportName);
                  Path desc = Paths.get(resultFile + "/" + folderName + ".html"); // Here emailable-report.html is renamed according to the XML file name and moved to this folder
                  try
                  {
                    Files.move(src, desc, StandardCopyOption.REPLACE_EXISTING);
                    System.out.println(src.resolveSibling(folderName + ".html"));
                    break;
                  } 
                  catch (IOException e) 
                  {
                    System.out.println(e);
                  }
             }
              else
              {
                  System.out.println("Expected file not present in the specified folder..!!!");
              }
          }
        }