In Java, best way to check if Selenium WebDriver has quit In Java, best way to check if Selenium WebDriver has quit selenium selenium

In Java, best way to check if Selenium WebDriver has quit


If quit() has been called, driver.toString() returns null:

>>> FirefoxDriver: firefox on XP (null))

Otherwise, it returns a hashcode of the object:

>>> FirefoxDriver: firefox on XP (9f897f52-3a13-40d4-800b-7dec26a0c84d)

so you could check for null when assigning a boolean:

boolean hasQuit = driver.toString().contains("(null)");


There are basically two approaches you can take, depending on your situation.

Situation 1: Extending WebDriver is not a realistic option.

This is the most common situation. Most developers working with Selenium in real-work situations who want a handy way of telling if a WebDriver has quit are working in an already established testing framework, and trying to refactor the framework to ensure that only your custom extensions of WebDrivers are used is probably more trouble than it's worth.

In this case, Sajan's answer and Gili's recommendation of his answer won't be useful, because overriding RemoteWebDriver#stopClient() is not an option. (Besides, even if it was, most people are looking for a simple answer.)

As long as you are only using standard implementations of WebDriver that come with Selenium (FirefoxDriver, ChromeDriver, InternetExplorerDriver, SafariDriver, etc.), you can cast the WebDriver to a RemoteWebDriver and then check if the sessionId is null (Pat was on the right track, but a direct call to sessionId is better than using toString()).

public static boolean hasQuit(WebDriver driver) {    return ((RemoteWebDriver)driver).getSessionId() == null;}

This answer should be good for 95% of all cases, because how often are you going to use a WebDriver that doesn't implement RemoteWebDriver? (Not very often.)

Situation 2: You CAN realistically extend your WebDriver.

This situation is less common, but perhaps you are either:
    (a)  Working with a well-designed and abstracted framework, or
    (b)  Starting a selenium test framework from scratch.

In this case, you can create your own interface that extends WebDriver:

public interface CustomDriver extends WebDriver {    boolean hasQuit();}

And then you can extend standard WebDrivers like so (in this example, ChromeDriver):

public class CustomChromeDriver extends ChromeDriver implements CustomDriver {    boolean hasQuit = false;    @Override    public boolean hasQuit() {        return hasQuit;    }    @Override    public void stopClient() {        super.stopClient();        hasQuit = true;    }}


StopClient method will be invoked after quit (RemoteWebDriver Source) is called, may be you can subclass your instance of RemoteWebDriver and override stopClient method, set some flag and check for the flag to determine if the webdriver is closed (quit).