范围报告:日志将附加到报告中的单个测试用例

时间:2016-10-14 09:33:31

标签: java selenium selenium-webdriver extent

我无法为不同的测试用例提供单独的日志/条目。即,生成的范围报告中的日志会附加到单个测试用例中。 使用范围报告版本2.41.1 ..

报告示例:Extent Report Logs Geting Appended to Single Test Case

MY Code就像这样:

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

我的测试是单独的分支,扩展了这个基类: 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类和TestNG并使用testng.xml运行测试用例。

我能够生成范围报告,但无法区分TC1和& TC2日志,即所有测试用例日志都附加到单个TestCase,如上面的屏幕截图所示。

我希望每个测试用例日志/条目都在范围报告中的不同行中。

任何人都可以纠正我的代码中的错误并帮助我。

提前致谢!!!

1 个答案:

答案 0 :(得分:0)

我认为,您应该在BeforeMethod中初始化记录器,并在AfterMethod中使用report.flush()。这可以解决你的问题。