Windows Mobile自动化测试之模拟用户操作

发表于:2009-9-24 14:50

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:未知    来源:51Testing博客转载

  其中hwndFocus就是我们想要的,但是不能高兴太早,这个函数似乎我们用不了,因为MSDN告诉我们“This function is only available to OEMs.”。无情的打击啊,不过柳暗花明的是在Windows Mobile Test Frameworkh中,微软提供了一个.NET DLL封装了这个函数,所以……嘿嘿~对了,那个DLL叫做Microsoft.WindowsCE.Win32API.dll,下面是一段C#代码:

public static void Input(string text){   
    GET_FOREGROUND_INFO info = new GET_FOREGROUND_INFO();   
    Win32.GetForegroundInfo(out info);   
    IntPtr focusedWindowHandle = new IntPtr(info.hwndFocus);   
    char[] txt = text.ToCharArray();   
    foreach (char c in txt){       
        SendChar(focusedWindowHandle, WM_CHAR, c, 0);   
    }
}
[DllImport("coredll.dll", SetLastError = true)]
private static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);
[DllImport("coredll.dll", EntryPoint = "SendMessage", CharSet = CharSet.Auto)]
private static extern int SendChar(IntPtr hwnd, int msg, int c, int lParam);
private const int WM_CHAR = 0x0102;

  模拟鼠标输入

  同键盘输入类似,模拟鼠标输入也有一个非常好的API

VOID mouse_event(  DWORD dwFlags,   DWORD dx,   DWORD dy,   DWORD dwData,   DWORD dwExtraInfo );

  用法也非常简单,只是有一点需要注意的是鼠标有自己的坐标,X,Y轴都是从0到65535,所以,如果你想在某个像素位置点击的话,别忘了转换一下坐标,下面是一个模拟鼠标拖拽的C#方法:

public static void Drag(int fromX, int fromY, int toX, int toY)

{

    if (fromX < 0 || fromX > (SCREEN_WIDTH - 1))
    {
        return;
    }

    if (fromY < 0 || fromY > (SCREEN_HEIGHT - 1))
    {
        return;
    }

    if (toX < 0 || toX > (SCREEN_WIDTH - 1))
    {
        return;
    }

    if (toY < 0 || toY > (SCREEN_HEIGHT - 1))
    {
        return;
    }

    Point from = ScreenToMouse(fromX, fromY);
    Point to = ScreenToMouse(toX, toY);

    MouseEvent(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE, from.X, from.Y, 0, 0);
    MouseEvent(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
    MouseEvent(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE, to.X, to.Y, 0, 0);
    MouseEvent(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);

}

private static Point ScreenToMouse(int x, int y)

{
    x = x < 0 ? 0 : x;
    x = x > (SCREEN_WIDTH - 1) ? (SCREEN_WIDTH - 1) : x;
    y = y < 0 ? 0 : y;
    y = y > (SCREEN_HEIGHT - 1) ? (SCREEN_HEIGHT - 1) : y;

    Point mouse = new Point();
    mouse.X = x * 65535 / SCREEN_WIDTH;
    mouse.Y = y * 65535 / SCREEN_HEIGHT;

    return mouse;
}

[DllImport("coredll.dll", EntryPoint = "mouse_event")]
private static extern int MouseEvent(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);

private const int MOUSEEVENTF_LEFTDOWN = 0x0002;
private const int MOUSEEVENTF_LEFTUP = 0x0004;
private const int MOUSEEVENTF_MOVE = 0x0001;
private const int MOUSEEVENTF_ABSOLUTE = 0x8000;

private const int SCREEN_WIDTH = 240;
private const int SCREEN_HEIGHT = 320;

  知道了怎么拖拽,要实现点击啥的就简单了吧,哈哈~

  总结完了~

22/2<12
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号