问题
I was using Extent Reports V3 with selenium / C# and I just upgraded to V4. Previously each run would give me a unique report based on the Date stamp / Class Name / Time stamp. However, after moving to V4, it always puts everything under the same file named "index" and a separate file named "dashboard" which is a file to sit above the other for navigation purposes.
Here is my code for starting the report:
htmlReporter = new ExtentHtmlReporter($"C:\\Test-Results\\" + dateStamp + "\\" + TestClassName + " " + timeStamp + ".html");
extent = new ExtentReports();
extent.AttachReporter(htmlReporter);
extent.AddSystemInfo("Host Name", "Extent Framework");
extent.AddSystemInfo("Environment", "Local Machine");
extent.AddSystemInfo("User Name", "MyName");
htmlReporter.LoadConfig(CurrentDirectory + "\\extent-config.xml");
Now, each time I run a test, it overwrites the existing Index file with the new test results rather than appending my current results OR giving me a unique index file. I can provide any additional information needed about how i'm starting the reports / creating the tests if needed but for now here is what lives in my test file:
[ClassInitialize()]
public static void MyClassInitialize(TestContext testContext)
{
report.startReport("Report Name");
}
[ClassCleanup()]
public static void MyClassCleanup()
{
report.Flush();
}
[TestInitialize()]
public void MyTestInitialize()
{
string name = TestContext.TestName;
report.CreateTest(name);
}
回答1:
It is enhanced feature of v4. To overcome it, We have to use ExtentV3HtmlReporter class in version 4. By using this class, We will be have Reports as we had. It will not override with index file. Also, There are many bugs get solved in V4. So things used same as version 4 report. You can compare both reports and you will be have your solution.
回答2:
I started working on Extent Reports v4 recently and to solve files getting replaced issue you have to use v4 with v3 format as per @Ishita Shah answer, please see this Extent Reports generating two HTML Reports
Also, I have done a little tweak to generate new html files for every run without replacing the already generated files.
string fileName = "ExtentReport_" + DateTime.Now.ToString("MMM-dd-yyyy hh-mm-ss");
//Rename index.html with a new file name
renameFile("C:\\Reports\\index.html", fileName + "_Index.html");
//Replace "index.html" string inside the "index.html" file with the new filename
replaceTextHTMLFile("C:\\Reports\\" + fileName + "_Index.html", "index.html", fileName + "_Index.html");
same logic can be used for Dashboard.html and tags.html
renameFile:
public static void renameFile(string filePath, string oldFileName, string newFileName)
{
System.IO.File.Move(filePath + oldFileName, filePath + newFileName);
}
replaceTextHTMLFile:
public static void replaceTextHTMLFile(string filePath, string findText, string replaceText)
{
try
{
StreamReader objReader = new StreamReader(filePath);
string content = objReader.ReadToEnd();
objReader.Close();
content = Regex.Replace(content, findText, replaceText);
StreamWriter writerObj = new StreamWriter(filePath);
writerObj.Write(content);
writerObj.Close();
}
catch (Exception e)
{
Console.WriteLine("Exception occurred. Messgae: " + e.Message);
}
}
来源:https://stackoverflow.com/questions/54237310/extent-reports-v4-overwriting-test-results