gpt4 book ai didi

.net - 使用 UI 自动化,Winforms 按钮多次调用

转载 作者:行者123 更新时间:2023-12-01 06:39:02 24 4
gpt4 key购买 nike

我正在尝试使用 MS UI Automation Framework在我的应用程序中进行一些自动化测试。我现在的目标是监听 GUI 事件并记录它们,类似于 Windows SDK 中提供的示例 (TestScriptGeneratorSample)。

只需使用该演示应用程序,我就可以使用一个按钮启动一个简单的 Windows 窗体应用程序,并看到它将单击事件记录为“调用”UIA 事件。但是,每次单击按钮,演示应用程序都会记录 4 “调用”事件。这是为什么?使用 WPF 按钮时,我没有看到这个问题。

我的猜测是,自从 System.Windows.Forms.Button类支持MSAA而不是 UIA,而是桥接 MSAA 接口(interface)的 UIA 部分行为不端,或者至少以我不理解且找不到任何文档的方式表现。
也许它正在报告鼠标按下,向上,单击,然后实际按下按钮的调用事件?

谁能解释这种行为,和/或提供一种解决方法,以便按下一个按钮会导致一个调用事件?

编辑:这是在 WinXP SP3 下。我刚刚安装了Windows Automation API 3.0 update并且仍然看到相同的行为。

编辑 2:我找到了this example作者不经意地提到这种行为是 Win32 控件中的一个错误,但没有引用任何证据......

这是我的示例应用程序(上面有一个按钮的表单),以及其中包含的事件监听。添加对 UIAutomationClient 的引用和 UIAutomationTypes .单击按钮并看到调用发生了四次而不是一次。

using System;
using System.Drawing;
using System.Windows.Automation;
using System.Windows.Forms;

namespace WindowsFormsApplication6
{
public partial class Form1 : Form
{
private TextBox m_Output;

public Form1()
{
InitializeComponent();

// set up the GUI with a single button...
Button b = new Button {Location = new Point(5, 5), Name = "Test", Text = "Click Me"};
// ... and a textbox to write events to.
m_Output = new TextBox { Location = new Point(5, 30), Name = "Output", Multiline = true, Size = new Size(250, 200) };

this.Controls.Add(b);
this.Controls.Add(m_Output);

// get the button as an UIA element
AutomationElement button = AutomationElement.FromHandle(b.Handle);
// attach a handler to the button, listening for the Invoke event
Automation.AddAutomationEventHandler(
InvokePattern.InvokedEvent,
button,
TreeScope.Element,
OnInvoked);
}

// Handler for button invoke events
private void OnInvoked(object Sender, AutomationEventArgs E)
{
AppendText("Invoked: " + ((AutomationElement)Sender).Current.AutomationId);
}

// Just dumps text to the textbox
private void AppendText(string Text)
{
if (m_Output.InvokeRequired)
{
m_Output.BeginInvoke((Action<string>)AppendText, Text);
}
else
{
m_Output.AppendText(DateTime.Now.ToString("hh:mm:ss.fff") + ": " + Text + Environment.NewLine);
}
}
}
}

最佳答案

对于它的值(value),我通过将发出的多个事件过滤为一个来解决它。在测试过程中,我发现了以下内容:

  • .NET 按钮(即 Winforms)在单击时按顺序生成以下事件:
  • 调用
  • 调用
  • 调用
  • 属性已更改(Name 属性)
  • 调用
  • Win32 按钮在某些情况下会生成以下事件(calc.exe 中的按钮):
  • 属性已更改(HasKeyboardFocus 属性)
  • 调用
  • Win32 按钮在其他情况下会生成以下事件(保存文件对话框中的“取消”):
  • 调用
  • 属性已更改(HasKeyboardFocus 属性)
  • 调用

  • 使用 FrameworkId AutomationElement 上的属性(property)与事件相关的,我可以区分第一种和第二种情况(它是 "Winform" 用于 .NET 按钮, "Win32" 用于 Win32 按钮)。然后对于两个 Win32 场景,我只需确保我得到一个 HasKeyboardFocus记录调用事件之前的属性更改事件。

    我还没有看到这不起作用,因为我似乎总是得到 HasKeyboardFocus属性更改事件,即使按钮已经聚焦(即我按了两次)。

    如果有人有的话,我仍然希望看到更多的见解...

    关于.net - 使用 UI 自动化,Winforms 按钮多次调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12591649/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com