TestNG浏览器在每次测试后都没有关闭

时间:2017-07-30 15:58:06

标签: testng

学习硒和TestNG

我有一个testNG.xml文件,该文件指定了3个测试

每个测试(下面的示例)都有一个@AfterTest注释和一个关闭浏览器的方法。在SUITE完成之前,浏览器都保持打开状态。我认为@AfterTest在每次测试后都会运行,所以为什么在每次测试后都不会被调用

测试:

    import java.io.IOException;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import junit.framework.Assert;
import pageObjects.LandingPage;
import resources.base;

public class validateNavBar extends base {

    public static Logger log = LogManager.getLogger(base.class.getName());

    @BeforeTest
    public void init() throws IOException {
        driver = initializeDriver();
        driver.get(prop.getProperty("url"));
    }

    @Test
    public void basePagenavigation() throws IOException {

        // Go to landing page and click Login
        LandingPage lp = new LandingPage(driver);
        // lp.getTitle().getText();
        Assert.assertTrue("NavBar is displayed", lp.getNavBar().isDisplayed());
        log.info("NavBar is visible");
    }

    @AfterTest
    public void teardown() {
        log.info("Closing browser");
        driver.close();
    }
}

日志:

10:45:59.768 [main] INFO  resources.base - Login test with params: 
     username: mark@mark.com
     Password: mark
     Text: Restricted user
10:46:06.299 [main] INFO  resources.base - Login test with params: 
     username: fred@fred.com
     Password: fred
     Text: Non Restricted user
10:46:06.492 [main] INFO  resources.base - NavBar is visible
10:46:06.493 [main] INFO  resources.base - Closing browser
10:46:07.821 [main] INFO  resources.base - Closing browser
10:46:08.502 [main] INFO  resources.base - Closing browser

正如您从日志中看到的那样,@ AfterTest正在SUITE结束时执行每个测试(或者它对我来说是什么样的。

任何建议

TIA

标记

1 个答案:

答案 0 :(得分:2)

TestNG不是JUnit。

@AfterTest在每个<test>节点之后运行,您想要的是@AfterMethod

您应该查看文档:{​​{3}}