简单 - 如何使用编码的UI从WPF组合框中选择项目

时间:2014-05-30 10:20:56

标签: wpf wpf-controls coded-ui-tests

我使用过编码用户界面进行Web应用程序,但我第一次尝试利用Coded UI进行WPF。我想对组合框中的项目执行单击但是,我无法在WPF中实现相同的功能。我试图遍历组合框中的项目,但它没有工作。尝试使用搜索属性 - 没有结果。此外,试图弄清楚AutomationElement的东西,但无法获得解决方案。如果我能够了解为达到要求需要遵循的方法,那将是很好的。

我已经捕获了控件并想要玩它们。没有记录和播放。

2 个答案:

答案 0 :(得分:4)

您可以使用WpfComboBox的SelectedItem属性,该属性获取您要选择的项目的名称(如我在yonder答案中的评论中所述)

var myComboBox = this.UIMap.UIMainWindowWindow.UIItemComboBox;
var items = myComboBox.Items;
myComboBox.SelectedItem = items[0].Name;

或者,如果您已经知道要设置的项目的索引,则可以设置SelectedIndex

var myComboBox = this.UIMap.UIMainWindowWindow.UIItemComboBox;
var items = myComboBox.Items;
myComboBox.SelectedIndex = 0;

或者您可以先单击组合框以展开它,然后获取项目元素的UITestControl并对其执行单击(遗憾的是,您必须手动单击组合框,因为看起来它没有“ExpandWhileSearching”配置。努力工作)

var myComboBox = this.UIMap.UIMainWindowWindow.UIItemComboBox;
var items = myComboBox.Items;
Mouse.Click(myComboBox);
Mouse.Click(items[0]);

var myComboBox = this.UIMap.UIMainWindowWindow.UIItemComboBox;
var items = myComboBox.Items;
myComboBox.Expanded = true;
Mouse.Click(items[0]);

答案 1 :(得分:0)

您将使用与Html相同的方式创建组合框对象,只需使用Microsoft.VisualStudio.TestTools.UITesting.WpfControls命名空间。例如:

public WpfComboBox tester
{
    get
    {
        WpfComboBox target = new WpfComboBox();
        return target;
    }
}

然后,您将创建一个UITestControlCollection对象来存储组合框的.Items。

UITestControlCollection comboBoxItems = tester.Items;

从这里开始,您应该能够随意编辑和设置所选项目(tester.SelectedItem = comboBoxItems[0].ToString();)。