ericmckean / chromedriver

Automatically exported from code.google.com/p/chromedriver
0 stars 0 forks source link

Cannot interact with beforeUnload dialog when closing last Chrome window #901

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
If a page would open a beforeUnload dialog when closing, the 
ChromeDriver.close() command simply closes the window and invalidates the 
session if it is the last window. This makes it impossible to interact with the 
dialog that should have displayed.

Using the sample HTML:
<html><body>
<script>
window.onbeforeunload = function() {
    return 'You have unsaved changes!';
}
</script>
</body></html>

Run code like the following:
driver.get("<url-to-sample-page>");
driver.close();
WebDriverWait wait = new WebDriverWait(driver, 2);
wait.until(ExpectedConditions.alertIsPresent());
driver.switchTo().alert().accept();

Expected behavior:
All lines of code should run, no exception should be thrown. The dialog should 
be accepted and the browser should then close.

Actual behavior:
An exception is thrown when trying to test if the alert is present:
org.openqa.selenium.remote.SessionNotFoundException: no such session
  (Driver info: chromedriver=2.10.267521,platform=Windows NT 6.1 SP1 x86_64) (WARNING: The server did not provide any stacktrace information)

Selenium version: 2.10.267521
Chromedriver version: 2.42.2
OS: Windows 7 64-bit (Windows NT 6.1 SP1 x86_64)
Browser: Chrome
Brower version: 37.0.2062.120

Original issue reported on code.google.com by para.sel...@gmail.com on 15 Sep 2014 at 7:52

GoogleCodeExporter commented 9 years ago

Original comment by samu...@chromium.org on 21 Feb 2015 at 12:18

GoogleCodeExporter commented 9 years ago

Issue is reproducible when Html contains beforeUnload functionality.
ChromeDriver:2.14, Chrome:v41, Windows 7

When user tries to navigate to different page from current page, 'Confirm 
navigation' message is displayed, on which user is allowed to select
'Leave page' and 'Stay on page'.

Through chromedriver, we are unable to select 'Leave page' or 'Stay on page' by 
using alert.accept() / alert.dismiss().

But through FirefoxDriver user able to select 'Leave page' or 'Stay on page' by 
using alert.accept() / alert.dismiss()

Html:
<html>
<head></head>
<body>
Test Before Unload
<script>
var myEvent = window.attachEvent || window.addEventListener;
var chkevent = window.attachEvent ? 'onbeforeunload' : 'beforeunload'; /// make 
IE7, IE8 compitable
myEvent(chkevent, function(e) { // For >=IE7, Chrome, Firefox
var confirmationMessage = 'Before unload confirmation message'; // a space
(e || window.event).returnValue = confirmationMessage;
return confirmationMessage;
});
</script>
</body></html>

Java:
String url = "Html/Bug_901.html";
String driverPath="drivers/chromedriver.exe";

System.setProperty("webdriver.chrome.driver",driverPath);
WebDriver driver = new ChromeDriver();
driver.get(url);
driver.close();

WebDriverWait wait = new WebDriverWait(driver, 2);
wait.until(ExpectedConditions.alertIsPresent());
Alert alert=driver.switchTo().alert();
alert.accept();

Original comment by ssudunag...@chromium.org on 25 Mar 2015 at 5:29

GoogleCodeExporter commented 9 years ago

Original comment by gmanikp...@chromium.org on 25 Mar 2015 at 4:40