Quit() vs Close() in Selenium WebDriver

I have been seeing a lot of questions everywhere and want to discuss about Quit() vs Close()

Close()

Use the following code.

//initiates the FirefoxDriver
WebDriver driver = new FirefoxDriver();
//Opens the Google Page
driver.get("http://google.com");
//Once on the Google page right click on any link (say images) and open it in a new tab
Thread.sleep(20000l);
//Now we have two browser windows opened with this webdriver instance. So when driver.close() is called it will
//close only the parent browser window, it will not close the new tab window which we opened.
driver.close();

Quit()

Use the following code.

//initiates the FirefoxDriver
WebDriver driver = new FirefoxDriver();
//Opens the Google Page
driver.get("http://google.com");
//Once on the Google page right click on any link (say images) and open it in a new tab
Thread.sleep(20000l);
//Now we have two browser windows opened with this webdriver instance. So when driver.<del>close</del>quit() is called it will
//close all browser windows, it will close both of the opened browser windows associated with the driver object..
driver.quit();

Thanks,
Khaja

Advertisements

2 thoughts on “Quit() vs Close() in Selenium WebDriver

  1. I would like to correct the statement given by Khaja on Quit() and Close():

    Quit() – will close all opened browsers which are opened by the WebDriver. But,
    Close() – will close only last/recently opened browser by the WebDriver (i.e. last child window).

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s