Closing Browser Sessions in Selenium WebDriver

Closing Browser Sessions in Selenium WebDriver

16 July 2024 Stephan Petzl Leave a comment QA

When working with Selenium WebDriver in your automated tests, properly closing browser sessions is crucial to avoid memory leaks and ensure that your tests run smoothly. This article will guide you through the best practices for closing browser sessions in Selenium WebDriver, particularly focusing on Python.

Understanding the Difference Between driver.close() and driver.quit()

In Selenium WebDriver, there are two primary methods for closing browser sessions:

  • driver.close() – This method closes the browser window on which the focus is set. If there are multiple tabs or windows open, driver.close() will only close the current window.
  • driver.quit() – This method calls driver.dispose(), which closes all the browser windows and ends the WebDriver session gracefully. It ensures that all resources are cleaned up, preventing any memory leaks.

For most cases, especially when you want to end the test program, using driver.quit() is recommended as it closes all opened browser windows and terminates the WebDriver session properly.

Implementing Proper Browser Closure

Below is an example of how to correctly implement browser closure in a Selenium test using Python:

from selenium import webdriver

class TestFoo(unittest.TestCase):
    
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.get('http://example.com')
    
    def tearDown(self):
        self.driver.quit()

    def test_example(self):
        # Your test code here
        pass
    

In this example, the WebDriver session is started in the setUp method and properly closed using driver.quit() in the tearDown method. This ensures that no resources are left hanging after the test execution.

Avoiding Common Pitfalls

A common mistake is creating a new WebDriver session in the tearDown method, which can lead to multiple sessions being opened and not closed properly. Ensure that the session created in setUp is the one being closed in tearDown.

class TestFoo(unittest.TestCase):
    
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.get('http://example.com')
    
    def tearDown(self):
        self.driver.quit()

    def test_example(self):
        # Your test code here
        pass
    

By following this pattern, you maintain a single WebDriver session per test case, ensuring proper resource management.

Using Repeato for No-Code Test Automation

If you’re looking for a more efficient way to manage your automated tests without delving into code, consider using Repeato. Repeato is a no-code test automation tool for iOS and Android that helps you create, run, and maintain automated tests for your apps. It leverages computer vision and AI to provide a fast and user-friendly testing experience.

With Repeato, you can easily set up and manage your test cases, ensuring high-quality assurance for your applications without the need for extensive coding knowledge. Learn more about Repeato and its capabilities on our documentation page.

Like this article? there’s more where that came from!