【问题标题】:How to subscribe to popup window opening in System.Windows.Automation如何订阅 System.Windows.Automation 中打开的弹出窗口
【发布时间】:2016-01-30 00:05:49
【问题描述】:

我正在尝试使用 System.Windows.Automation 库进行一些 UI 测试,并且能够取得一些进展,但是我无法订阅在我的应用中创建弹出窗口的事件。我尝试在根(桌面对象)以及窗口上使用Automation.AddStructureChangedEventHandler,但这不起作用。我也尝试使用不同的范围,但也没有帮助。

AutomationElement desktop = AutomationElement.RootElement;
AutomationElement app = desktop
    .FindFirst(TreeScope.Children,
        new PropertyCondition(AutomationElement.NameProperty,
            "Name of the App", PropertyConditionFlags.IgnoreCase));

ActivateWindow(app);

AutomationElement appWindow = app
    .FindFirst(TreeScope.Children,
        new PropertyCondition(AutomationElement.ControlTypeProperty,
            ControlType.Window));

// Find a button that opens a popup window and click it
AutomationElement button = appWindow
    .FindFirst(TreeScope.Children, Condition.TrueCondition)
    .FindAll(TreeScope.Children, Condition.TrueCondition)[8];

MoveMouseToAndClick(button);

Automation.AddStructureChangedEventHandler(desktop, TreeScope.Descendants, setupWindowOpen);

setupWindowOpen 处理程序有时会触发,但看起来其他应用程序会发生这种情况,而不是我的(我在发件人元素对象上看到 Internet Explorer id)。 提前致谢。

【问题讨论】:

    标签: c# windows testing ui-automation


    【解决方案1】:

    弹出控件很棘手,因为它们可能直接位于桌面下方或您的应用程序下方,或者可能位于其他任何地方(最好猜测是桌面的直接子级)。我建议您使用 Microsoft SDK 附带的 WinSDK_AccEvent.exe 实用程序来测试弹出窗口的各种事件的行为,例如:桌面和自动化应用程序上的 WindowOpened、StructureChanged、ToolTipOpened。这应该为您提供要订阅的正确事件以及在什么范围内。 重要提示:如果您使用的是 UI 自动化框架的托管版本,则最好迁移到本机版本,因为托管版本对它可以捕获的内容有限制,并且存在一些无法捕获的性能错误被克服。此外,它显然不再受到官方支持。希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      您可以订阅WindowOpenedEvent(请参阅此page)。看起来很简单,但我无法让它发挥作用。

      如果您仍然对 UI 测试感兴趣并且熟悉 Selenium 驱动程序,您可以尝试我在 https://github.com/kfrajtak/WinAppDriver 上提供的实现。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-02-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多