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: