1

The title says it all. I want to listen for a window closed event on my browser in WebDriver 4.8.1. I tried looking into WebDriverListener, but I am not having any luck. Here is my MCVE:

public class Test implements WebDriverListener {

    private Object lock = new Object();

    public Test() throws InterruptedException, MalformedURLException {
        WebDriver chrome = new ChromeDriver();
        EventFiringDecorator<WebDriver> decorator = new EventFiringDecorator<WebDriver>(this);
        WebDriver driver = decorator.decorate(chrome);

        driver.navigate().to(new URL("https://www.google.com/"));
        driver.manage().window().maximize();

        synchronized (lock) {
            lock.wait();
        }
    }

    public void beforeAnyWindowCall(Window window, Method method, Object[] args) {
        // Prints: maximize null
        System.out.println(method.getName() + " " + Arrays.toString(args));
    }

    public void beforeClose(WebDriver driver) {
        // Never is called
        System.out.println("Closing");
        synchronized (lock) {
            lock.notifyAll();
        }
    }

    public static void main(String[] args) throws InterruptedException, MalformedURLException {
        new Test();
        // Never is called
        System.out.println("Closed");
    }

}

In Swing, we have WindowListener#windowClosed(WindowEvent), which is fired when a window is closed. Can a similar thing be accomplished in Selenium WebDriver? To clarify, I am not looking to know when the WebDriver is closed, but rather when the window is (manually) closed. Also, I am not looking for a method which I call to determine if the window is closed. I want an event to fire when the is closed.

I looked at the following questions but could not find a working answer:

  1. How can I have Selenium ChromeDriver exit properly upon manually closing the window?
  2. Get browser closing event in selenium web browser
  3. How to determine when the browser is closed?
Cardinal System
  • 2,749
  • 3
  • 21
  • 42
  • 1
    Are you closing the window manually? Try to add a close() to the end (and sleep for a bit instead of lock). – cwittah Feb 28 '23 at 14:46
  • 1
    @cwittah yes, I am closing it manually. Using `WebDriver#close` *does* notify the event listener, but I'm looking to be notified when the browser is manually closed. I will update my question to be more clear. – Cardinal System Feb 28 '23 at 14:54
  • 1
    Have you tried catching the exception? An exception should be thrown when closing the window manually. See: https://stackoverflow.com/questions/31048777/get-browser-closing-event-in-selenium-web-browser – cwittah Feb 28 '23 at 14:56
  • @cwittah That answer really isn't very good. It's catching ALL exceptions, not just the one caused when a window is closed. ElementNotFoundException? Fire up a new browser. TimeoutException? Fire up a new browser. You get the idea... – JeffC Feb 28 '23 at 17:48

2 Answers2

1

You can use a method like below , when a browser is killed unexpectedly then UnreachableBrowserException exception is thrown

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

    return isClosed;
}
Abhay Chaudhary
  • 1,763
  • 1
  • 8
  • 13
  • This is not the solution I need, but I am still upvoting because it may be useful to others and it does not call `Driver#close`. – Cardinal System Mar 27 '23 at 18:54
-1

Using try-catch, something like below:

driver.get("https://www.google.com");
        
try {
       System.out.println("Browser is open : " + driver.getWindowHandle());
    }  catch(Exception e) {
       System.out.println("Browser is closed");
    }
Thread.sleep(5000);
// closed the browser manually at this stage

try {
       System.out.println("Browser is open :" + driver.getWindowHandle());
    }  catch(Exception e) {
       System.out.println("Browser is closed");
    }

Console output:

Browser is open : CDwindow-540829C626B281B2E3F66CA5F7E169D4
Browser is closed
Shawn
  • 4,064
  • 2
  • 11
  • 23
  • `catch(Exception e)` Catching all exceptions is almost always a really bad idea because it hides every exception thrown and makes debugging much more difficult. Instead, catch only the exception(s) you need to. – JeffC Feb 28 '23 at 17:45