Opera - driver.WindowHandles returns wrong count

淺唱寂寞╮ 提交于 2019-12-12 03:34:57

问题


In my scenario, I'm verifying whether clicking on a link navigates to another page (verifying for the page title). IE, FF and chrome return 2 as expected but Opera returns 4. I didn't have any other Opera instances opened at the time of running tests. It clicks on the link and required page is opened but WindowHandles returns 4.

Code:

string BaseWindow = Drivers._driverInstance.CurrentWindowHandle;
Drivers._driverInstance.SwitchTo().Frame(Drivers._driverInstance.FindElement(By.ClassName("iframe-fix")));
if (Drivers._driverInstance.GetType().Name.ToString() == "InternetExplorerDriver")
{
    IJavaScriptExecutor js = (IJavaScriptExecutor)Drivers._driverInstance;
    js.ExecuteScript("arguments[0].click();", Drivers._driverInstance.FindElement(By.LinkText("Professional Services.")));
}
else
{
    Drivers._driverInstance.FindElement(By.LinkText("Professional Services.")).Click();
}
    System.Collections.ObjectModel.ReadOnlyCollection<string> handles = Drivers._driverInstance.WindowHandles;
    if (handles.Count == 2)
    {
      foreach (string handle in handles)
      {
         if (handle != BaseWindow)
         {
            string title = Drivers._driverInstance.SwitchTo().Window(handle).Title;
            Assert.AreEqual("title of the page", Drivers._driverInstance.Title);
          }
      }
     }
    else
    {
        Assert.Fail("WindowHandles returns " + handles.Count + " instead of 2");
    }
Drivers._driverInstance.SwitchTo().Window(BaseWindow);

Can someone suggest why Opera returns 4 instead of 2.

Thanks.


回答1:


The Opera driver doesn't return the right number of handles. This issue has already been reported to the project but it seems that the project is no longer maintained:

https://github.com/operasoftware/operachromiumdriver/issues/15




回答2:


I encountered the same thing as you with Opera driver, plus (if I remember it right), the CurrentWindowHandle property doesn't work either.

Workaround:

public static void SwitchToPopup(TestTarget target, bool toPopup)
{
    if (target.IsOpera)
    {
        if (toPopup)
        {
            _windowIndex += 3;
            new WebDriverWait(target.Driver, TimeSpan.FromSeconds(DefaultTimeoutInSeconds)).Until(d => d.WindowHandles.Count > _windowIndex);
        }
        else
        {
            _windowIndex -= 3;
        }
        target.Driver.SwitchTo().Window(target.Driver.WindowHandles[_windowIndex]);
    }
    else
    {
        IEnumerable<string> windowHandles = toPopup ? target.Driver.WindowHandles : target.Driver.WindowHandles.Reverse();

        bool bFound = false;
        foreach (string windowHandle in windowHandles)
        {
            if (bFound)
            {
                target.Driver.SwitchTo().Window(windowHandle);
                break;
            }
            bFound = windowHandle == target.Driver.CurrentWindowHandle;
        }
    }
}


来源:https://stackoverflow.com/questions/36353057/opera-driver-windowhandles-returns-wrong-count

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!