Selenium Webdriver sessionId or check if all browser windows are closed Selenium Webdriver sessionId or check if all browser windows are closed selenium selenium

Selenium Webdriver sessionId or check if all browser windows are closed


Just set

driver=null; 

everytime you quit the browser and than check

if (browser!=null){  //Attention: this comand is not supported  //as far as i know ;)  driver.doSomething();}

or

try{}catch (NullPointerException e)    e.printStackTrace();    System.err.print"DAMN";}

or receive a NullPointerException ;)


public bool InstanceExist    {        get        {            if (Instance != null)            {                try                {                    return (Instance.WindowHandles != null); // allways returns true if browser instance exist or thrown error                }                catch (Exception e)                {                    return false;                    // means that browser was closed by user                }            }            return false; // means that it wasn't created yet or was closed by developer programmally        }    }

You need to check 3 situations:

  1. driver wasnt created
  2. driver was closed by developer
  3. browser was closed by user, but driwer instance still exists

All of those situations is checked with this code.


I think the cleanest way to detect if all windows are closed is smth like:

boolean allWindowsClosed = webDriver.getWindowHandles().isEmpty();

getWindowHandles returns a set of window handles for all open windows - see http://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/WebDriver.html#getWindowHandles()