Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I need to check a collection of Page Objects to see, for each one, if quit() has been called on its WebDriver.

I've written the following method to check a WebDriver's state:

public static boolean hasQuit(WebDriver driver) {         try {             driver.getTitle();             return false;         } catch (SessionNotFoundException e) {             return true;         } } 

Here's the problem: I don't like having to throw and catch an exception to discover the truth of a boolean, but it seems I have no choice since the WebDriver API doesn't provide a method to check if the driver has quit.

So my question is, is there a better way to check if a WebDriver has quit?

I found a similar (and more general) question here, but the question did not have any code that had been tried, and the only answer was to always set the WebDriver to null after quiting (which I don't necessarily have control over).

like image 674
James Dunn Avatar asked Sep 04 '13 16:09

James Dunn


People also ask

How can I tell if Selenium is running?

Or You can also check with http://localhost:4444/selenium-server/driver/?cmd=getLogMessages If serveris runnning then it will show 'ok' in browser.

What does quit () do in Selenium?

quit() method closes all browser windows and ends the WebDriver session.


1 Answers

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)"); 
like image 125
Dingredient Avatar answered Oct 02 '22 17:10

Dingredient