Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between webdriver.Dispose(), .Close() and .Quit()

What is the difference between these

  1. Webdriver.Close()
  2. Webdriver.Quit()
  3. Webdriver.Dispose()

Which one to be used and when?

like image 813
Puran Joshi Avatar asked Feb 25 '13 12:02

Puran Joshi


People also ask

What is difference between close and quit in WebDriver?

close() closes only the current window on which Selenium is running automated tests. The WebDriver session, however, remains active. On the other hand, the driver. quit() method closes all browser windows and ends the WebDriver session.

What is driver Dispose ()?

driver. dispose - As mentioned previously, is an internal method of WebDriver which has been silently dropped according to another answer - Verification needed. This method really doesn't have a use-case in a normal test workflow as either of the previous methods should work for most use cases.

What is the similarity between WebDriver close () and quit () methods?

What is the similarity between WebDriver's close() and quit() methods? closes the active web browser window. closes all opened web browser windows. does not accept arguments.

Can we use close and quit together in Selenium?

In Selenium Webdriver, there are two methods to close a browser window, close() and quit() . These methods are often used interchangeably, but they have different functions.


1 Answers

This is a good question I have seen people use Close() when they shouldn't. I looked in the source code for the Selenium Client & WebDriver C# Bindings and found the following.

  1. webDriver.Close() - Close the browser window that the driver has focus of
  2. webDriver.Quit() - Calls Dispose()
  3. webDriver.Dispose() Closes all browser windows and safely ends the session

The code below will dispose the driver object, ends the session and closes all browsers opened during a test whether the test fails or passes.

public IWebDriver Driver;  [SetUp] public void SetupTest() {     Driver = WebDriverFactory.GetDriver(); }  [TearDown] public void TearDown() {     if (Driver != null)       Driver.Quit(); } 

In summary ensure that Quit() or Dispose() is called before exiting the program, and don't use the Close() method unless you're sure of what you're doing.

Note
I found this question when try to figure out a related problem why my VM's were running out of harddrive space. Turns out an exception was causing Quit() or Dispose() to not be called every run which then caused the appData folder to fill the hard drive. So we were using the Quit() method correctly but the code was unreachable. Summary make sure all code paths will clean up your unmanaged objects by using exception safe patterns or implement IDisposable

Also
In the case of RemoteDriver calling Quit() or Dispose() will also close the session on the Selenium Server. If the session isn't closed the log files for that session remain in memory.

like image 105
rcasady616 Avatar answered Sep 20 '22 18:09

rcasady616