简体   繁体   English

CodedUI搜索孙子控件?

[英]CodedUI search for grandchild control?

I have a table which has multiple rows. 我有一个有多行的表。 I want to verify that a specific string StringName is in the table. 我想验证表中是否有特定的字符串StringName I used CodedUI to find the control, and the UI map is like this: 我使用CodedUI查找控件,UI映射如下所示:

public class UIItemTable : WpfTable
{
    #region Fields
    private UIItemRow mUIItemRow;
    #endregion
}

public class UIItemRow : WpfRow
{
    #region Fields
    private UIBlahBlahBlahCell mUIBlahBlahBlahCell;
    #endregion
}

public class UIBlahBlahBlahCell : WpfCell
{
    #region Fields
    private WpfText mUIBlahBlahBlahText;
    #endregion
}

I want to find a WpfText that matches my StringName . 我想找到一个WpfText我匹配StringName So I added a function to UIItemTable : 所以我给UIItemTable添加了一个函数:

public class UIItemTable : WpfTable
{
    public WpfText Find(string StringName)
    {
        WpfText StringNameWpfText = new WpfText(this);
        StringNameWpfText.SearchProperties[WpfText.PropertyNames.Name] = StringName;
        return StringNameWpfText;
    }

    #region Fields
    private UIItemRow mUIItemRow;
    #endregion
}

But CodedUI cannot find the WpfText control. 但是CodedUI找不到WpfText控件。 The error I received is: 我收到的错误是:

Microsoft.VisualStudio.TestTools.UITest.Extension.UITestControlNotFoundException. Microsoft.VisualStudio.TestTools.UITest.Extension.UITestControlNotFoundException。 The playback failed to find the control with the given search properties. 播放未能找到具有给定搜索属性的控件。 Additional Details: TechnologyName: 'UIA' ControlType: 'Text' Name: ... 其他详细信息:TechnologyName:'UIA'ControlType:'Text'名称:...

I think this error may be due to the fact that the WpfCell I want to search is actually a grandchild of the table. 我认为此错误可能是由于我要搜索的WpfCell实际上是表的孙代而造成的。 But I thought CodedUI handles tree traversals right? 但是我认为CodedUI处理树遍历对吗? How do I search for a grandchild? 我如何寻找孙子?

  1. You should move your code out of the partial UIMap.designer.cs class as it will be overwritten the next time you record a control/assert/etc. 您应将代码移出部分UIMap.designer.cs类,因为下次记录控件/声明/等时它将被覆盖。 Move you code to the uimap.cs partial class instead. 而是将您的代码移至uimap.cs子类。

  2. The text control may or may not be the next level down, if it is not on the child level of UIItemTable, your code will fail. 文本控件可能会(也可能不是)下一级别下降,如果它不在UIItemTable的子级别上,则代码将失败。 You should try to use the test tool builder cross hair to identify the level it exists at. 您应该尝试使用测试工具构建器的十字准线来确定其存在的级别。

  3. You can use the child enumerator and search through all child/grandchild/etc elements until you find your text item, an example below would be something like: 您可以使用子枚举器并搜索所有子项/孙子项/等元素,直到找到您的文本项,以下示例如下所示:

     public UIControl FindText(UIControl ui) { UIControl returnControl = null; IEnumerator<UITestControl> UIItemTableChildEnumerator = ui.GetChildren().GetEnumerator(); while(uiChildEnumerator.MoveNext()) { if(uiChildEnumerator.Current.DisplayText.equals(StringName)) { returnControl = uiChildEnumerator.Current break; } else { returnControl = this.FindText(uiChildEnumerator.Current) if(returnControl != null) { break; } } } return returnControl; } 

4 . 4。 Another option would be to use the FindMatchingControls() function and parse through similar to the above statement. 另一个选择是使用FindMatchingControls()函数并通过类似于上面的语句进行解析。 You might still need the proper direct parents though 您可能仍然需要适当的直系父母

    WpfText StringNameWpfText = new WpfText(this);

    IEnumerator<UITestControl> textItems = StringNameWpfText .FindMatchingControls().GetEnumerator();

        while (textItems.MoveNext())
        {
           //search
        }

Hope this helps 希望这可以帮助

I usually find cells first. 我通常首先找到细胞。 I believe any content in a table would be in a cell. 我相信表格中的任何内容都会在单元格中。 So we can search for the cells first and then dive in it. 因此,我们可以先搜索细胞,然后再潜入其中。

One way to find cell whose value matches with the given string. 查找值与给定字符串匹配的单元格的一种方法。

WpfCell cell = myWpfTable.FindFirstCellWithValue(string StringName);
if(cell != null)
    return cell.Value // returns WpfControl;
else 
    return null;

Another way to get the value of cell which contains a particular string 获取包含特定字符串的单元格值的另一种方法

WpfCell myCell = myWpfTable.Cells
                           .FirstOrDefault(c => c.Value.Contains("StringName"));
var myTextControl = myCell != null ? myCell.Value : null;

If the text is nested deep in the cell, then you can do this. 如果文本嵌套在单元格的深处,则可以执行此操作。 Just like what you were doing with the table 就像您在桌子上做什么一样

// Find cell which contains the particular string, let say it "myCell"
WpfText mytext = new WpfText(myCell);
mytext.SearchProperties.Add(WpfText.PropertyNames.Name, "StringName", PropertyExpressionOperator.Contains);
if(mytext.Exist)
    return myText;
else 
    retrun null;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM