问题与TestNG和Eclipse有关吗?

时间:2017-02-10 12:45:46

标签: eclipse selenium-webdriver webdriver testng

例如,以下代码应该捕获异常:

  1. 异常将输出到控制台window == correct
  2. 该异常未在testNg == wrong
  3. 上显示为失败

    为什么会发生这种情况?

    我的代码:

        public void scrollToElementByLocator() {
        try {
            driver.findElement(By.id("wrong locator")).click();
        } catch (Exception e) {
            System.out.println("Exception! - unable to scroll to element, Exception: " + e.toString());
        }
    }
    

1 个答案:

答案 0 :(得分:1)

TestNG marks a test as Fail, if the code inside the test raises any Exception.

在你的代码中,当你通过捕获它自己处理异常时,不会向调用者引发异常,因此,TestNG永远不会知道在代码中引发异常。

删除try-catch阻止,因此会从测试中引发异常,因此TestNG可以将测试标记为Fail

尝试以下代码:

@Test
public void scrollToElementByLocator() {
        driver.findElement(By.id("wrong locator")).click();
}

注意:您必须使用@Test注释标记方法,告诉TestNG它是一种测试方法。

如果要在引发异常时执行某些操作,请尝试以下代码(抛出使用throw关键字捕获的异常):

@Test
public void scrollToElementByLocator() {
    try {
        driver.findElement(By.id("wrong locator")).click();
    } catch (Exception e) {
        System.out.println("Exception! - unable to scroll to element, Exception: " + e.toString()); //prints the exception
     throw e;
    }
}
相关问题