2

我有以下 testng.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="suite" verbose="8">
<parameter name="nodeURL" value=""></parameter>
<listeners>
<listener class-name="com.org.CustomExtentReporter"></listener>
</listeners>
    <test name="Default test1">
        <classes>
            <class
                name="com.org.SuiteConfiguration">
            </class>
            <class name="com.org.web.HomePage"> </class>
            <class name="com.org.web.Dashboard"> </class>
        </classes>
    </test>
</suite> 

在上面的 xml 中,我有两个类,它们将创建带有 4 个测试节点的报告及其登录范围报告。

我希望以下列方式生成报告:

测试类1

  • 测试用例 1
  • 测试用例 2

测试类2

  • 测试用例 1
  • 测试用例 2

现在,当用户单击测试用例 1 时,日志/结果将仅显示对应于测试类。

我们可以使用范围报告来做到这一点吗?我正在使用 ITestListener 进行测试报告。

public class CustomExtentReporter implements ITestListener {

    ExtentReports extent = ExtentManager.getInstance();
    public static ExtentTest test;

    public void onTestStart(ITestResult result) {
        //System.out.println("onTestStart called ...");
        test = extent.createTest(result.getMethod().getMethodName());
    }

    public void onTestSuccess(ITestResult result) {
        //test = extent.createTest("passTest");
        //System.out.println("onTestSuccess called ...");
        test.log(Status.PASS, MarkupHelper.createLabel(result.getMethod().getMethodName() + "Test Case Status is passed", ExtentColor.GREEN));
    }

    public void onTestFailure(ITestResult result) {
        WebDriver driver = (WebDriver) result.getTestContext().getAttribute("driver");
        //System.out.println("onTestFailure called ...");
        //test.log(Status.FAIL, MarkupHelper.createLabel(result.getMethod().getMethodName() + " - Test Case Failed", ExtentColor.RED));
        String screenShotPath = ScreenShotRecorder.captureScreenshot(driver, result.getMethod().getMethodName());
        test.log(Status.FAIL, MarkupHelper.createLabel(result.getThrowable() + " - Test Case Failed", ExtentColor.RED));
        try {
            test.fail(result.getThrowable()).addScreenCaptureFromPath(screenShotPath);
        } catch (IOException e) {
            test.log(Status.FAIL, "!!!!!!!!! Exception occurred while attaching the screenshot to extent report");
        }
        //test.log(Status.FAIL, MarkupHelper.createLabel(result.getThrowable() + " - Test Case Failed", ExtentColor.RED));
    }

    public void onTestSkipped(ITestResult result) {
        test.log(Status.SKIP, MarkupHelper.createLabel(result.getMethod().getMethodName() + " - Test Case Skipped", ExtentColor.ORANGE));
    }

    public void onTestFailedButWithinSuccessPercentage(ITestResult result) {
        // TODO Auto-generated method stub
        //System.out.println("onTestFailedButWithinSuccessPercentage called ...");
    }

    public void onStart(ITestContext context) {
        //System.out.println("onStart called ...");
        //System.out.println("test name ::: "+ context.getName());
        //test = extent.createTest(context.getName());
    }

    public void onFinish(ITestContext context) {
        //System.out.println("onFinish called ...");
        extent.flush();
        extent.removeTest(test);
    }
4

0 回答 0