Samuel French
Samuel French

Reputation: 665

Cannot close Firefox window after Selenium WebDriver test (Java)

I know there are a few other similar questions on here, but I read through them and was unable to solve my issue. I also am not entirely fluent in JUNIT annotations and the like, so that is kind of confusing me as well. Here is what I have, and I just want the firefox window to close after a successful (or unsuccessful) test.

import junit.framework.TestCase;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class TestWorkGaps extends TestCase {
private WebDriver driver;
private String baseUrl;

@Before
public void setUp() {
    driver = (WebDriver) new FirefoxDriver();
    baseUrl = "https://dev.XXX.com";
    driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
}
@Test
public void test() throws InterruptedException {
    driver.get(baseUrl);
    success = core.TestCore.checkMain(driver);
    if (!success) {
        fail("Main page did not load correctly.");
    }
//various other tasks
success = core.LoginLogout.logout(driver);
    if (!success) {
        fail("Not able to logout.");
    }
}

@After
public void closeWindow()
{
    driver.close();
    driver.quit();
}
}

Thanks in advance, you guys are the best. I can provide my pom.xml if it is relevant.

Upvotes: 1

Views: 684

Answers (1)

blalasaadri
blalasaadri

Reputation: 6188

You are mixing old and new JUnit; this is why it is not working. You have two possible ways of fixing this:

  1. The old way: remove the annotations and rename closeWindow() to tearDown(). This will override the corresponding function in TestCase.
  2. The new way: don't extend TestCase. Then the annotations will be used and the closeWindow() method will be called.

Your setUp() function already overrides the corresponding function in TestCase which is why that part worked. The closeWindow() however was unknown to TestCase and it seems JUnit uses a different runner if TestCase is extended.

Upvotes: 1

Related Questions