0
votes

Learning selenium and TestNG

I have a single testNG.xml file that file specifies 3 tests

Each test (sample below) has an @AfterTest annotation and a method that closes the browser. The browsers all stay open until the SUITE has completed. I thought @AfterTest ran after each test, so why doesn't treadown get called after each test

Test:

    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();
    }
}

Log:

10:45:59.768 [main] INFO  resources.base - Login test with params: 
     username: [email protected]
     Password: mark
     Text: Restricted user
10:46:06.299 [main] INFO  resources.base - Login test with params: 
     username: [email protected]
     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

As you can see from the log, the @AfterTest is executing fro each test at the end of the SUITE (or that's what it looks like to me.

Any suggestions

TIA

Mark

1

1 Answers

2
votes

TestNG is not JUnit.

@AfterTest is run after each <test> node and what you want is @AfterMethod.

You should have look at the documentation: http://testng.org/doc/documentation-main.html