开发者

WebDriver in C# (work with windows and synchronization)

开发者 https://www.devze.com 2023-04-09 22:04 出处:网络
I work with webDriver in #IE9 and I find one problem. If I started tests in Run mode, then all test fail because webDriver not exists (two window ie), but if I put breakpoint in tests and start tests

I work with webDriver in #IE9 and I find one problem. If I started tests in Run mode, then all test fail because webDriver not exists (two window ie), but if I put breakpoint in tests and start tests Debug mode I have passed all tests. Please tell me, what do, because I don't know. This my code:

private void MyMethods(IWebdriver driver)
{

    foreach (var item in driver.WindowHandles) // if I put breakpoint, I see 2 count Window Handles else this methods don't work.
    {
        if (driver.SwitchTo().Window(开发者_高级运维item).Title == "PortalSubMenuPopupForm")
        {
            driver.SwitchTo().Window(item);
            break;
        }
    }
}


Selenium has an "issue" with IE where new windows might not appear on the WindowHandles list right away.

The solution is either

  • wait a fixed amount of time before calling driver.WindowHandles

or

  • use the WebDriverWait class to wait for the number of elements under WindowHandles to change

I think the second one is more robust. Here is a quick implementation:

public void LaunchNewWindow(IWebElement element)
{
    int windowsBefore = driver.WindowHandles.Count;
    element.Click();

    TimeSpan timeout = new TimeSpan(0, 0, 10);
    WebDriverWait wait = new WebDriverWait(driver, timeout);

    wait.Until((_driver) =>
    {
        return _driver.WindowHandles.Count != windowsBefore;
        //optionally use _driver.WindowHandles.Count > windowsBefore
    });
}

Now you can use the function like so:

IWebElement clickMe = //some element that launches a new window

LaunchNewWindow(clickMe);
foreach (var item in driver.WindowHandles)
{
   //etc.
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号