C# CodedUI搜索孙子控制?

C# CodedUI搜索孙子控制?,c#,coded-ui-tests,C#,Coded Ui Tests,我有一张有多行的桌子。我想验证表中是否有特定的字符串StringName。我使用CodedUI查找控件,UI映射如下: public class UIItemTable : WpfTable { #region Fields private UIItemRow mUIItemRow; #endregion } public class UIItemRow : WpfRow { #region Fields private UIBlahBlahBlahCe

我有一张有多行的桌子。我想验证表中是否有特定的字符串
StringName
。我使用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
}
我想找到一个与我的
StringName
匹配的
WpfText
。因此,我在
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
}
但是CodedUI找不到
WpfText
控件。我收到的错误是:

Microsoft.VisualStudio.TestTools.UITest.Extension.UITestControlNotFoundException。 播放找不到具有给定搜索的控件 财产。其他详细信息:技术名称:“UIA”控制类型: “文本”名称:

我认为这个错误可能是因为我要搜索的WpfCell实际上是表的孙子。但我认为CodedUI处理树遍历对吗?我如何寻找孙子

  • 您应该将代码移出部分UIMap.designer.cs类,因为它将在下次录制控件/断言等时被覆盖。请将代码移到UIMap.cs部分类

  • 文本控件可能是下一级,也可能不是下一级,如果它不在UIItemTable的子级,代码将失败。您应该尝试使用测试工具生成器十字线来标识它所在的级别

  • 您可以使用子枚举器搜索所有子/孙/等元素,直到找到您的文本项,下面的示例如下:

    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;
    }
    
    公共UIControl查找文本(UIControl ui)
    { 
    UIControl返回控件=空;
    IEnumerator UIItemTableChildEnumerator=
    ui.GetChildren().GetEnumerator();
    while(uiChildEnumerator.MoveNext())
    {
    if(uiChildEnumerator.Current.DisplayText.equals(StringName))
    {
    returnControl=uiChildEnumerator.Current
    打破
    }
    其他的
    {
    returnControl=this.FindText(uiChildEnumerator.Current)
    if(returnControl!=null)
    {
    打破
    }
    }
    }
    返回控制;
    }
    
  • 四,。另一个选项是使用FindMatchingControls()函数并通过类似于上述语句的方式进行解析。不过,你可能仍然需要合适的直系父母

        WpfText StringNameWpfText = new WpfText(this);
    
        IEnumerator<UITestControl> textItems = StringNameWpfText .FindMatchingControls().GetEnumerator();
    
            while (textItems.MoveNext())
            {
               //search
            }
    
    WpfText StringNameWpfText=新的WpfText(此);
    IEnumerator textItems=StringNameWpfText.FindMatchingControls().GetEnumerator();
    while(textItems.MoveNext())
    {
    //搜寻
    }
    

    希望这有帮助,我通常先找到细胞。我相信表格中的任何内容都会在单元格中。所以我们可以先搜索细胞,然后潜入其中

    查找其值与给定字符串匹配的单元格的一种方法

    WpfCell cell = myWpfTable.FindFirstCellWithValue(string StringName);
    if(cell != null)
        return cell.Value // returns WpfControl;
    else 
        return null;
    
    获取包含特定字符串的单元格值的另一种方法

    WpfCell myCell = myWpfTable.Cells
                               .FirstOrDefault(c => c.Value.Contains("StringName"));
    var myTextControl = myCell != null ? myCell.Value : null;
    
    如果文本嵌套在单元格的深处,则可以执行此操作。就像你在桌子上做的一样

    // 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;
    
    第(1)项不清楚。
    uimap.cs
    uimap.designer.cs
    文件的顶部附近都有
    partial class
    。这一点最好是这样说:将代码移出
    uimap.designer.cs
    文件,因为它将在下次录制控件/assert/etc时被覆盖。将代码移到
    uimap.cs
    文件中,因为这两个文件都属于同一类。其他几点看起来不错。