I found a problem trying to use Selenium WebDriver for testing our application. The issue is in unstable pop-ups focusing in IE9. It is not always reproducible, it takes pl
With the IE driver, the order in which the windows appear in the collection is not guaranteed. That is, the 0th window in the collection is not necessarily the first window opened by the session. Given that is the case, you'll need to do something like the following:
private string FindNewWindowHandle(IWebDriver driver, IList existingHandles, int timeout)
{
string foundHandle = string.Empty;
DateTime endTime = DateTime.Now.Add(TimeSpan.FromSeconds(timeout));
while (string.IsNullOrEmpty(foundHandle) && DateTime.Now < endTime)
{
IList currentHandles = driver.WindowHandles;
if (currentHandles.Count != existingHandles.Count)
{
foreach (string currentHandle in currentHandles)
{
if (!existingHandles.Contains(currentHandle))
{
foundHandle = currentHandle;
break;
}
}
}
if (string.IsNullOrEmpty(foundHandle))
{
System.Threading.Thread.Sleep(250);
}
}
// Note: could optionally check for handle found here and throw
// an exception if no window was found.
return foundHandle;
}
The usage of the above function would be something like the following:
IList handles = driver.WindowHandles;
// do whatever you have to do to invoke the popup
element.Click();
string popupHandle = FindNewWindowHandle(driver, handles, 10);
if (!string.IsNullOrEmpty(popupHandle))
{
driver.SwitchTo().Window(popupHandle);
}