如何在范围报告中附加数据提供者名称和方法名称?

时间:2018-08-28 09:23:28

标签: java selenium extentreports selenium-extent-report

我在当前项目中正在研究范围报告框架。在测试类中,我正在使用Data-provider类导入测试数据。特定测试包含10个验证测试,迭代数据提供者密钥。 关于范围报告,我正在将方法名称写入报告中 但是我必须用与每个验证测试相匹配的名称来区分每个数据提供者测试。 这是我的脚本:1。范围报告配置:

   @BeforeSuite
   public void setUp()
   {
       htmlReporter = new ExtentHtmlReporter(System.getProperty("user.dir") +"/test-output/extentReport.html");
       extent = new ExtentReports();
       extent.attachReporter(htmlReporter);

       extent.setSystemInfo("OS", "Windows");
       extent.setSystemInfo("Host Name", "1016086");
       extent.setSystemInfo("Environment", "QA");
       extent.setSystemInfo("User Name", "BABURAJ V D");

       htmlReporter.config().setChartVisibilityOnOpen(true);
       htmlReporter.config().setDocumentTitle("Extent report");
       htmlReporter.config().setReportName("Final Report");
   }


      @BeforeClass
      public synchronized void beforeClass() {
          test = extent.createTest(getClass().getSimpleName());
          parentTest.set(test);

      }

      @BeforeMethod
      public synchronized void beforeMethod(Method method) {

          child = parentTest.get().createNode(method.getName());
          childTest.set(child);

      }
      @AfterMethod
        public synchronized void afterMethod(ITestResult result) {
            if (result.getStatus() == ITestResult.FAILURE)
                childTest.get().fail(result.getThrowable());
            else if (result.getStatus() == ITestResult.SKIP)
                childTest.get().skip(result.getThrowable());
            else
                childTest.get().pass("Test passed");

   }

   @AfterSuite

   public void tearDown(){

       extent.flush();
   }

Extend Report

@Test(priority = 2, dataProvider = GE_XR240_DEVICE_DataProvider.DEVICE_ADD_VALIDATIONS, dataProviderClass = GE_XR240_DEVICE_DataProvider.class)

public void OPTIMA_XR240_FAILED_MESSAGE_VALIDATION(Map<String, String> deviceTestData) throws InterruptedException { }

这些是在特定测试OPTIMA_XR240_FAILED_MESSAGE_VALIDATION中发生的验证或应该在UI中显示的错误消息:

Add Device Failed - System name should be filled
Add Device Failed - System Id should be filled
Add Device Failed - Manufacturer should be assigned
Add Device Failed - System model should be assigned
Add Device Failed - Current IP Address / Host Name should be filled

这样我就可以使报告中包含与其执行的验证有关的信息,而不仅仅是方法名称。

2 个答案:

答案 0 :(得分:0)

正在使用方法名称创建节点,因为您已定义名为“ OPTIMA_XR240_FAILED_MESSAGE_VALIDATION”的方法

如果要获取自定义名称,则可以通过以下方式传递它:

 child = parentTest.get().createNode("Test node");
 childTest.set(child);

每次都想给出一些数字标识,例如“测试节点1”,“测试节点2”等。您可以通过Count增量或循环获得它,因此它的定义如下:

child = parentTest.get().createNode("Test node" +i);
childTest.set(child);

您可以在方法完成之前增加i值。

答案 1 :(得分:0)

这是来自extent-testng-plugin的预发布代码段,它将作为版本4的一部分很快发布,但是将其发布在这里,因为这是将数据提供者引入您的方法的方法:

public class ExtentITestListener
    implements ITestListener {

    private static final ExtentReports EXTENT = Extent.getInstance();

    private static ThreadLocal<ExtentTest> methodTest = new ThreadLocal<ExtentTest>();
    private static ThreadLocal<ExtentTest> dataProviderTest = new ThreadLocal<>();

    @Override
    public synchronized void onStart(ITestContext context) { }

    @Override
    public synchronized void onFinish(ITestContext context) {
        EXTENT.flush();
    }

    @Override
    public synchronized void onTestStart(ITestResult result) {      
        String methodName = result.getMethod().getMethodName();
        if (result.getParameters().length>0) {
            if (methodTest.get() != null && methodTest.get().getModel().getName().equals(methodName)) { } 
            else {
                createTest(result);
            }
            String paramName = Arrays.asList(result.getParameters()).toString();
            ExtentTest paramTest = methodTest.get().createNode(paramName);
            dataProviderTest.set(paramTest);
        } else {
            createTest(result);
        }
    }

    private void createTest(ITestResult result) {
        String methodName = result.getMethod().getMethodName();
        ExtentTest test = EXTENT.createTest(methodName);
        methodTest.set(test);

        String[] groups = result.getMethod().getGroups();
        if (groups.length > 0) {
            Arrays.asList(groups)
                .forEach(x -> methodTest.get().assignCategory(x));
        }
    }

    @Override
    public synchronized void onTestSuccess(ITestResult result) {
        getTest(result).pass("Test passed");
    }

    private ExtentTest getTest(ITestResult result) {
        ExtentTest t = result.getParameters() != null && result.getParameters().length>0
                ? dataProviderTest.get()
                : methodTest.get();
        return t;
    }

    @Override
    public synchronized void onTestFailure(ITestResult result) {
        getTest(result).fail(result.getThrowable());
    }

    @Override
    public synchronized void onTestSkipped(ITestResult result) {
        getTest(result).skip(result.getThrowable());
    }

    @Override
    public synchronized void onTestFailedButWithinSuccessPercentage(ITestResult result) { }

}


public class Extent 
    implements Serializable {

    private static final long serialVersionUID = 1L;

    private static class ExtentReportsLoader {

        private static final ExtentReports INSTANCE = new ExtentReports();

        static {
        }
    }

    public static synchronized ExtentReports getInstance() {
        return ExtentReportsLoader.INSTANCE;
    }

    @SuppressWarnings("unused")
    private ExtentReports readResolve() {
        return ExtentReportsLoader.INSTANCE;
    }

}

由于这是一个ITestListener,因此可以将其直接添加到测试或基类中,如下所示:

@Listeners(ExtentITestListenerAdapter.class)
public class MyTests {
    @Test
    public void test() {
        ...
    }
}