What is driver.close(); and driver.quit(); in Selenium WebDriver?

Driver.close and driver.quit are two different methods which are used to close the browser. But there is some difference in between them, as below –

1) WebDriver.Close() This method is used to close the current open window. It closes the current open window on which driver has focus on. If Suppose your Webdriver has opened more than one browser window, if you use driver.close(); then, it will close only 1 browser window on which driver is focused currently.

2) WebDriver.Quit() This method is used to destroy the instance of WebDriver. It closes all Browser Windows associated with that driver and safely ends the session. WebDriver.Quit() calls Dispose. If Suppose your Webdriver has opened more than one browser window, if you use driver.quit(); then, it will close all browser windows which are opened by that driver.

3) WebDriver.Dispose() This method closes all Browser windows and safely ends the session

You should use driver.quit whenever you want to end the program. It will close all opened browser window and terminates the WebDriver session. If you do not use driver.quit at the end of program, WebDriver session will not close properly and files would not be cleared off memory. This may result in memory leak errors.
Conclusion:
Suppose if we are working with only one window, close () and quit () technically do exactly same thing that is closes the browser/window and releases the memory by quitting the driver. But if in our test, it opens another window while execution and we wish to close one of the windows, to continue execution; we can switch the focus to desired window and perform close action on it. On other hand if we are done with the execution and we want all the browser instances to be killed and release the memory, Quit command is recommended.
Example:
package com.aayushCorp.utilities;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

import org.testng.annotations.AfterMethod;

import org.testng.annotations.BeforeMethod;


public class LoginToAayushCorp { 

                WebDriver driver;

               
                @BeforeMethod

                public void Login() throws Exception {

 
                                driver =new FirefoxDriver();
                                 driver.navigate().to("https://www.google.co.i");
                                 driver.manage().window().maximize();
 
                }             

               

                @AfterMethod

                public void TearDown(){

                                driver.close();

                                driver.quit();

                }

 

}

Leave Comment

Your email address will not be published. Required fields are marked *

Looking for learning Framework Development from Scratch? Lookout for Detailed Framework Development videos on YouTube here -

https://www.youtube.com/automationtalks

Get the Framework code at Github Repo: https://github.com/prakashnarkhede?tab=repositories