Skip to main content

Selenium driver.close()

  • WebDriver's close() method closes the web browser window that the user is currently working on or we can also say the window that is being currently accessed by the WebDriver.
  • The command neither requires any parameter nor does it return any value.
  • When you use driver.close(), only the window that has focus is closed. It is better to use driver.close() when you are dealing with multiple tabs or windows.

Selenium driver.quit()

  • Unlike close() method, quit() method closes down all the windows that the program has opened.
  • Same as close() method, the command neither requires any parameter nor does is return any value. 
  • driver.quit() is used to exit the browser, end the session, tabs, pop-ups etc.
  • quit() is a webdriver command which calls the driver.dispose method, which in turn closes all the browser windows and terminates the WebDriver session.
  • If we do not use quit() at the end of program, the WebDriver session will not be closed properly and the files will not be cleared off memory. This may result in memory leak errors.

Example

public class CloseAndQuit 
{
    WebDriver driver;
    @BeforeClass
    public void befClass()
    {
        System.setProperty("webdriver.chrome.driver",System.getProperty("user.dir") + "//drivers//chromedriver");
        driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
        driver.get("http://automationpractice.com/index.php?id_product=1&controller=product");
    }
	
    @Test
    public void MyMethod() throws AWTException, InterruptedException
    {
        driver.findElement(By.cssSelector("button[class='btn btn-default btn-twitter']")).click();
        driver.close();
    }
}

Above code will open product page of automation practice website and click on Twitter button, which further will open a new Twitter Window. Hence, there will be 2 windows one with automationpractice.com another with Twitter. Once driver.close() command will execute it will close automationpractice.com window and twitter window will remain open.

Now,  replace driver.close() with driver.quit(). You will find that both Windows automationpractice.com and twitter window are closed. Similar will be the case if you are dealing with multiple tabs with driver.quit() all tabs will be closed and with driver.close() currently focus tab will be closed.

If the Automation process opens only a single browser window, the close() and quit() commands work in the same way. Both will differ in their functionality when there are more than one browser windows or tabs opened during Automation.

Video Tutorial

About

Elix is a premium wordpress theme for portfolio, freelancer, design agencies and a wide range of other design institutions.