简体   繁体   中英

SwitchTo method for Coded UI

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?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM