从断言获取日志消息

时间:2019-01-11 16:49:28

标签: java selenium testng extentreports

我正在使用范围报告库(版本4)将html报告集成到我们的测试框架中。

为此,我编写了一个包装器,包装了我们的默认TestNG记录器(用于控制台输出)和ExtentReport记录器(ExtentTest.log,用于收集html报告的数据)。 我们使用TestNg作为我们的测试框架。

在从失败的断言(软断言和硬断言)中捕获日志消息以将其显示在html报告中时出现问题,它们仅进入控制台日志。 捕获那些Assert日志消息以在html-report中显示它们的可能解决方案是什么?

我无法扩展Assert(或SoftAssert)类并添加自己的实现(通过在其中添加ExtentTest.log实例),因为我必须替换数十个测试中的所有断言。

public class Loggers {
  private static final Loggers instance = new Loggers();
  private static ExtentTest test;
  private static ExtentReports extent;
  private static Logger LOG;
  private static final String REPORT_LOCATION = "test-output/reports.extent.html";

  /**
   * Returns an instance of {@link ExtentReports} object. If it doesn't exist creates a new instance and returns it
   */
  public static ExtentReports getLogger() {
    if ( extent == null ) {
      createInstance();
    }
    return extent;
  }

  /**
   * Create ExtentReport and attaches htmlReporter to it
   */
  public static void createInstance() {
    ExtentHtmlReporter htmlReporter = getHTMLReporter();
    extent = new ExtentReports();
    extent.attachReporter( htmlReporter );
  }

  /**
   * This method creates, configures and returns an instance of ExtentHtmlReporter
   *
   */
  public static ExtentHtmlReporter getHTMLReporter() {
    ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter( REPORT_LOCATION );
     return htmlReporter;
  }

  /**
   * This method logs a message with the INFO level for both instances of TestNG Logger and ExtentTest
   */
  public void info( String message ) {
    LOG.info( message );
    test.log( Status.INFO, message );
  }

2 个答案:

答案 0 :(得分:1)

找到了解决方案。如果有人会有类似的问题,将在这里发布。

如果将记录器放在侦听器类中,则可以将记录器放在那里,并使用ITestResult作为参数,特别是其方法getThrowable(它返回运行该方法时抛出的throwable)

 /**
   * Triggered upon the failure of a test
   */
  public void onTestFailure( ITestResult testResult ) {
      LOG.fail( testResult.getThrowable() );
 }

它将在报告中显示失败的断言或引发的异常。

答案 1 :(得分:0)

以下代码适用于我的范围报告试试吧!

1)初始化范围报告和记录器

public static Logger log = Logger.getLogger("devpinoyLogger");
public ExtentReports rep = ExtentManager.getInstance();
public static ExtentTest test;

2)将配置文件ReportsConfig.xml用于扩展报告,您可以从extentreports官方站点获得该报告。

3)创建可加载配置文件并设置范围报告文件输出的ExtentManager类。

   public class ExtentManager {

        private static ExtentReports extent;
        public static ExtentReports getInstance(){

            if(extent==null){
                System.out.println("Path of user DIR"+System.getProperty("user.dir"));
                extent = new ExtentReports(System.getProperty("user.dir")+"\\target\\surefire-reports\\ExecutionReport.html",true,DisplayOrder.OLDEST_FIRST);
                extent.loadConfig(new File(System.getProperty("user.dir")+"\\src\\main\\java\\extentconfig\\ReportsConfig.xml"));       
            }
            return extent;
        }
    }

4)使用您创建的INFO和ERROR登录方法在“范围”报告中显示日志。

public void click(String xpath) {

    try {
        driver.findElement(By.xpath(Variables.OR.getProperty(xpath))).click();
        System.out.println(xpath + "Button clicked");
        test.log(LogStatus.INFO, xpath + " Button clicked");
        Thread.sleep(1000);
    } catch (Exception e) {
        System.err.println("Cannot Click " + e.getMessage());
        test.log(LogStatus.ERROR,"Unable to click on :: " + xpath + " Button");
        throw new AssertionError("Unable to click on ::  " + xpath + " Button", e);
    }
}

5)使用自定义侦听器类CustomListeners

public class CustomListeners extends TestBase implements ITestListener, ISuiteListener {

public boolean flag;

..implement all methods of CustomListeners class and use logs in onTestSuccess and onTestFailure Methods.

public void onTestSuccess(ITestResult arg0) {

    test.log(LogStatus.PASS, arg0.getName().toUpperCase() + " PASS");
    rep.endTest(test);
    rep.flush();

}


public void onTestFailure(ITestResult arg0) {
    System.out.println(arg0 + " =================Test Case Failed===========================");
    flag = true;
    System.out.println("Flag is inside onTestFailure " + flag);
    System.setProperty("org.uncommons.reportng.escape-output", "false");

    try {
        test.log(LogStatus.FAIL, arg0.getName().toUpperCase() + " Failed with exception : " + arg0.getThrowable());
        rep.endTest(test);
        rep.flush();
    } catch (IOException e) {
        System.err.println("IOException occurs " + e.getMessage());
        e.printStackTrace();
    }

}

}

  • PASS测试用例的ExtentReport视图

enter image description here

  • FAIL测试用例的ExtentReport视图

enter image description here