【问题标题】:GUI-Test automation: Finding WinForms buttons via pinvoke with c#GUI 测试自动化:使用 c# 通过 pinvoke 查找 WinForms 按钮
【发布时间】:2010-08-06 12:51:41
【问题描述】:

我正在用 C# 为应用程序构建一个小型 GUI 测试自动化工具。 测试工具的功能之一是关闭被测试应用程序弹出的对话框。

我遇到的麻烦是在没有给出完整类名的情况下找到要单击的按钮。我使用 FindWindowEx 方法来获取对话框和我想要单击的按钮。我知道按钮的标题,但麻烦的是我还需要为按钮指定类名。类名并不总是相同,但看起来像这样:“WindowsForms10.BUTTON.app.0.3ce0bb8”。例如,如果您在本地或通过单击一次启动应用程序,最后“3ce0bb8”的部分是否不同。

所以,我的问题是:我怎样才能找到按钮,只需指定类的第一部分(始终相同),如“WindowsForms10.BUTTON.app”。或者我可以用其他方法解决这个问题吗?怎么办?

dll 导入如下所示:

[DllImport("user32.dll", SetLastError = true)]
    static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string pszWindow);

尝试单击按钮时,我的代码如下所示:

private void SendDialogButtonClick(IntPtr windowHandle, ApplicationStartType applicationStartType)
    {
        if (applicationStartType == ApplicationStartType.Localy)
            buttonClassName = "WindowsForms10.BUTTON.app.0.3ce0bb8";
        else if (applicationStartType == ApplicationStartType.ClickOnce)
            buttonClassName = "WindowsForms10.BUTTON.app.0.3d893c";

        // Find the "&No"-button
        IntPtr buttonAndNoHandle = FindWindowEx(windowHandle, IntPtr.Zero, buttonClassName, "&No");

        // Send the button click event to the appropriate button found on the dialog
        if (buttonAndNoHandle.ToInt64() != 0)
        {
            SendMessage(new HandleRef(null, buttonAndNoHandle), WM_CLICK, IntPtr.Zero, IntPtr.Zero);
        }
    }

【问题讨论】:

    标签: c# pinvoke ui-automation


    【解决方案1】:

    是的,这很难,类名是自动生成的。您不能使用 FindWindowEx(),您必须使用 EnumChildWindows() 和 GetClassName() 来迭代控件。

    您可以修改Managed Spy tool 的源代码,让这一切变得更加简单和清晰。

    【讨论】:

      猜你喜欢
      • 2011-02-03
      • 2019-03-16
      • 2012-09-17
      • 2016-08-23
      • 2011-07-21
      • 1970-01-01
      • 2015-03-30
      • 1970-01-01
      • 2015-06-10
      相关资源
      最近更新 更多