I am currently refining my test automation framework and I'm confused about the best practices for terminating a session. I noticed that sometimes my browser windows remain open after a test suite finishes, while other times the entire execution stops abruptly. What exactly is the technical distinction between driver.close() and driver.quit()? Specifically, which one should I use to manage memory effectively when dealing with multiple browser tabs or windows during a heavy CI/CD pipeline execution?
3 answers
The primary difference lies in the scope of the termination. driver.close() is used to close the current browser window or tab that has the focus of the WebDriver. If only one window is open, it might appear to end the session, but the WebDriver process itself may remain active. In contrast, driver.quit() is the "clean-up" command; it closes all associated windows, tabs, and most importantly, it terminates the WebDriver process and background services entirely. At iCertGlobal, we recommend using driver.quit() at the end of every test suite execution. This ensures that resources like chromedriver.exe are released from system memory, preventing "zombie processes" from slowing down your local machine or automation server.
Are you specifically working with a multi-tab application where you need to close just the current child window and return control to the parent window, or are you looking for a global teardown method?
Think of close() as closing a tab and quit() as closing the entire browser and stopping the engine. Always use quit() at the very end of your script to be safe.
I agree with Nancy. I’ve seen automation servers crash because developers only used close(), leaving hundreds of hidden driver processes running in the background. Using quit() is definitely the gold standard for stable automation.
Steven, that's an important point for anyone handling pop-up windows. If you use driver.quit() while trying to just close a single pop-up, you'll kill the entire test run. For multi-window scenarios, you’d use driver.close() for the specific tab and then use driver.switch_to.window(original_handle) to keep the session alive. But once the final assertion is done, always follow up with driver.quit() in the @AfterTest or tearDown method to avoid memory leaks.