【问题标题】:Event will not fire upon listbox item selection - .Net UIAutomation framework选择列表框项目时不会触发事件 - .Net UIAutomation 框架
【发布时间】:2011-09-03 08:45:27
【问题描述】:

我今天正在学习 .NET UI 自动化框架。所以到目前为止我做了什么(参考各种文章)

  1. 有一个 WinForm,上面有 Listbox、PictureBox、TextBox 和 Button 控件。请参考图片:

  2. 我有一个控制台应用程序,其中包含所有 UI 自动化测试脚本或代码,可自动执行 winform UI 测试。

工作: 从列表框中选择项目后,图片框会加载一些图像并显示它(要加载的代码在列表框的 SelectedIndexChanged 事件中)。

下面是Forms listBox控件的代码:

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() 方法确实有效,Form 列表框项被选中,如图所示 :

如图所示,列表框项目已被选中,但 SelectedIndexChange 事件未触发,并且图片框未反映更改。

所以任何指针都会有很大帮助:)

谢谢

【问题讨论】:

  • 我不知道是什么问题,但是您可以通过在声明 AutomationElement itemToSelectInListBox = listBoxItems[new Random().Next(0, listBoxItems.Count - 1)]; 之后添加 listbox1_selectedIndexChanged(null,null); 来临时解决问题
  • 正如我所说,这个自动化 UI API 位于控制台应用程序(另一个项目和 exe)中,而表单位于另一个。正如你所说,我能做的唯一方法是使用 relfection api。但如果我这样做,那将是完全不同的实例,根本没有任何联系。

标签: c# winforms user-interface ui-automation microsoft-ui-automation


【解决方案1】:

@zenwalker 列表是否由数据绑定填充?如果是,则有可能选择事件不会触发。你能分享将数据绑定到列表框的代码吗?抱歉,我没有足够的代表来添加 cmets。

或者,您可以参考以下 SO 文章,了解我们如何对列表框进行数据绑定Winforms, databinding, Listbox and textbox

【讨论】:

  • 不,我在设计时手动添加了列表框的内容。所以我在这里没有使用任何数据绑定上下文。谢谢:)
【解决方案2】:

如果将 SelectionMode 更改为 MultiSimple,这将起作用。我不确定为什么会这样。 但如果 SelectionMode 为 One,则不会触发 selectedindex 事件。

【讨论】:

    【解决方案3】:

    可能有点晚了,但我仍然希望这对某人有所帮助:

    我遇到了完全相同的问题。能够通过鼠标单击触发事件。对于以下代码,您需要引用 Microsoft.TestAPI (http://www.nuget.org/packages/Microsoft.TestApi/0.6.0),但还有其他方法可以模拟点击。

        static AutomationElement SelectItem(AutomationElement item)
        {
            if (item != null)
            {
                ((SelectionItemPattern)item.GetCurrentPattern(SelectionItemPattern.Pattern)).Select();
                System.Windows.Point point = item.GetClickablePoint();
                Microsoft.Test.Input.Mouse.MoveTo(new System.Drawing.Point((int)point.X, (int)point.Y));
                Microsoft.Test.Input.Mouse.Click(MouseButton.Left);
            }
    
            return item;
        }
    

    【讨论】:

      【解决方案4】:

      SelectionMode 设置为单个或一个时,不会触发事件SelectedIndexChanged

      确保在触发 SelectionChanged 事件时也更新 PictureBox

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多