lundi 1 août 2016

Java Selenium close browser after assertTrue

I have a code with many classes.
There is a class which creates the driver -

public class DriverDelegate {

    private String baseURL = "someLink";
    private WebDriver driver;
    private WebDriverWait wait;

    DriverDelegate(String url) {
        System.setProperty("webdriver.chrome.driver", "${directory}");
        driver = new ChromeDriver();
        driver.get(baseURL + url);
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        wait = new WebDriverWait(driver, 5);
    }

    public WebDriver getDriver() {
        return driver;
    }

I create new driver for every test. And most of my lines are the ones containing assertTrue like this-

public class UserInterfaceTests extends BaseTest{


    @Test
    public void headerClicker() throws java.lang.Exception {

        //Startup
        DriverDelegate driverDelegate = new DriverDelegate("url");
        WebDriver driver = driverDelegate.getDriver();

        //Some random assertTrue
        assertTrue("Test(HeaderClicker) - NoSuchElementException click", TestHelper.headerClicker(schedule, driver));

        //I hope that it is not neccessary to put up all helper classes like TestHelper or BaseTest

Now I launch my tests from a class called Startup -

public class Startup{
    @Test
    public void HeaderClicker() throws Exception{ UserInterfaceTests UI = new UserInterfaceTests(); UI.headerClicker();}

My question here is how to close the browser after the assertTrue fails. Things like @AfterTest, @AfterSuite etc do not work because other methods can not use the same driver that was used in the test.
Any ideas?

Aucun commentaire:

Enregistrer un commentaire