通过在代码中添加搜索属性来动态搜索控件

Searching controls dynamically by adding search properties in code

我试图通过在需要时在我的代码中添加一些搜索属性来在运行时识别控件。在 Visual Studio 2012 中使用编码 UI。

请参考下面的截图。我的测试场景是:

1) 单击特定选项卡(屏幕截图中选择的第 2nd 选项卡)

选项卡列表已修复,因此我可以在我的 UI地图中为每个选项卡创建一个控件。

2) 在每个Tab里面,都有一个包含一些数据的表格结构。 Table 标题是固定的,但行数和行内的数据是动态的。

3) 勾选所需标签的复选框

4) Select 键入所需标签

我已将我的 UI地图创建为:

添加了以下代码:

UIEquipmentTagText.SearchProperties.Add(new PropertyExpression(WpfText.PropertyNames.Name, "1302"));// say I want second row
To fetch the respective checkbox control, I am using:
UITestControl UIEquipmentTagCell = UIEquipmentTagText.GetParent();//get the cell of Tag name
UITestControl UIEquipmentTagRow = UIEquipmentTagCell.GetParent();//get the row
UITestControl UIEquipmentCheckBoxCell = UIEquipmentTagRow.GetChildren()[0];//get the first cell i.e the checkbox cell
UITestControl UIEquipmentCheckBox = UIEquipmentCheckBoxCell.GetChildren()[0]; // get the checkbox control

但这对我不起作用。我猜 UIRow 控件仅引用第一行(虽然我没有指定查找 1st 行)

我不想在我的行搜索条件中包含行号。

是否有任何解决方法可以仅基于标记文本获得我想要的所有控件?

终于找到了解决方案..获取所有行并迭代以找到匹配的行

UITestControlCollection allRows = row.FindMatchingControls(); 


            foreach (UITestControl x in allRows)
            {
                UITestControl Tag = x.GetChildren()[1].GetChildren()[0];//row->tag cell->tag text

                if (Tag.GetProperty("Name").Equals("1302"))//say I want to select row having 1302 tag 
                {
                    UITestControl checkBox = Tag.GetParent().GetParent().GetChildren()[0].GetChildren()[0];//TagText->TagCell->Row->CheckBoxCell->Checkbox

                    Mouse.Click(checkBox);
                }

            }