本文介绍了CodedUI“ FindMatchingControls()”工作10%的时间,但通常会返回大约一半的控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题::我正在使用FindMatchingControls()在WPF表中创建行集合。我编写了一个循环,将每一行中的ComboBox设置为给定值。整个过程有时有效,但并非总是如此,FindMatchingControls()实际上找到大约一半的行。如何配置超时或更改设置以使其每次都能找到所有50个控件,或者找到前10个,然后找到接下来的10个,等等?

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 given value. The whole thing works sometimes but more often than not, FindMatchingControls() actually finds about half of the rows. How can I configure timeouts or change settings to make it find all 50 controls every time or perhaps to find the first 10 and then to find the next 10 etc?

背景:我正在测试WPF窗口,上面有一个表格,表格中的每一行都有一个下拉列表。一共有50行,将来还会有更多行,因此我无法记录每个行的设置,我记录的测试对于每个新版本都会过时(每个月左右)。

Background: I am testing a WPF window and on it, there's a table, each row in the table has a drop down list. There are 50 rows and in future there could be more so it is not feasible for me to record the setting of each one, my recorded test would be out of date with each new version (every month or so).

因此,我记录了1个ComboBox的设置,然后使用FindMatchingControls()创建了一个Collection。我遍历Collection,将该集合中的每个ComboBox设置为所需的选择。前23行显示在我当前的屏幕分辨率上。唯一的问题是FindMatchingControls()有时返回23,有时26,有时34,有时返回所有50行!我的问题是,如何修复下面的代码,以便它总是返回所有50行(将来可能还会返回更多行)。

I have therefore recorded the setting of 1 ComboBox and then I used FindMatchingControls() to create a Collection. I loop through the Collection setting each ComboBox in that collection to the desired selection. The first 23 rows are shown on my current screen resolution. The only problem is that FindMatchingControls() sometimes returns 23, sometimes 26 , sometimes 34 and sometimes it returns all 50 rows! My question is, how do I fix the code below so that it always return all 50 rows (and possibly more in future).

您可以从代码中看到找到父控件两次,因此伪代码在下面。

You can see from the code that I found the Parent control twice so pseudo code is below.

伪代码

1)查找父容器(表)
2)定义一行(是父表的子表)
3)使用FindMatchingControls获取行集合
4)遍历

1) Find Parent Container (table)2) Define a row (that is a child of the parent table)3) Use FindMatchingControls to get a Collection of Rows4) Loop through the Collection, finding the ComboBox in each row and setting it's selection to a value passed into the method.

CODE:

    public void PlaceAnOrderScreen_SelectItems_List(String item /*Value to set all 50 ComboBoxes to*/)
    {
        WpfControl rowOfOrderItems = new WpfControl(this.UIOptimalOrderSystemClientShWindow.UIItemCustom22.UIListViewAutoID37Table);
        rowOfOrderItems.SearchProperties[WpfControl.PropertyNames.ControlType] = "DataItem";
        rowOfOrderItems.SearchProperties[WpfControl.PropertyNames.ClassName] = "Uia.ListViewItem";
        rowOfOrderItems.WindowTitles.Add("Order Management System");

        rowOfOrderItems.Find();
        rowOfOrderItems.DrawHighlight();  //Visible diagnostic

        //should get a collection of 50 controls ...
        //... but this is dodgy, it sometimes finds 23, 26, 34 or ocassionaly all 50 controls.
        //There are 23 visible controls and the rest, you have to scroll down to see.
        UITestControlCollection itemRows = rowOfOrderItems.FindMatchingControls();

        int c = 0;
        int i = 1;
        string label = String.Empty;

        foreach (var auditSelectionBox in itemRows)
        {
            //After the top 15 drop down selections have been made, strat scrolling down.
            //This is because setting the Value for a list box that is off the screen
            //causes it to complain the control is blocked...
            if (c >= 15)
            {
                if (i >= 3) //The scroll wheel moves 3 rows at a time, so only scroll once for every 3 rows...
                {
                    Mouse.MoveScrollWheel(-1);
                    i = 0;
                }
            }
            i++;
            c++;

            WpfCell auditDDL1 = new WpfCell(auditSelectionBox);
            auditDDL1.SearchProperties[WpfCell.PropertyNames.ColumnHeader] = "Total";
            auditDDL1.WindowTitles.Add("OrderSystem 5");

            //Works but takes 5 - 16 seconds per drop down list
            auditDDL1.Value = item;
        }
    }


推荐答案

要尝试根据另一行查找匹配的控件,您可以使用一种方法,该方法采用父级(在您的情况下为表)并以递归方式返回其所有子级。它一直向下挖掘,直到找到所有可用的子代。表格的行数无关紧要,它将尝试获取所有行。

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;
}

父类

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

我刚刚添加了 Value 属性,可轻松访问UITestControl的名称。

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

这篇关于CodedUI“ FindMatchingControls()”工作10%的时间,但通常会返回大约一半的控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 12:27