In many example Selenium C# scripts you’ll see either driver.Close() or driver.Quit() used at the end of the test. You may also see reference to driver.Dispose(). But what do they do and what’s the difference?
This method only closes the browser window that is in focus. If you’re running multiple tests and wish to keep the WebDriver session active, then use the Close() method and continue on to the next part of your test.
The Quit() method closes all browser windows that are open and also ends the WebDriver session. This should be used when you have finished testing as then any unmanaged code will be cleaned up, logs written and memory and hard drive space freed up.
Reading the API documentation we can see this is also a method under the IWebDriver interface and so can be called directly. However, calling the driver.Quit() method also calls the dispose() method. The IWebDriver interface is there to help dispose of unmanaged code, so it makes sense that it’s called from driver.Quit().
In summary of the above, you can use driver.Close() as you run your tests, but at the conclusion of testing be sure to call driver.Quit() as part of your test teardown, which is where this should ideally be.
Driver.Quit() in TearDown
When a test passes the Quit() method will be reached and executed. However, in the event of the test failing it may not be reached. Even where you use a Try-Catch block, errors may cause the test execution to stop unexpectedly.
To help ensure the Quit() method is executed, move it the TearDown:
Public void TearDown()
if (driver != null)
In this way your test will end gracefully with browsers and WebDriver session being closed and any temporary files and memory use cleaned up.
The Selenium Guys