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

后端 未结 2 929
北恋
北恋 2020-12-07 06:18

Problem: I am using FindMatchingControls() to create a Collection of rows in a WPF table. I have written a loop that will set the ComboBox in each row to a

相关标签:
2条回答
  • 2020-12-07 06:28

    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.

    0 讨论(0)
  • 2020-12-07 06:33

    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.

    0 讨论(0)
提交回复
热议问题