Fofole
Fofole

Reputation: 3568

Selenium catch popup on close browser

I'm trying to test that when I close my window a popup shows with a warning message.

I've tried both driver.close() and driver.quit() after making sure I'm on the proper window but this just terminates the process since my popup doesn't show.

I could test it by using the awt Robot and sending alt+f4 but this doesn't seem too reliable.

What would be the proper way to do this?

Upvotes: 2

Views: 1461

Answers (2)

Manu
Manu

Reputation: 2301

Instead of using driver.quit() to close the browser, closing it using the Actions object may work for you. This is another way to close the browser using the keyboard shortcuts.

Actions act = new Actions(driver);
act.sendKeys(Keys.chord(Keys.CONTROL+"w")).perform();

Or, if there are multiple tabs opened in driver window:

act.sendKeys(Keys.chord(Keys.CONTROL,Keys.SHIFT+"w")).perform();

Upvotes: 2

Vicky
Vicky

Reputation: 3021

You need to switch into the alert if the alert is displayed accept or dismiss it and then call driver.quit();

if(driver.switchTo().alert() != null)//switches only if alert is displayed
{
    Alert alert = driver.switchTo().alert();
    alert.dismiss(); // alert.accept();

}
driver.quit();

Hope this helps you...kindly get back if it is not working for you

Upvotes: 0

Related Questions