java - 使用监听器和不使用监听器创建范围报告有什么区别

标签 java testng listener extentreports

我想生成一个范围报告,但我对使用监听器和不使用监听器创建范围报告之间的区别感到困惑?

我已经完成了为两者编写的代码,但我看不出区别,也看不出哪个更好,因为它看起来一样,结果也一样。谁能给我解释一下哪个更好?下面附上我的示例代码,当我删除监听器仍然可以得到相同的结果:

Listener.java

package listener;
public class Listener implements ITestListener {
ExtentTest test;
public void onTestStart(ITestResult result) {
    System.out.println("on test start");

}
public void onTestSuccess(ITestResult result) {
    System.out.println("on test success");
}
public void onTestFailure(ITestResult result) {
    System.out.println("on test failure");
    result.getThrowable();
}
public void onTestSkipped(ITestResult result) {
    System.out.println("on test skipped");
    result.getThrowable();
}
public void onTestFailedButWithinSuccessPercentage(ITestResult result) {
    System.out.println("on test sucess within percentage");
}
public void onStart(ITestContext context) {
    System.out.println("on start");

}
public void onFinish(ITestContext context) {
    System.out.println("on finish");
} }

BaseTest.java

public class BaseTest {
public AppiumDriver<WebElement> driver;
ExtentReports extent;
ExtentTest test;

String timeStamp = new SimpleDateFormat("EEE-d-MMM-yyyy.HH-mm-ss ").format(new Date());
String fileName = System.getProperty("user.dir") + "/result/report" + timeStamp + ".html";
ExtentHtmlReporter htmlReports;
@Parameters("deviceModel")
@BeforeTest
public void setUp() throws MalformedURLException {
    htmlReports = new ExtentHtmlReporter(fileName);
    extent = new ExtentReports();
    extent.attachReporter(htmlReports);
    htmlReports.config().setReportName("Testing");
    htmlReports.config().setTheme(Theme.STANDARD);
    htmlReports.config().setTestViewChartLocation(ChartLocation.BOTTOM);
    htmlReports.config().setDocumentTitle("HtmlResultTest");

    System.out.println("Session is creating");
    DesiredCapabilities capabilities = new DesiredCapabilities();
    capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "ios");
    capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, "10.3.3");
    capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Agmo");
    capabilities.setCapability(MobileCapabilityType.BROWSER_NAME, "Safari");
    driver = new IOSDriver<WebElement>(new URL("http://localhost:4723/wd/hub"), capabilities);
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    System.out.println("Session is created");
}

@AfterTest
public void tearDown() {
    extent.flush();
}

@AfterMethod
public void checkResults(ITestResult testResult){
    if (testResult.getStatus()==ITestResult.FAILURE){
        test.log(Status.FAIL, "Test case is failed because of below problem");
        test.log(Status.FAIL, testResult.getThrowable());
    }else if (testResult.getStatus()==ITestResult.SUCCESS){
        test.log(Status.PASS, "Test case is passed");
    }else if (testResult.getStatus()==ITestResult.SKIP){
        test.log(Status.SKIP, testResult.getThrowable());
    }
} }

测试.java

public class Testing extends BaseTest {
@Test(priority = 0)
public void InvalidVerificationCode() {
    test = extent.createTest("Invalid code");
    System.out.println("Starting test " + new Object() {}.getClass().getEnclosingMethod().getName());
    driver.get("https://r2c2-staging.azurewebsites.net");
    try {
        driver.findElement(By.xpath("//button[@class=\"btn btn-white-green\" and @type=\"button\"]")).click();
        driver.findElement(By.xpath("//input[@class=\"ng-untouched ng-pristine ng-invalid\" and @type=\"text\"]")).sendKeys("sarinadia+2@agmostudio.com");
        driver.findElement(By.xpath("//input[@class=\"ng-untouched ng-pristine ng-invalid\" and @type=\"password\"]")).sendKeys("12345");
        driver.findElement(By.xpath("//button[@class=\"signInBtn\" and @type=\"submit\"]")).click();
    }catch (Exception e){
        driver.findElement(By.xpath("//div[@class=\"title\"]")).click();
    }
    driver.findElement(By.xpath("//img[@class=\"ng-star-inserted\"]")).click();
    driver.findElement(By.xpath("//div[@class=\"logout\"]")).click();
    driver.findElement(By.xpath("//button[@class=\"btn btn-white-green\" and @type=\"button\"]")).click();
    driver.findElement(By.xpath("//input[@class=\"ng-untouched ng-pristine ng-invalid\" and @type=\"text\"]")).sendKeys("sarinadia+2@agmostudio.com");
    driver.findElement(By.xpath("//input[@class=\"ng-untouched ng-pristine ng-invalid\" and @type=\"password\"]")).sendKeys("12345");
    driver.findElement(By.xpath("//button[@class=\"signInBtn\" and @type=\"submit\"]")).click();
    try{
        driver.findElement(By.xpath("//div[@class=\"logout\" and @text=\"Logout\"]"));
    }catch (Exception e){
        String actual_error = driver.findElement(By.xpath("//div[@class=\"modal-content\"]")).getText();
        Assert.assertTrue(actual_error.contains("The user name or password provided is incorrect"));
        driver.findElement(By.xpath("//button[@class=\"btn btn-primary ok\" and @type=\"button\"]")).click();
        System.out.println("Tess login with invalid verification code pass!!\n");
    }
}

文件.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="tests">
<listeners>
    <listener class-name="Listener"/>
</listeners>
<test name="Test ios">
    <parameter name="deviceModel" value="Agmo" />
    <classes>
        <class name="Testing">
            <methods>
                <include name="InvalidVerificationCode"/>
            </methods>
        </class>
    </classes>
</test>
</suite>

最佳答案

没有区别,只是两种创建报告的方式。

虽然,查看代码,您对 Listener 的使用不会做任何事情,因为它只是写入控制台。该报告仅从 BaseTest 生成。

关于java - 使用监听器和不使用监听器创建范围报告有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53312632/

相关文章:

java - 作为 Maven 测试运行时,TestNG 测试会失败,但作为 TestNG 套件运行时会通过

java - widgetSelected() 和不同的操作系统平台行为

java - 将监听器定义的字符串变量导入到另一个包中

java - 使用 Ant 构建 jar 文件(htsjdk jar)

java - 如何使用 mockito 和 testNg 测试枚举中的默认值

java - 使用 Java 9 使 Ropeytask 崩溃

java - 使用 Maven 在多个线程上同时运行单个 TestNG 测试

android - 注册和取消注册 onSharedPreferenceListeners

java - websphere 应用程序服务器处理多少个并发请求(WAS 8.0)?

java - 错误: ')' expected compiler error