selenium: What if user close the browser or webdriver? how can I detect if the browser is closed?
Asked Answered
G

2

5
  • OS:Window 10

  • Browser: Chrome webDriver

  • Browser Version: Chrome 63.0.3239.10(64bit)

  • Selenium Version 2.44

Added below dependency :

<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>2.44.0</version>
</dependency>

Expected Behavior -

I want check if the driver is closed by user directly, and restart the webdriver if there is no browser.

Before Driver webdriver = new ChromeDriver() codes, the webdriver is null state,

but after Driver webdriver = new ChromeDriver() code finished, even if user close the browser, webdriver is not destroyed.

so, after user close the browser, all code related to webdriver has the error:: "unreachable Exception".

I want restart the webdriver, if the browser is closed by user, but I can not detect the situation.

  1. driver!=null code is not working, because driver still exist after browser is closed by user
  2. if((driver.getWindowHandle().equals("")) is not working, because Chrome Unreachable Exception, because browser is closed by user

What I want to do is to check if the browser is gone, because of user?

Gherkin answered 21/12, 2017 at 23:40 Comment(2)
Hi. Did you check this answer? You may find something using the toString() method (driver.toString().contains("null")) #27616970Gluteus
But what remains unclear to me is how we can check whether the browser were closed by the user... Selenium will close it, but not like a "real user".Gluteus
A
7

You can perform any action on driver object, if it is throwing UnreachableBrowserException, then there is problem to communicate with the browser.

The most common causes for this exception are:

  1. The provided server address to RemoteWebDriver is invalid, so the connection could not be established.
  2. The browser has died mid-test.

And you can call the following method to verify the browser is closed or not.

public boolean isBrowserClosed(WebDriver driver)
{
    boolean isClosed = false;
    try {
        driver.getTitle();
    } catch(UnreachableBrowserException ubex) {
        isClosed = true;
    }

    return isClosed;
}
Alforja answered 22/12, 2017 at 5:41 Comment(1)
other commands can be used to check if the browser is alive too, e.g. cookie retrieval.Comfit
M
-1

Addressing your queries:

  • What if user close the browser or webdriver : First of all automated test execution shouldn't be interuptted by manual intervention. It's against all the best practices. If you forcefully close the Web Browser then WebDriver will throw org.openqa.selenium.WebDriverException as follows :

     Exception in thread "main" org.openqa.selenium.WebDriverException: Process unexpectedly closed with status: 0
     Build info: version: '3.8.1', revision: '6e95a6684b', time: '2017-12-01T19:05:32.194Z'
     System info: host: 'foo', ip: 'foo', os.name: 'foo', os.arch: 'foo', os.version: 'foo', java.version: 'foo'
     Driver info: driver.version: FirefoxDriver
     remote stacktrace: stack backtrace:
        0:           0x47e934 - <no info>
        1:           0x47f0a3 - <no info>
        2:           0x442649 - <no info>
        3:           0x449cc3 - <no info>
        4:           0x42a890 - <no info>
        5:           0x406f5e - <no info>
        6:           0x40cfc9 - <no info>
        7:           0x6bef19 - <no info>
        8:           0x420756 - <no info>
        9:           0x6b96e0 - <no info>
       10:      0x7fa0fb01842 - BaseThreadInitThunk
    
  • How can I detect if the browser is closed? : If the Automation Script handles proper initiation and closure of Web Browser you don't need to explicitly validate if _browser is closed or not. Cross checking dead Web Browser and Web Browser session / chores would be a pure overhead. So a better practice would be to write clean code

  • Restart the webdriver if there is no browser : Should be handled by the Automation Script through WebDriver and Web Browser initialization.

  • User close the browser, webdriver is not destroyed : As you WebDriver and Web Browser was started through you Automation Script an user closing the Web Browser would be a malpractice. Web Browser closure must be handled by the the Automation Script.

  • I want restart the webdriver : Neither you can connect to the previous instance of the WebDriver nor to the previous instance of the Web Browser. You have to reinitialize again as follows :

     WebDriver driver =  new ChromeDriver();
    

See the discussion How can I reconnect to the browser opened by webdriver with selenium? for details.

Mares answered 22/12, 2017 at 9:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.