CodedUI “FindMatchingControls()” works 10% of the time but usually returns about half of the controls

我怕爱的太早我们不能终老 提交于 2019-11-28 02:25:53

Instead of trying to find matching controls based on another row, you could use a method that takes the parent (in your case the table) and returns all it's children in a recursive way. It digs all the way down until all available children have been found. It shouldn't matter how much row's your table has, it will try and get all of them. It's usable for any UITestControl.

public ParentControl GetChildControls(UITestControl parentControl)
{
    ParentControl parent = new ParentControl();

    if (parentControl != null)
    {
        List<ParentControl> children = new List<ParentControl>();

        foreach (UITestControl childControl in parentControl.GetChildren())
        {
            children.Add(GetChildControls(childControl));
        }

        parent.Children = new KeyValuePair<UITestControl, List<ParentControl>>(parentControl, children);
    }

    return parent;
}

The parent class

public class ParentControl
{
    public KeyValuePair<UITestControl, List<ParentControl>> Children { get; set; }
    public string Value
    {
        get
        {
            return Children.Key.Name;
        }
    }
}

I just added the Value property for easy access to the name of UITestControl.

PixelPlex (above) has provided the best answer. All I had to add to PixelPlex's code was an If statement to set the ComboBox to a value when it was found. The foreach is therefore as below in my case ...

            foreach (UITestControl childControl in parentControl.GetChildren())
            {
                children.Add(GetChildControls(childControl));

                //Added below If statement to set ComboBox selected item to "Carrots"...
                if (childControl.ClassName == "Uia.ComboBox")
                {
                    WpfComboBox cb = (WpfComboBox)childControl; 
                    cb.SelectedItem = "Carrots";
                }
            }

This selects Carrots from my ComboBox... Everything that does not satisfy my If statement is not relevant so I don't do anything with it.

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