0

I am running tests with WebDriver, when a test fails, the browser does not close. On a Windows machine this is a huge problem because I then have several instances of the Firefox still running in the background. Kindly advise

Here's the code :

public static WebDriver driver;
    private String sTestCaseName;

    @BeforeMethod
    public void beforeMethod() throws Exception {
        DOMConfigurator.configure("log4j.xml");
        sTestCaseName = Constant.Login_Name;
        Log.startTestCase(sTestCaseName);
        new BaseClass(driver);
    }

    @Test(description = "Login", enabled = true)
    public void TestLogin_Success() throws Exception {
        try {
            driver = new FirefoxDriver();
            LoginBuilder.Execute(driver);
            Log.info("Successfully Login!"); 
        } catch (Exception e) {

            Log.error(e.getMessage());
            throw (e);
        }

    }

    @Test(description = "Login_Failed", enabled = true)
    public void TestLogin_Failed() throws Exception {
        try {
            driver = new FirefoxDriver();
            LoginBuilder.Execute_Failed(driver);
            Log.info("Unsuccessfully Login!"); 
        } catch (Exception e) {

            Log.error(e.getMessage());
            throw (e);
        }

    }

    @AfterMethod
    public void afterMethod() {
        Log.endTestCase(sTestCaseName); 
        driver.close();
    }
Jayden Ng
  • 141
  • 1
  • 1
  • 14

3 Answers3

0

Add finally block to your try catch block. Close the WebDriver there.

Sample code snippet

try {
    ....
    ....
} catch(Exception e) {
    ....
    ....
} finally {
    ....
  driver.close();
}

More info on Dispose, Close and Quit - Difference between webdriver.Dispose(), .Close() and .Quit()

Community
  • 1
  • 1
Clement Amarnath
  • 5,301
  • 1
  • 21
  • 34
0

Call driver.quit() instead of driver.close()

@AfterMethod
public void afterMethod() {
    Log.endTestCase(sTestCaseName); 
    driver.quit();
}
Mobrockers
  • 2,128
  • 1
  • 16
  • 28
0

Why don't you try to use @AfterClass

Jobin
  • 5,610
  • 5
  • 38
  • 53