Reputation: 219
Is there a way to see if all the browser windows are closed? I see that if you call driver.quit() or driver.close() on the WebDriver, the sessionId becomes null. Is there a way to check that?
I don't want to make a call to a closed or quit driver as it throws a WebDriverException. So I want to check to see the state of the browser before continuing on.
Upvotes: 2
Views: 18347
Reputation: 1
in C#
public void BrowserCheck()
{
try
{
var h = webDriver.WindowHandles;
return; // no problem
}
catch // browser window is closed so re-init it
{
webDriver.Quit();
WebDriverInit(); // your init code
}
}
Upvotes: 0
Reputation: 11416
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:
All of those situations is checked with this code.
Upvotes: 2
Reputation: 327
Actually calling getWindowHandles while the browser windows is gone will raise an "UnreachableBrowserException".
You have to put the call into a try-catch block and handle that error. Actually that is the only known WORKING workournd for catching unexpected browser windows closes. I have a static method in a config class doing my driver handling: I restart my browser like this:
protected static void loadPages() {
if (driver == null || driver.toString().contains("null")) { //null refers to an missing session id
driver = new FirefoxDriver();
//load all my page objects like
loginpage = new LoginPage(driver);
//....
//....
}
try {
if (driver.getWindowHandles() == null || driver.getWindowHandles().isEmpty()){ //will cause an UnreachableBrowserException if the browser really is not avalable.
try { //you actually dont need this try catch block
driver.quit();
} catch (Exception e) {
System.err.println("Quitting levtover driver did not work.");
}
driver = null; //you have to set the driver to null
loadPages();
}
} catch (UnreachableBrowserException ube) {
driver = null; //like above set to null to make sure no driver left
}
}
Now, ofc your current test will fail but you will be able to go on with the rest of them.
Upvotes: 1
Reputation: 19
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()
Upvotes: 1
Reputation: 5096
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 ;)
Upvotes: 2