【问题标题】:Visual Studio (C#) - Simulate Win32 Button PressVisual Studio (C#) - 模拟 Win32 按钮按下
【发布时间】:2014-01-19 23:12:19
【问题描述】:

我需要自动化一些程序的常见活动,例如 Microsoft Word。这些测试不是用于商业用途,而是用于回归测试。

到目前为止,我只是移动鼠标,然后在按钮上方单击鼠标左键。 但是,我们的程序会验证按钮是否处于“按下”状态。当自动化按下鼠标点击按钮时,按钮不会按下(按钮不会“下沉”)

这是为什么呢?如何使自动鼠标单击使按钮像普通鼠标单击一样被“按下”?

在这种情况下,顺便说一句,我指的是快速工具栏中的“保存”按钮。

我使用的代码很简单——首先我找到我想要点击的对象,然后使用这段代码;

public void MouseClick(params string[] args)
{
    AutomationElement automationElement = elementUtils.FindObjectFullPath(args[1], args[4], args[5]);
    string action = args[args.Length - 1];

    if (automationElement == null)
        return;

    FocusApplicationExp(automationElement);

    try
    {
        int x = (int)automationElement.GetClickablePoint().X;
        int y = (int)automationElement.GetClickablePoint().Y;
        MouseMoveExp(x, y);
        MouseAction(action, automationElement);
    }
    catch (Exception)
    {
        Console.WriteLine("Failed to get clickable point for " + automationElement.Current.Name + " - Trying to get coordinates of bounding rectangle");
        tl.AddLine("Failed to get clickable point for " + automationElement.Current.Name + " - Trying to get coordinates of bounding rectangle");
        res.AddLine("Failed to get clickable point for " + automationElement.Current.Name + " - Trying to get coordinates of bounding rectangle");

        int x = (int)(automationElement.Current.BoundingRectangle.Right - automationElement.Current.BoundingRectangle.Width / 2);
        int y = (int)(automationElement.Current.BoundingRectangle.Bottom - automationElement.Current.BoundingRectangle.Height / 2);
        MouseMoveExp(x, y);
        MouseAction(action, automationElement);
        return;
    }
}

private void MouseAction(string action, AutomationElement automationElement)
{
    switch (action)
    {
        case "Left":
            Console.WriteLine("Left clicking on: " + automationElement.Current.Name);
            tl.AddLine("Left clicking on: " + automationElement.Current.Name);
            AutomationUtility.DoMouseClick();
            res.AddLine("Left clicked on: " + automationElement.Current.Name);
            break;
        case "Right":
            Console.WriteLine("Right clicking on: " + automationElement.Current.Name);
            tl.AddLine("Right clicking on: " + automationElement.Current.Name);
            AutomationUtility.DoMouseRightClick();
            res.AddLine("Right clicked on: " + automationElement.Current.Name);
            break;
        case "Double":
            Console.WriteLine("Double clicking on: " + automationElement.Current.Name);
            tl.AddLine("Double clicking on: " + automationElement.Current.Name);
            AutomationUtility.DoMouseDoubleClick();
            res.AddLine("Double clicked on: " + automationElement.Current.Name);
            break;
         default:
            break;
    }
}

public static void DoMouseClick()
{
    Mouse.Click(MouseButton.Left);
}

我确实尝试了一些变化,例如在按钮按下和按下之间设置一些延迟,以防事件发生但太快。但是即使我按住按钮,按钮也永远不会进入“按下”状态(当然是在自动化中)

更清楚一点 - 自动化确实有效,它确实点击了按钮,它确实会导致单词保存。这里的问题是使按钮在视觉上被按下(并且在程序方面,改变它的状态)。

我不确定这是否意味着什么,但识别按钮状态和事件的系统是“UX”,并且基于 Win32 的标识符确实以某种方式找到了“按下”状态。

编辑:过去几天我一直在阅读它,事情变得越来越清晰。 我使用的自动化是基于 UIAutomation,但是按钮状态“Pressed”是一个 MSAA 状态,它没有移动到 UI 对象的 UIAutomation 生成,所以基于 UIAutomation 的自动化不会调用这个状态。 换句话说,我需要找到一种方法来使用 UIAutomation 代码将 MSAA 信号发送到 UIAutomation 元素(包含 IAccessible 接口)。问题是,它只支持另一种方式(将屏蔽为 MSAA 信号的 UIAutomation 信号发送到 MSAA 元素,它不支持 MSAA-Only 信号,例如“Enter state Pressed”)

【问题讨论】:

  • 如您所见,这台机器的点击速度太快了。我们看不到您尝试过的替代方案。这对于 UI 测试应该无关紧要,重要的是 Word 看到鼠标点击。
  • 问题是,我们的程序(不是自动化)收集事件,并使用它们来了解是否执行了操作。在这种情况下,鼠标单击名称为“保存”和父级“快速工具栏”的按钮,对象状态:已按下、可聚焦。所以我需要自动点击鼠标来像人类一样工作。

标签: c# testing ms-word automation simulation


【解决方案1】:

我不知道你是否使用这种方式,但它可能对点击部分有所帮助

public void ClickOnPoint(IntPtr wndHandle )
{
//for handle you can use (this.Handle)
//you can assign point directly or recalibrate as you did already 
Point buttonPoint(100, 500)
// store old pos if needed
POINT oldPoint;
GetCursorPos(out oldPoint);
// get screen coordinates
ClientToScreen(wndHandle, ref buttonPoint);
// set cursor on coords, and press mouse
SetCursorPos(buttonPoint.X, buttonPoint.Y);
//you can change the timing
mouse_event(0x00000002, 0, 0, 0, UIntPtr.Zero); // left mouse button down
mouse_event(0x00000004, 0, 0, 0, UIntPtr.Zero); // left mouse button up
// return mouse 
SetCursorPos(oldPoint.X, oldPoint.Y);
}

【讨论】:

    【解决方案2】:

    也许使用 SendInput 结帐。

    在下面的帖子中,我发布了一些使用 SendInput 的类。

    How to move the cursor or simulate clicks for other applications?

    注意:如果延迟,我会看到保存按钮变为橙色。我没有看到它立即变成橙色。

      public static void LeftClick()
      {
         DoMouse( NativeMethods.MOUSEEVENTF.LEFTDOWN, new Point( 0, 0 ) );
         System.Threading.Thread.Sleep( 200 );
         DoMouse( NativeMethods.MOUSEEVENTF.LEFTUP, new Point( 0, 0 ) );
      }
    

    【讨论】:

    • 我试过了。从某种意义上说,它可以正确发送输入,但仍然不会导致按钮更改状态。还是谢谢你!
    • 没问题。觉得值得一试。 :)
    • 我已经更新了答案.. 延迟使用 SendInput 似乎会使按钮变为橙色。
    【解决方案3】:

    好吧,我没有找到我正在寻找的答案,但这似乎仍然有效;

     [System.Runtime.InteropServices.DllImport("user32.dll")]
        public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
    
        public const int MOUSEEVENTF_LEFTDOWN = 0x02;
        public const int MOUSEEVENTF_LEFTUP = 0x04;
        public const int MOUSEEVENTF_RIGHTDOWN = 0x08;
        public const int MOUSEEVENTF_RIGHTUP = 0x10;
    
        //This simulates a left mouse click
        public static void LeftMouseClick(int xpos, int ypos)
        {
            mouse_event(MOUSEEVENTF_LEFTDOWN, xpos, ypos, 0, 0);
            Thread.Sleep(100);
            mouse_event(MOUSEEVENTF_LEFTUP, xpos, ypos, 0, 0);
        }
    
        public static void RightMouseClick(int xpos, int ypos)
        {
            mouse_event(MOUSEEVENTF_RIGHTDOWN, xpos, ypos, 0, 0);
            Thread.Sleep(100);
            mouse_event(MOUSEEVENTF_RIGHTUP, xpos, ypos, 0, 0);
        }
    

    我不确定 Derek 或 Za7ef 的答案有何不同,因为我都试过了。我已经更改了几次代码,所以它可能有不同的原因。还是谢谢你!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-08
      • 1970-01-01
      • 2016-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多