SwitchTo method for Coded UI

扶醉桌前 提交于 2019-12-25 12:53:12

问题


I am working on a, hand coded, Coded UI test for an application.

The application has a certain function that operates in 2 windows. When a button is clicked, a new window appears and then the user makes a selection upon which that window closes, and then the user continues taking actions on the main window.

With Selenium, I would handle that by iterating through all the window handles and switching by using passing it the URL of the page I wanted using the "driver.SwitchTo().Window(handle)" method. However, with Coded UI, I have not found a similar solution. Using the Process class I can do something similar that could work:

Process[] myList = Process.GetProcessesByName("iexplore");
foreach (Process item in myList)
if (item.MainWindowTitle.Contains("Window Title"))
{
item.Kill();
}

The problem is that the application that I am testing is poorly designed and all windows throughout the application have the same name, thus it will not work.

Is there a method which can be used to switch to a different window on Coded UI? Or what would be the best approach?


回答1:


Take a look at this question, it might help: Interacting with multiple instances of an application in Coded UI

You don't do "switching" in CUIT, every window and control is accessed via a UITestControl object. If you want to do stuff on an other window you create a new object for it. If you can't differentiate the two windows with search properties you can use the Instance property or FindMatchingControls method.

To catch a window creation event you can use a winhook. It gives you the window handle of every window created. Using that you can find out if the window created is the window you are waiting for and then use the UITestControlFactory.FromWindowHandle to create a UITestControl for CUIT to interact with. If you have a generated class for that window you can create an instance of that class and call its CopyFrom method to pass to it the control you created from the window handle.




回答2:


I am using:

public static HtmlDocument WaitForPopup(string popupName, string text)
{
    BrowserWindow browser = new BrowserWindow();
    browser.SearchProperties.Add(BrowserWindow.PropertyNames.Name, popupName,
    PropertyExpressionOperator.Contains);

    browser.WindowTitles.Add(popupName);
    browser.WaitForControlExist();
    browser.WaitForControlReady();
    browser.SetFocus();

    HtmlDocument html = new HtmlDocument(browser);
    html.SearchProperties.Add(HtmlDocument.PropertyNames.InnerText, text,
    PropertyExpressionOperator.Contains);
    html.WaitForControlExist();
    html.WaitForControlReady();
    Playback.Wait(1000);

    return html;
}

Just call WaitForPopup("your window name", "some constant text in window") and continue with tests there. Note that this method will return html of the new window and you can use it further on.



来源:https://stackoverflow.com/questions/23716927/switchto-method-for-coded-ui

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