范围报告:日志被附加到报告中的单个测试用例
Extent Reports: Logs are Getting Appended on to a Single Test Case in the Report
我无法为不同的测试用例设置单独的 Logs/entries。即日志被附加到生成的范围报告中的单个测试用例。
使用范围报告版本 2.41.1..
报告样本:
我的代码是这样的:
Base Class of TestNG:
public static ExtentReports report;
public static ExtentTest logger;
@BeforeSuite
@Parameters({"BrowserName"})
public void beforeSuite(@Optional("Firefox") String BrowserName)
{
date = new Date();
SimpleDateFormat ft = new SimpleDateFormat ("dd-MM-yyyy");
String TodaysDate = ft.format(date);
prop = ResourceBundle.getBundle("DataFile");
Driver = Browser_Factory_FSQA_Class.getBrowser(BrowserName);
path = "//home//workspace//....//FSQA.ExtentReports//FSQA_ExtentReport_" + TodaysDate;
File f=new File(path);
if(!f.exists())
{
report = new ExtentReports(path);
}
}
@BeforeTest
@Parameters({"BrowserName"})
public void SetUpConfig(@Optional("Firefox") String BrowserName)
{
report = new ExtentReports(path, false,DisplayOrder.NEWEST_FIRST);
report.loadConfig(new File("//home//......//extent-config.xml"));
report.addSystemInfo("Build-Tag", prop.getString("Build-Tag"))
.addSystemInfo("Selenium ", prop.getString("SelVer"))
.assignProject(prop.getString("Application"))
.addSystemInfo("Envirnoment", prop.getString("AppEnvirnoment"))
.addSystemInfo("Extent", prop.getString("ExtRpVer"));
logger = report.startTest(this.getClass().getSimpleName());
logger.log(LogStatus.INFO,this.getClass().getSimpleName() +" will Run on "+ BrowserName +" Browser");
Driver.get(prop.getString("URL"));
Driver.manage().window().maximize();
Driver.manage().window().maximize();
Driver.manage().timeouts().implicitlyWait(45, TimeUnit.SECONDS);
}
@AfterMethod(alwaysRun=true)
public void afterMethod(ITestResult result) throws IOException
{
try
{
if(result.getStatus()==ITestResult.FAILURE)
{
String res = captureScreenshot(Driver, result.getName());
String image= logger.addScreenCapture(res);
System.out.println(image);
String TestCaseName = this.getClass().getSimpleName() + " Test Case Failure and Title/Boolean Value Failed";
logger.log(LogStatus.FAIL, TestCaseName + logger.addScreenCapture(res));
}
else if(result.getStatus()==ITestResult.SUCCESS)
{
logger.log(LogStatus.PASS, this.getClass().getSimpleName() + " Test Case Success and Title Verified");
}
else if(result.getStatus()==ITestResult.SKIP)
{
logger.log(LogStatus.SKIP, this.getClass().getSimpleName() + " Test Case Skipped");
}
}
catch(Throwable t)
{
logger.log(LogStatus.ERROR,t.getMessage());
}
}
public String captureScreenshot(WebDriver Driver, String TestName)
{
try
{
date = new Date();
SimpleDateFormat ft = new SimpleDateFormat ("dd_MM_yyyy");
String TodaysDate = ft.format(date);
TakesScreenshot ts=(TakesScreenshot)Driver;
File source=ts.getScreenshotAs(OutputType.FILE);
Sc_Destination = prop.getString("SC_Dest")+TestName+"__"+TodaysDate+".png";
FileUtils.copyFile(source,new File(Sc_Destination));
logger.log(LogStatus.FAIL,image, "Title verification");*/
return Sc_Destination;
}
catch (Exception e)
{
System.out.println("Exception while taking screenshot "+e.getMessage());
}
return Sc_Destination;
}
@AfterTest(alwaysRun=true)
public void AfterTest()
{
Driver.close();
report.endTest(logger);
report.flush();
}
@AfterSuite
public void AfterSuite()
{
report.close();
Driver.quit();
}
我的测试是扩展此基础的独立类 Class:
TC1:
public classTC1 extends BaseTestNG
{
@Test(groups = {"Happy_Path"} , description="TC1")
public void TestCase1() throws InterruptedException, Exception
{
logger.log(LogStatus.INFO, " Test Case2 Block Entered");
Thread.sleep(4000);
......
......
logger.log(LogStatus.INFO, "Assert Flag Received");
Thread.sleep(4000);
Assert.assertTrue(AssertFlag);
}
}
TC2:
public classTC2 extends BaseTestNG
{
@Test(groups = {"Happy_Path"} , description="TC2")
public void TestCase2() throws InterruptedException, Exception
{
logger.log(LogStatus.INFO, " Test Case2 Block Entered");
Thread.sleep(4000);
......
......
logger.log(LogStatus.INFO, "Assert Flag Received");
Thread.sleep(4000);
Assert.assertTrue(AssertFlag);
}
}
我正在使用 POM Classes 以及 TestNG 和 运行 使用 testng.xml 的测试用例。
我能够生成范围报告,但无法区分 TC1 和 TC2 日志,即所有测试用例日志都附加到单个测试用例,如上面的屏幕截图所示。
我希望每个测试用例 Logs/Entry 在范围报告的单独行中。
任何人都可以纠正我代码中的错误并帮助我。
提前致谢!!!
我认为,您应该在 BeforeMethod 中初始化您的 logger,并在 AfterMethod 中使用 report.flush()。这可能会解决您的问题。
我无法为不同的测试用例设置单独的 Logs/entries。即日志被附加到生成的范围报告中的单个测试用例。 使用范围报告版本 2.41.1..
报告样本:
我的代码是这样的:
Base Class of TestNG:
public static ExtentReports report;
public static ExtentTest logger;
@BeforeSuite
@Parameters({"BrowserName"})
public void beforeSuite(@Optional("Firefox") String BrowserName)
{
date = new Date();
SimpleDateFormat ft = new SimpleDateFormat ("dd-MM-yyyy");
String TodaysDate = ft.format(date);
prop = ResourceBundle.getBundle("DataFile");
Driver = Browser_Factory_FSQA_Class.getBrowser(BrowserName);
path = "//home//workspace//....//FSQA.ExtentReports//FSQA_ExtentReport_" + TodaysDate;
File f=new File(path);
if(!f.exists())
{
report = new ExtentReports(path);
}
}
@BeforeTest
@Parameters({"BrowserName"})
public void SetUpConfig(@Optional("Firefox") String BrowserName)
{
report = new ExtentReports(path, false,DisplayOrder.NEWEST_FIRST);
report.loadConfig(new File("//home//......//extent-config.xml"));
report.addSystemInfo("Build-Tag", prop.getString("Build-Tag"))
.addSystemInfo("Selenium ", prop.getString("SelVer"))
.assignProject(prop.getString("Application"))
.addSystemInfo("Envirnoment", prop.getString("AppEnvirnoment"))
.addSystemInfo("Extent", prop.getString("ExtRpVer"));
logger = report.startTest(this.getClass().getSimpleName());
logger.log(LogStatus.INFO,this.getClass().getSimpleName() +" will Run on "+ BrowserName +" Browser");
Driver.get(prop.getString("URL"));
Driver.manage().window().maximize();
Driver.manage().window().maximize();
Driver.manage().timeouts().implicitlyWait(45, TimeUnit.SECONDS);
}
@AfterMethod(alwaysRun=true)
public void afterMethod(ITestResult result) throws IOException
{
try
{
if(result.getStatus()==ITestResult.FAILURE)
{
String res = captureScreenshot(Driver, result.getName());
String image= logger.addScreenCapture(res);
System.out.println(image);
String TestCaseName = this.getClass().getSimpleName() + " Test Case Failure and Title/Boolean Value Failed";
logger.log(LogStatus.FAIL, TestCaseName + logger.addScreenCapture(res));
}
else if(result.getStatus()==ITestResult.SUCCESS)
{
logger.log(LogStatus.PASS, this.getClass().getSimpleName() + " Test Case Success and Title Verified");
}
else if(result.getStatus()==ITestResult.SKIP)
{
logger.log(LogStatus.SKIP, this.getClass().getSimpleName() + " Test Case Skipped");
}
}
catch(Throwable t)
{
logger.log(LogStatus.ERROR,t.getMessage());
}
}
public String captureScreenshot(WebDriver Driver, String TestName)
{
try
{
date = new Date();
SimpleDateFormat ft = new SimpleDateFormat ("dd_MM_yyyy");
String TodaysDate = ft.format(date);
TakesScreenshot ts=(TakesScreenshot)Driver;
File source=ts.getScreenshotAs(OutputType.FILE);
Sc_Destination = prop.getString("SC_Dest")+TestName+"__"+TodaysDate+".png";
FileUtils.copyFile(source,new File(Sc_Destination));
logger.log(LogStatus.FAIL,image, "Title verification");*/
return Sc_Destination;
}
catch (Exception e)
{
System.out.println("Exception while taking screenshot "+e.getMessage());
}
return Sc_Destination;
}
@AfterTest(alwaysRun=true)
public void AfterTest()
{
Driver.close();
report.endTest(logger);
report.flush();
}
@AfterSuite
public void AfterSuite()
{
report.close();
Driver.quit();
}
我的测试是扩展此基础的独立类 Class: TC1:
public classTC1 extends BaseTestNG
{
@Test(groups = {"Happy_Path"} , description="TC1")
public void TestCase1() throws InterruptedException, Exception
{
logger.log(LogStatus.INFO, " Test Case2 Block Entered");
Thread.sleep(4000);
......
......
logger.log(LogStatus.INFO, "Assert Flag Received");
Thread.sleep(4000);
Assert.assertTrue(AssertFlag);
}
}
TC2:
public classTC2 extends BaseTestNG
{
@Test(groups = {"Happy_Path"} , description="TC2")
public void TestCase2() throws InterruptedException, Exception
{
logger.log(LogStatus.INFO, " Test Case2 Block Entered");
Thread.sleep(4000);
......
......
logger.log(LogStatus.INFO, "Assert Flag Received");
Thread.sleep(4000);
Assert.assertTrue(AssertFlag);
}
}
我正在使用 POM Classes 以及 TestNG 和 运行 使用 testng.xml 的测试用例。
我能够生成范围报告,但无法区分 TC1 和 TC2 日志,即所有测试用例日志都附加到单个测试用例,如上面的屏幕截图所示。
我希望每个测试用例 Logs/Entry 在范围报告的单独行中。
任何人都可以纠正我代码中的错误并帮助我。
提前致谢!!!
我认为,您应该在 BeforeMethod 中初始化您的 logger,并在 AfterMethod 中使用 report.flush()。这可能会解决您的问题。