rshf / chromedriver

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

alert commands hang if the current target window is closed #717

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Issue Description:
When asking chromedriver to switch to an alert and it doesn't exist, it should 
throw an exception.  In specific circumstances, it will hang for 10 minutes, 
and won't even allow for a WebDriverWait timeout to interrupt it.  Eventually 
it continues as expected, minus 10 mins.

Steps to reproduce (if relevant, you MUST provide a simplified html page or
link to public site):

JAVA code to reproduce:

public class AlertBug
{
  private static WebDriver driver;

  @Test
  public void test() throws Exception
  {
    driver = new ChromeDriver();

    // Simple page with a pop up
    driver.get("http://jsfiddle.net/y3U98/show/");

    String handle = driver.getWindowHandle();

    driver.findElement(By.linkText("Popup")).click();

    // Go through handles to find string
    Set<String> winHandles = driver.getWindowHandles();
    Iterator<String> h = winHandles.iterator();

    // Find the window handle that didn't exist before
    while (h.hasNext())
    {
      String next = h.next();
      if (!next.equals(handle))
        driver.switchTo().window(next);
    }

    // Close the new window
    driver.findElement(By.linkText("Close Window")).click();

    // If you remove this sleep, an exception is thrown at switch
    // If you keep the sleep, chrome hangs for 10 mins, and then continues
    Thread.sleep(10000);

    try
    {
      driver.switchTo().alert();
    }
    catch (NoAlertPresentException ae)
    {
      // if no alert, ignore
    }

  }
}

Original issue reported on code.google.com by edw...@gmail.com on 25 Feb 2014 at 9:55

GoogleCodeExporter commented 9 years ago
Thanks for the report.
I could reproduce the issue.

Just curious, do you expect an alert to be triggered by the click which is to 
close the second window (a tab in chrome)?

Original comment by st...@chromium.org on 26 Feb 2014 at 6:22

GoogleCodeExporter commented 9 years ago
The problem within chromedriver is:
when a command related to alert is received, chromedriver tries to connect to 
the target window.

However, in this case, the target window is closed already.
For some reason, it hangs if chromedriver sends command to devtools.

Original comment by st...@chromium.org on 26 Feb 2014 at 6:23

GoogleCodeExporter commented 9 years ago
The use case for us is that the alert is conditional.  In some cases, when 
submitting a form on a pop up, the pop up just closes, and sometimes it throws 
up an alert and then closes once the alert is cleared.  In either case, we need 
to at least check to see if its there before moving on, and we usually catch 
the NoAlertPresentException and move on.  This wasn't easy to minimally 
reproduce so its been happening for many weeks now, and we just narrowed it 
down.

Without the sleep, its a race condition, depending on whether the code can 
check the alert before the browser closes.  With the sleep, it forces the bug.

Original comment by edw...@gmail.com on 26 Feb 2014 at 1:57

GoogleCodeExporter commented 9 years ago
For the moment, you may try replacing the sleep with:
    driver.getWindowHandles()
If there no alert and the window is closed, the invalid window handle will be 
removed from a cache in chromedriver and you will get a "NoSuchWindow" error.

Original comment by st...@chromium.org on 26 Feb 2014 at 4:16

GoogleCodeExporter commented 9 years ago

Original comment by st...@chromium.org on 26 Feb 2014 at 4:19

GoogleCodeExporter commented 9 years ago
Seems to be doing the trick:

if (driver.getWindowHandles().contains(driver.getWindowHandle())) 

.. catch (NoSuchWindowException e)

Original comment by edw...@gmail.com on 26 Feb 2014 at 7:19

GoogleCodeExporter commented 9 years ago
The above workaround seems to work well in ChromeDriver.  FirefoxDriver however 
treats the scenario differently, causing an UnhandledAlertException to be 
thrown, and for the alert to be cleared automatically.  Beware of this side 
effect if you're running tests on both browsers and try to implement this 
workaround.

Original comment by edw...@gmail.com on 28 Feb 2014 at 7:23

GoogleCodeExporter commented 9 years ago
An update, it seems that there is still the smallest of race conditions here 
which rears its ugly head in various scenarios:

try {
  if (driver.getWindowHandles().contains(driver.getWindowHandle()))
  {
    // Window must be open
    // EXCEPT if it closed the millesecond after executing the above statement
    // in which case the following alert will freeze for 10 minutes
    Alert alert = driver.switchTo().alert();
  }
}
catch (Exception e)
{
  // no window open
}

Original comment by edw...@gmail.com on 5 May 2014 at 6:42

GoogleCodeExporter commented 9 years ago

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

GoogleCodeExporter commented 9 years ago

Original comment by gmanikp...@chromium.org on 30 Mar 2015 at 11:38

GoogleCodeExporter commented 9 years ago

Original comment by samu...@chromium.org on 2 Apr 2015 at 10:35