Selenium

How to Close a Browser in Selenium

In this article, we will discuss about the methods to close the browser. Selenium supports two methods to close the browser: (1) close() method and (2) quit() method. But both are used in different situations. We will explain how and where we use each of them with an implemented example. This article implements two approaches to check whether the WebDriver session ID is active or null.

How to Close a Browser in Selenium

Closing the browser is one of the operations when we finish the website verification testing. The Selenium framework also provides the commands to close the entire browser or tabs after finishing the Automation Testing with the help of WebDriver.

Selenium supports two methods to close the browsers: (1) driver.close() and (2) driver.quit(). How and when each of the methods is used are explained in the following:

Close() Method

The close() method is one of the operations that is supported by Selenium to close the browser window which is in focus. If there are multiple tabs that are open in the browser, the close() method, with the help of WebDrivers, only closes the focused tab on which the Selenium Automation Test is currently running and the rest of the tabs are not closed. The close() method only closes the current tab but does not destroy the WebDriver session.

The syntax to close the browser window is given in the following:

driver.close()

Where the “driver” is the instance of Webdriver.

Quit() Method

The Selenium framework supports the quit() operation to close all the open tabs of the browser. Quit() method enables you to close all the tabs that are open in the browser. The browser WebDriver uses the quit() method to end the WebDriver session which is currently active.

The quit() method not only closes all opened browser tabs but terminates the Webdriver sessions by calling the driver.dispose method. In case the Webdriver does not destroy the session after closing the browser windows, there are chances of leaking memory files and errors to occur.

The syntax to close the browser window is given in the following:

driver.quit()

Where the “driver” is the instance of Webdriver.

Approach 1: Close the Current Browser Window While Keeping the WebDriver Session Alive

In Selenium, the close() method enables the Webdriver session alive after closing the current tab. Let’s take a scenario where we open the browser and close the currently focused tab then check whether the session is not closed. To do so, we use the https://www.ebay.com/ website.

Let’s begin to implement!

First, create the Java file with the name as Close_browser.java. Also, select the option of the static void class. Then, the Java file automatically creates the public class with the file name and generates a public static void main class. The code is mentioned as follows:

public class Close_browser {

   public static void main(String[] args) {

   }

}

Now, let’s import the necessary packages of WebDriver, SessionId, RemoteWebDriver, and time.Duration for implicit class.

Here is the code for the necessary import package:

import java.time.Duration;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.remote.RemoteWebDriver;

import org.openqa.selenium.remote.SessionId;

You can refer to the whole code that we provided in the following. Following that, we break down this code into small chunks so you could understand what function each one serves.

package ui;
import java.time.Duration;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.SessionId;
public class Close_browser {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "C:\\browserdrivers\\chromedriver.exe");
        ChromeDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
               driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
        driver.get("https://www.ebay.com/");
        System.out.println("Open the Ebay website in Chrome browser");
        driver.close();
        System.out.println("Close the Browser");
        SessionId id = ((RemoteWebDriver)driver).getSessionId();
        System.out.println("Get Session Id after close the method:" + id);
    }
}

Let’s now break down the previous code line by line and explain how each line works.

For this scenario implementation, we use the Chrome browser. In the initial section of the code, access the local directory of the Chrome drivers before starting the Chrome browser drivers.

The following syntax is used to implement it:

System.setProperty("webdriver.chrome.driver","C:\\browserdrivers\\chromedriver.exe");

ChromeDriver driver = new ChromeDriver();

After that, we maximize the browser window using the following given syntax:

driver.manage().window().maximize();

Keep in mind that loading all elements of the web page, this function waits until the implicit wait is given for the driver as we have given in our following code:

driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));

driver.get("https://www.ebay.com/");

The implictlyWait() is the method to perform implicit wait. The duration function mentions the time lag, the “of Second” is the time unit, while the program waits 10 seconds for the elements to load and open the requested URL website.

After the requested website opens in the browser, print the “Open the Ebay website in Chrome browser” into the console.

Here is the syntax to print the text into the console:

System.out.println("Open the Ebay website in Chrome browser");

In this chunk of code, we now close the currently opened browser window using the close() method.

Here is the syntax code to close the current browser window and print the “Close the Browser” text into the console:

driver.close();

System.out.println("Close the Browser");

Now, the final chunk of code refers to checking if the session is destroyed or not:

SessionId id = ((RemoteWebDriver)driver).getSessionId();

In the previously-given code, the RemoteWebDriver class is used to retrieve the session ID of the WebDriver. A WebDriver server is used to communicate with a remote web browser using the RemoteWebDriver class, a subclass of the WebDriver class. The getSessionId() method is used to fetch the session ID if active.

System.out.println("Get Session Id after close the method:" + id);

The system.out.println() is the Java function which is used to print the string and variable values. Here, we print the session ID of the WebDriver.

In the following screenshot, we can see the output result of the executed code:

As you can observe, after running the previous code, the close() method closes the current working browser tab. But still, the WebDriver session ID is activated which is printed into the previous console.

Approach 2: Close Every Browser Window and End the WebDriver Session

In Selenium, the quit() method enables the Webdriver session to end and close all the opened tabs. Let’s take the scenario, where we will open the Chrome browser and close all the opened tabs in the chrome browser then check whether either session id becomes null.

Let’s begin to implement!

You can refer to the whole code that we provided in the following illustration. Following that, we break down this code into small chunks so you could understand what function each one serves.

package ui;
import java.time.Duration;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.SessionId;
public class Close_browser {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "C:\\browserdrivers\\chromedriver.exe");
        ChromeDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
               driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
        driver.get("https://www.ebay.com/");
        System.out.println("Open the Ebay website in Chrome browser");
        driver.quit();
        System.out.println("Close the Browser");
        SessionId id = ((RemoteWebDriver)driver).getSessionId();
        System.out.println("Get Session Id after close the method:" + id);
    }
}

Let’s now break down the provided code line by line and explain how each line works.

For this scenario implementation, we repeat all the same steps of approach #1. To launch the browser, maximize the browser and implement the implicit wait.

In this chunk of code, we now close all the opened browsers window using the quit() method.

Here is the syntax code to close all the browsers window and print the “Close the Browser” text into the console:

driver.quit();

System.out.println("Close the Browser");

Now, the final chunk of code refers to checking if the session is destroyed or not:

SessionId id = ((RemoteWebDriver)driver).getSessionId();

In the previously-given code, the RemoteWebDriver class is used to retrieve the session ID of the WebDriver. A WebDriver server is used to communicate with a remote web browser using the RemoteWebDriver class, a subclass of the WebDriver class. The getSessionId() method checks if the session is null.

System.out.println("Get Session Id after close the method:" + id);

The system.out.println() is the Java function which is used to print the string and variable values. Here, we print the session ID of the WebDriver.

In the following screenshot, we can see the output result of executed code:

As you can observe, after running the previous code, the quit() method closes all the opened browser tabs and destroys the WebDriver session that is printed into the previous console which is null.

Conclusion

You learned the two methods of closing the browser: close() and quit() methods. This article explained both methods in detail on where to use the close() method and where to use the quit() method. Then, we implemented both approaches with the example to check whether the WebDriver session ID is alive or not. This tutorial is best for both beginner and experienced developers.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content