Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium WebDriver windows switching issue in Internet Explorer 8-10

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 place in about 20% of windows switching but makes testing on IE almost impossible. In FireFox everything works perfect.

  1. I try to increase timeout:

TimeSpan interval = new TimeSpan(0, 0, 10); driver.Manage().Timeouts().ImplicitlyWait(interval);

  1. Create own methods for objects finding:

               for (int x = 0; x <= waitTimeOut; x++)
                {
                    try
                    {
                        element = (IWebElement)driver.FindElement(By.XPath(obj.Xpath));
                        return element;
                    }
    
                    catch{}
                }
    
  2. Try to use CssSelecotrs

  3. Try to make some reswitching before finding element:

    driver.SwitchTo().Window(GetWindowHandle(2, 1)); driver.SwitchTo().Window(GetWindowHandle(0, 1)); driver.SwitchTo().Window(GetWindowHandle(2, 1));

If the issue occurs, it always occurs only with the first element I try to find on the page. If the element is found there is no any problems with finding other elements on this page. So I decided the problem is in focusing.

Windows handles in debugger displays correctly. For example if I switches to the third window, driver.CurrentWindowHandle gives me correct handle of third window . But if I try to find any element, FindElement() throws me an exception. The page is loaded, I can click the element manually but FindElement() can't find it. If I rerun the test, this step can be passed without any problems and failed only at the next switching or further. It's unpredictable.

What can be the reason of such problem?

like image 728
Dmitry Komar Avatar asked Dec 16 '22 10:12

Dmitry Komar


1 Answers

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<string> existingHandles, int timeout)
{
    string foundHandle = string.Empty;
    DateTime endTime = DateTime.Now.Add(TimeSpan.FromSeconds(timeout));
    while (string.IsNullOrEmpty(foundHandle) && DateTime.Now < endTime)
    {
        IList<string> 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<string> 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);
}
like image 110
JimEvans Avatar answered Dec 22 '22 01:12

JimEvans