I am writing a library for automation testing. There is hell lot of tools and libraries which is not solving all the purpose. Where you need to extend or create plug-in or pay for support. This Need leads to innovation. I started with MS UI automation library, that’s a good place to start. After used it for some time I thought there is no enough support for legacy win32 applications. And it is not supporting for the mouse operation. There are few places where the mouse operation is must, that lead to found mouse simulation using win32 API. There are few other operations like mouse wheel move …. But I guess it not need for automation
Mouse Structure
[StructLayout(LayoutKind.Sequential)]
public struct MouseInput
{
private int dx;
private int dy;
private int mouseData;
private int dwFlags;
private int time;
private IntPtr dwExtraInfo;
public MouseInput(int dwFlags, IntPtr dwExtraInfo)
{
this.dwFlags = dwFlags;
this.dwExtraInfo = dwExtraInfo;
dx = 0;
dy = 0;
time = 0;
mouseData = 0;
}
}
Mouse Constants
public const int INPUT_KEYBOARD = 1;
public const int INPUT_MOUSE = 0;
public const int MOUSEEVENTF_ABSOLUTE = 0x8000;
public const int MOUSEEVENTF_LEFTDOWN = 0x0002;
public const int MOUSEEVENTF_LEFTUP = 0x0004;
public const int MOUSEEVENTF_MIDDLEDOWN = 0x0020;
public const int MOUSEEVENTF_MIDDLEUP = 0x0040;
public const int MOUSEEVENTF_MOVE = 0x0001;
public const int MOUSEEVENTF_RIGHTDOWN = 0x0008;
public const int MOUSEEVENTF_RIGHTUP = 0x0010;
public const int MOUSEEVENTF_VIRTUALDESK = 0x4000;
public const int MOUSEEVENTF_WHEEL = 0x0800;
public const int MOUSEEVENTF_XDOWN = 0x0080;
public const int MOUSEEVENTF_XUP = 0x0100;
Win32 Methods for mouse
[DllImport("user32.dll", SetLastError = true)]
public static extern uint SendInput(uint nInputs,ref Input pInputs, int cbSize);
[DllImport("user32.dll")]
public static extern bool SetCursorPos(System.Drawing.Point cursorInfo);
[StructLayout(LayoutKind.Explicit)] //This is used for key board and mouse
public struct Input
{
[FieldOffset(0)]
private int type;
[FieldOffset(4)]
Private MouseInput mi;
public static Input Mouse(MouseInput mouseInput)
{
var input = new Input {type = INPUT_MOUSE, mi = mouseInput};
return input;
}
}
Actual methods
public void Click(Point point) //System.Drawing.Point
{
SetCursorPosition(point);
MouseOperation(MOUSEEVENTF_LEFTDOWN);
//Add delay if it not working fine
MouseOperation(MOUSEEVENTF_LEFTUP);
}
public void RightClick(Point point)
{
SetCursorPosition(point);
MouseOperation(MOUSEEVENTF_RIGHTDOWN);
//Add delay if it not working fine
MouseOperation(MOUSEEVENTF_RIGHTUP);
}
public void DoubleClick(Point point)
{
SetCursorPosition(point);
MouseOperation(MOUSEEVENTF_LEFTDOWN);
//Add delay if it not working fine
MouseOperation(MOUSEEVENTF_LEFTUP);
MouseOperation(MOUSEEVENTF_LEFTDOWN);
//Add delay if it not working fine
MouseOperation(MOUSEEVENTF_LEFTUP);
}
private void SetCursorPosition(Point curPoint)
{
SetCursorPos(curPoint);
}
private void MouseOperation(int operation)
{
var mInput = new MouseInput(operation,GetMessageExtraInfo());
Input input = Input.Mouse(mInput);
SendInput(1, ref input, Marshal.SizeOf(typeof (Input)));
}
No comments:
Post a Comment