Selenium

How to Close the Browser in Selenium

Closing the browser after finishing a verification is a usual practice when testing an application. This is also followed while we do automation testing using Selenium.

Today, we shall discuss various methods to close the browser.

Illustration Scenario

Let us take a scenario where we shall close a browser and then check if the webdriver session-id is null or not.

Approach 1: Using close() Method

The first approach is using the close() method. It closes the active web browser. However, after closing the browser, the webdriver session does not terminate(session-id remains not null).

Implementation
Let us have a Java file BrwClose.java with the below code.

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.SessionId;
import java.util.concurrent.TimeUnit;

public class BrwClose {
    public static void main(String[] k) {

        System.setProperty("webdriver.chrome.driver", "chromedriver");
        WebDriver brw = new ChromeDriver();
        brw.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        brw.get("https://www.selenium.dev/");
        brw.close();
        SessionId id = ((RemoteWebDriver) brw).getSessionId();
        System.out.println("Get session Id after close method: " + id);
    }

}

After completing the implementation, we need to save and run this Java file.

The above code, lines 2 to 6, are the Java imports needed for Selenium.

Lines 8 and 9 describe the name of the class and static object declaration.

In line 11, we instruct the Selenium webdriver to search for the chrome driver executable file within the project directory.

In lines 12 to 14, we first create a Selenium webdriver object and store it in the brw variable. Then, we have introduced an implicit wait for the webdriver object for three seconds. Lastly, we are opening the https://www.selenium.dev/ application in the Chrome browser.

In line 15, we have closed the Chrome browser using the close() method.

Lines 16 to 17 are used to capture the webdriver session’s current session-id after closing the browser using the getSessionId() method.

Output
On running the above code, we have obtained the text – Get session Id after close method: 83e173edb5467b19560ac9368c4dc51- as output. Thus we see that the active browser gets closed, but the webdriver session remains alive(as we get a non-null session-id).

Approach 2: Using quit() Method

The second approach is using the quit() method. It closes every browser which is opened. Also, after closing the browser, the webdriver session-id becomes null.

Implementation with Approach 2
Let us have a Java file BrwQuit.java with the below code.

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.SessionId;
import java.util.concurrent.TimeUnit;

public class BrwQuit {
    public static void main(String[] k) {

        System.setProperty("webdriver.chrome.driver", "chromedriver");
        WebDriver brw = new ChromeDriver();
        brw.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        brw.get("https://www.selenium.dev/");
        brw.quit();
        SessionId id = ((RemoteWebDriver) brw).getSessionId();
        System.out.println("Get session Id after quit method: " + id);
    }

}

Post the implementation; we require to save and run this Java file.

The above code, lines 2 to 6, are the Java imports needed for Selenium.

Lines 8 and 9 describe the class name and static object declaration.

In line 11, we instruct the Selenium webdriver to search for the chrome driver executable file within the project directory.

In lines 12 to 14, we first create a Selenium webdriver object and store it in the brw variable. Then, we have introduced an implicit wait for the webdriver object for three seconds. Lastly, we are opening the https://www.selenium.dev/ application in the Chrome browser.

In line 15, we have closed the Chrome browser using the quit() method.

Lines 16 to 17 are used to capture the webdriver session’s current session-id after quitting the browser using the getSessionId() method.

Output
We have obtained the text – Get session Id after quit method: null – as an output on running the above code. Thus we see that the browser gets closed, and the webdriver session terminates(as we get a null session-id).

Conclusion

Thus we have seen how to close the browser using Selenium. First, we have used the close() method, and the second approach is based on the quit() method. We have also seen how the quit() method helps to conclude a Selenium webdriver session.

About the author

Debomita Bhattacharjee