如何在范围报告中捕获所有测试用例?

How to capture all test cases in extent report?

我在打印范围报告中的所有测试用例时遇到问题。我在测试用例文件中添加了两个测试用例,但只有最后一个测试用例显示在范围报告中。下面是我的代码。我正在使用 TestNG 执行测试用例。

public class TC02_Login extends BaseClass {

    ExtentReports extent;
    ExtentTest logger;
    WebDriver driver;

    @BeforeMethod
    public void createReport() {
        ExtentHtmlReporter reporter = new ExtentHtmlReporter("./Reports/finalReport.html");
        extent = new ExtentReports();
        extent.attachReporter(reporter);
    }

    @Parameters({ "userName", "password" })
    @Test(priority = 1)
    public void login(String userName, String password) throws InterruptedException, IOException {

        logger = extent.createTest("Login Module");
        objSeleutils.click(objLogin.lbl_signin);
        objSeleutils.enterText(objLogin.txt_email, userName);
        objSeleutils.enterText(objLogin.txt_password, password);
        objSeleutils.click(objLogin.btn_signup);

        boolean txt_welcome = objSeleutils.existsElement(objLogin.ddl_welcome);
        Assert.assertEquals(txt_welcome, true);
    }

    @Test(priority = 2)
    public void dashboard() {

        logger = extent.createTest("Dashboard Verification.");
        boolean txt_welcome = objSeleutils.existsElement(objLogin.btn_findPet);
        Assert.assertEquals(txt_welcome, false);
    }

    @AfterMethod
    public void teardown(ITestResult results) throws IOException {
        if (results.getStatus() == ITestResult.FAILURE) {
            String path = System.getProperty("user.dir") + "/Screenshots/" + "Fail_" + System.currentTimeMillis()
                    + ".png";
            String temp = objSeleutils.tackScreenShot(path);
            logger.fail(results.getThrowable().getMessage(), MediaEntityBuilder.createScreenCaptureFromPath(temp).build());
            logger.log(Status.FAIL, "Test Case Failed.");
        }

        if (results.getStatus() == ITestResult.SUCCESS) {
            String path = System.getProperty("user.dir") + "/Screenshots/" + "Pass_" + System.currentTimeMillis()
                    + ".png";
            String temp = objSeleutils.tackScreenShot(path);
            logger.log(Status.PASS, "Test Case Passed.", MediaEntityBuilder.createScreenCaptureFromPath(temp).build());
        }
        extent.flush();
    }
}

这是我在 运行 我的测试用例中的 testNG 文件。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Web" verbose="2">

<parameter name="browserName" value="Chrome" />
<parameter name="projectURL" value="http://sitename.com" />
<parameter name="userName" value="ashishsavaliya@gmail.com" />
<parameter name="password" value="*********" />

<test name="Test">          
        <classes>
            <class name='testCase.TC02_Login' />
        </classes>
  </test>
</suite>

这是因为您的@BeforeMethod 中的逻辑应该在@BeforeClass 或@BeforeSuite 中,因为您现在使用每个新测试重新创建整个范围对象。

正如您在 BeforeMethod 中定义的那样,它将被 Last @Test 覆盖。您需要通过一些区分才能获得这两个报告。

喜欢,可以使用计数变量

public class TC02_Login extends BaseClass {

        ExtentReports extent;
        ExtentTest logger;
        WebDriver driver;
        int count = 1;

        @BeforeMethod
        public void createReport() {
            ExtentHtmlReporter reporter = new ExtentHtmlReporter("./Reports/finalReport"+ count + ".html");
            extent = new ExtentReports();
            extent.attachReporter(reporter);
        }

    @AfterMethod
    public void teardown(ITestResult results) throws IOException {
    count++;
    }
}

有很多方法可以做到这一点。您可以使用的一种方法是使用相关的测试名称调用每个 @Test 中的每个报告。