【问题标题】:Trying to find a specific WpfControl for use in Coded UI tests试图找到用于编码 UI 测试的特定 WpfControl
【发布时间】:2013-11-29 01:54:41
【问题描述】:

我正在尝试创建一些编码 UI 测试,以便在一组 WPF 控件上多次使用。我可以使用 Coded UI 测试构建器单独创建每个测试,但我希望创建“一般”案例,因为每个测试都非常相似,只是稍作修改。

为了创建这种一般情况,我需要能够找到(然后单击)给定父级的控件(在本例中为WpfWindow;我正在寻找的控件有几个层次,但是无论深度如何,我都希望能够找到控件)。我已经能够使用AutomationElement 找到我要查找的控件,但是编码的 UI 似乎不喜欢它,并且一旦找到我就无法单击它。

我尝试在父窗口上使用GetChildren(),然后递归搜索以找到有问题的控件。我正在使用 SearchProperties 来查找我正在寻找的特定 WPF 控件的 NameControlType,但看起来并非所有内容都有 Name 并且它失败了。

我也尝试为特定控件设置SearchProperties,但它会抛出一个异常,说找不到它。

有什么建议吗?我是否以正确的方式解决这个问题,还是我错过了一些基本的东西?

除此之外:使用 Coded UI 测试构建器构建类似的 Coded UI 测试有多“糟糕”?我的意思是,使用 Coded UI Test Builder 创建 800 个不同的测试似乎是错误的,这些测试都具有相同的前 5 个步骤(根据运行位置略有不同)。

【问题讨论】:

    标签: c# wpf coded-ui-tests


    【解决方案1】:

    我重新访问了AutomationElement 解决方案,并能够找出点击部分。

    public AutomationElement findControl(AutomationProperty propertyOfItem, string itemToSearchFor, System.Windows.Automation.ControlType controlType)
    {
                Condition condition = new System.Windows.Automation.AndCondition(
                       new System.Windows.Automation.PropertyCondition(propertyOfItem, itemToSearchFor),
                       new System.Windows.Automation.PropertyCondition(AutomationElement.ControlTypeProperty, controlType));
    
                AutomationElement foundControl = AutomationElement.RootElement.FindFirst(TreeScope.Descendants | TreeScope.Children, condition);
                return foundControl;
    }
    

    点击部分从AutomationElement 获取可点击点并将其存储在System.Windows.Point 中,并在此基础上创建System.Drawing.Point。然后在这一点上调用Mouse.Click()

    【讨论】:

      【解决方案2】:

      我们的处理方式是记录控件而不是步骤。然后,您可以传递使用完整 parent-parent-parent 映射的控件。 . . -parent 映射到方法中。

      ControlB = genericControl(parentWindow,     
          "ControlType","Button","ControlName","Send");
      TestOne(ControlB);
      TestOne(ControlC);
      
      public void TestOne(UITestControl control, string Value)
      { 
          //do stuff
          Mouse.Click(control);
          Assert.AreEqual(Value, control.GetProperty("InnerText"));
      }
      

      通用控制:

      public UITestControl genericControl(UITestControl parentWindow, String Property1, String Value1, String Property2, String Value2)
      {
          UITestControl control = new UITestControl(parentWindow);
          control.TechnologyName = "MSAA";
          control.SearchProperties.Add(Property1, Value1, Property2, Value2);
      
          return control;
      }
      

      等等

      如果每个步骤都是通用的,它应该可以工作。

      ETA - 您必须具有要使用的控件的某些特征。除非您想使用 FindAllMatchingControls 并循环遍历所有控件,或者使用不同的类型来过滤列表,否则您必须为每个控件提供唯一的标识信息。

      【讨论】:

      • 我不太确定这是否有帮助。是的,它是通用的,但我的问题实际上是找到我想要的特定控件。这里的问题是找到控制权传递给public void TestOne(UITestControl control, string Value)
      • 我明白你的意思。我想我的下一个问题是当一个或多个父母特定于当前运行时,如何使用完整的 parent-parent-parent...-parent 映射来控制。例如,我的控件的末尾如下所示:...UIBrowseTreeCustom.UITreeViewTree.UICNameTreeItem.UIItemTreeItem; 其中CName 指的是构建编码 UI 测试的计算机的名称。我会尝试一些东西然后会回来。
      • 现在我们处于问题的核心!!在对象的属性窗口中,您可以将搜索属性从“等于”更改为“包含”,这样您就可以将 name = WindowParent123d 更改为 name.Contains(WindowParent),它会找到匹配项。您还可以更改/删除控件的窗口标题。
      猜你喜欢
      • 2015-07-08
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多