Coded UI - UITestControlCollection using FindMatchingControls() is empty on consecutive runs

半腔热情 提交于 2019-12-13 04:48:32

问题


I use SpecFlow with Coded UI to create some automated functional tests for a WPF application.

Let's see the following SpecFlow scenario:

Scenario: Issue
When Results button is pressed
    When Result 123 is selected
When Results button is pressed
    When Result 123 is selected

1st and 3rd row parameter: Results
2nd and 4th row parameter: 123

Here are the methods for the upper mentioned steps:

When Results button is pressed

public void PressButton(string buttonName)
{
    WpfButton uIButton = this.UIAUT.UIButton;
    uIButton.SearchProperties[WpfButton.PropertyNames.Name] = buttonName;

    uIButton.WaitForControlEnabled();
    Mouse.Click(uIButton);
}

When Result 123 is selected

public void SelectResultByID(string resultId)
{
    WpfListItem uIResult = this.UIAUT.UITier1List.UITier2ListBox.UITier3ListItem;
    var allResults = uIResult.FindMatchingControls();

    foreach (WpfListItem item in allResults)
    {
        string[] elem = item.GetChildren().GetNamesOfControls();
        if (elem[0] == resultID)
        {
            Mouse.Click(item);
        }
    }
}

The first three rows are OK. When the 4th step When Result 123 is selected is executed again var allResults = uIResult.FindMatchingControls(); is empty so the foreach part is skipped, no action is taken and the test is passed.

Could someone tell me what is wrong? It is obvious that I miss something.


回答1:


Assuming you code is generated by recording (or is similar) then I would suspect the "cacheing" nature of the higher level UI Controls. The application may draw one UI Control containing the first 123 and draw another UI Control for the second 123. These controls look identical but are different and have different window-ids (or handles or whatever). One of the UI Controls in

WpfListItem uIResult = this.UIAUT.UITier1List.UITier2ListBox.UITier3ListItem;

probably refers to the UI Control of the first 123 even though it is no longer on display. I suspect UITier3ListItem or its child controls; if it were ...1... or ...2... then I would expect a failure message rather than zero matches.



来源:https://stackoverflow.com/questions/20418813/coded-ui-uitestcontrolcollection-using-findmatchingcontrols-is-empty-on-cons

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