今天我正在学习.NET UI自动化框架。所以到目前为止我所做的(指各种文章)事件不会触发listbox项目选择 - .Net UIAutomation框架
有一个WinForm的Listbox,PictureBox,TextBox和Button控件。请参阅图片:
我有一个控制台应用程序,它具有所有UI自动化测试脚本或代码,可以自动进行winform UI测试。
工作: 一旦从列表框中选择项目,图片框加载一些图像,并显示它(加载代码是在列表框的SelectedIndexChanged事件)。
下面是窗体列表框控件的代码:
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
textBox1.BackColor = Color.White;
pictureBox1.Image = imageCollection.ElementAtOrDefault(listBox1.SelectedIndex);
textBox1.Text = pictureBox1.Image.GetHashCode().ToString();
this.Refresh();
}
现在我UIAutomation测试脚本代码如下所示:(只显示必要的部分)
AutomationElement listBoxElement = mainFormWindowElement.FindFirst(TreeScope.Children,
new PropertyCondition(AutomationElement.AutomationIdProperty, "listBox1"));
Assert.IsNotNull(listBoxElement, "Cant find the listbox element");
AutomationElementCollection listBoxItems =
listBoxElement.FindAll(TreeScope.Children,new PropertyCondition(AutomationElement.ControlTypeProperty,ControlType.ListItem));
AutomationElement itemToSelectInListBox = listBoxItems[new Random().Next(0, listBoxItems.Count - 1)];
Object selectPattern = null;
if (itemToSelectInListBox.TryGetCurrentPattern(SelectionItemPattern.Pattern, out selectPattern))
{
(selectPattern as SelectionItemPattern).AddToSelection();
(selectPattern as SelectionItemPattern).Select();
}
执行代码后, Select()方法确实起作用,并且如下所示选择表单列表框项目 :
正如您在图像,列表框项目被选中,但事件SelectedIndexChange没有被激发,并且图框不反映更改。
所以任何指针是有很大帮助:)
感谢
我不知道是什么问题,但你可以通过添加'listbox1_selectedIndexChanged一个临时的解决方法(NULL,NULL);'会后声明'AutomationElement itemToSelectInListBox = listBoxItems [新的随机()和Next(0, listBoxItems.Count - 1)]; ' – Marshal
正如我所说,这个自动化用户界面API是在一个consoleapp(另一个项目和exe文件)和窗体是在另一个。我所能做的唯一方法就是使用relfection API。但是,如果我这样做,它将是完全不同的实例,根本没有连接。 – Zenwalker