模拟鼠标在自动化测试中的应用
尽管UI Automation可以实现控件的自动化,但是,自定义控件的自动化往往由于自定义控件中对自动化的支持不够,而导致无法使用UI Automation的相关Pattern来操作此控件。此时我们可以使用模拟鼠标的操作来达到相应的效果。
. NET并没有提供改变鼠标指针位置、模拟点击操作的函数;但是Windows API提供了。在.NET中模拟鼠标通常是调用底层的API来实现。
注:在使用前必须添加using System.Runtime.InteropServices;命名空间。
移动鼠标到指定的位置
/// <summary> /// Add mouse move event /// </summary> /// <param name="x">Move to specify x coordinate</param> /// <param name="y">Move to specify y coordinate</param> /// <returns></returns> [DllImport("user32.dll")] extern static bool SetCursorPos(int x, int y); |
该函数可以改变鼠标指针的位置。其中x,y是相对于屏幕左上角的绝对位置。
移动鼠标到指定的位置并进行相应的鼠标操作
/// <summary> /// Mouse click event /// </summary> /// <param name="mouseEventFlag">MouseEventFlag </param> /// <param name="incrementX">X coordinate</param> /// <param name="incrementY">Y coordinate</param> /// <param name="data"></param> /// <param name="extraInfo"></param> [DllImport("user32.dll")] extern static void mouse_event(int mouseEventFlag, int incrementX, int incrementY, uint data, UIntPtr extraInfo); |
这个函数不仅可以设置鼠标指针绝对的位置,而且可以以相对坐标来设置。另外,该函数还可以模拟鼠标左右键点击、鼠标滚轮操作等。其中的MouseEventFlag是一个常量,定义如下:
const int MOUSEEVENTF_MOVE = 0x0001; const int MOUSEEVENTF_LEFTDOWN = 0x0002; const int MOUSEEVENTF_LEFTUP = 0x0004; const int MOUSEEVENTF_RIGHTDOWN = 0x0008; const int MOUSEEVENTF_RIGHTUP = 0x0010; const int MOUSEEVENTF_MIDDLEDOWN = 0x0020; const int MOUSEEVENTF_MIDDLEUP = 0x0040; const int MOUSEEVENTF_ABSOLUTE = 0x8000; |
上面列举的两个函数的详细说明,可参考MSDN帮助文档。
如下代码为在控件的指定位置上单击鼠标左键和右键。
public void ClickLeftMouse(int processId, string automationId) { AutomationElement element = FindElementById(processId, automationId); if (element == null) { throw new NullReferenceException(string.Format("Element with AutomationId '{0}' and Name '{1}' can not be find.", element.Current.AutomationId, element.Current.Name)); } Rect rect = element.Current.BoundingRectangle; int IncrementX = (int)(rect.Left + rect.Width / 2); int IncrementY = (int)(rect.Top + rect.Height / 2); //Make the cursor position to the element. SetCursorPos(IncrementX, IncrementY); //Make the left mouse down and up. mouse_event(MOUSEEVENTF_LEFTDOWN, IncrementX, IncrementY, 0, 0); mouse_event(MOUSEEVENTF_LEFTUP, IncrementX, IncrementY, 0, 0); } public void ClickRightMouse(int processId, string automationId) { AutomationElement element = FindElementById(processId, automationId); if (element == null) { throw new NullReferenceException(string.Format("Element with AutomationId '{0}' and Name '{1}' can not be find.", element.Current.AutomationId, element.Current.Name)); } Rect rect = element.Current.BoundingRectangle; int IncrementX = (int)(rect.Left + rect.Width / 2); int IncrementY = (int)(rect.Top + rect.Height / 2); //Make the cursor position to the element. SetCursorPos(IncrementX, IncrementY); //Make the left mouse down and up. mouse_event(MOUSEEVENTF_RIGHTDOWN, IncrementX, IncrementY, 0, 0); mouse_event(MOUSEEVENTF_RIGHTUP, IncrementX, IncrementY, 0, 0); } |
在上面的代码中,我们通过如下步骤来达到点击鼠标的目的:
1.通过UI Automation来找到需要鼠标点击的控件;
2.然后取得此控件的坐标;
3.在设置点击坐标在控件的中心位置;
4.移动鼠标的位置到控件的中心位置(此步骤可以不要,但是为了达到双保险的效果,我们还是移动一次)。
5.调用Win32 API模拟鼠标点击控件。
注:Rect需要引用WindowBase.dll,添加using System.Windows;明明空间。