0

我无法为不同的测试用例提供单独的日志/条目。即日志被附加到生成的范围报告中的单个测试用例中。使用范围报告版本 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();
}

我的测试是扩展此基类的单独类: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,如上面的屏幕截图所示。

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

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

提前致谢!!!

4

1 回答 1

0

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

于 2016-10-14T10:36:17.137 回答