The Issue

Sometimes, when there is a need to run hundreds of automated tests, there will be cached memory every time a test is executed. Closing only the current tab/window of the web browser might increase the memory used by the system, and lead to memory leaks.

The Answer

The above-mentioned memory problem can be resolved with Selenium by using a function: quit().

There is another function in Selenium which is close() which closes only a single tab/window according to our requirement if we do not need to manage the cached memory completely. You can find the functionality of the close() function in our previous post, How To Use Selenium Function – Close().

The Code

Now, an example of how to use quit() function. The quit() function invokes another function called dispose(), which is responsible for flushing all the browser windows.

The Result

Steps performed by using the above-written code are

  • Opens a new browser
  • Navigates to “3Qi Labs website”
  • Opens a new tab and switches to this new tab
  • Navigates to “Google Home Page”
  • Opens a second new tab and switches to this second new tab
  • Navigates to “YouTube Home Page”
  • Quits the entire browser closing all the tabs

The Takeaway

Selenium function quit() enables the user to terminate an entire browser (thereby closing all tabs) session. This can prevent memory leaks and issues when running a significant number of automated test cases.