CodedUI搜索的孙子控制

本文关键字:控制 搜索 CodedUI | 更新日期: 2023-09-27 18:16:46

我有一个有多行的表。我想验证一个特定的字符串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'控制类型:"Text"名称:…

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

CodedUI搜索的孙子控制

  1. 你应该把你的代码移出部分UIMap.designer.cs类,因为下次你记录控件/assert/等时它会被覆盖。将代码移到uimap.cs部分类中。

  2. 文本控件可能是也可能不是下一个级别,如果它不是在UIItemTable的子级别,你的代码将失败。您应该尝试使用测试工具构建器交叉毛来确定它存在的级别。

  3. 你可以使用子枚举器并搜索所有的子/孙子/等元素,直到你找到你的文本项,下面的例子类似于:

    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。另一种选择是使用FindMatchingControls()函数并进行类似于上述语句的解析。不过,你可能仍然需要合适的直系父母

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

希望能有所帮助

我通常先找到单元格。我相信表中的任何内容都应该在单元格中。因此,我们可以先搜索单元格,然后再深入其中。

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

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;