• 自动化测试(二)


    实例1:找到已知名称的窗口

    AutomationElement desktop = AutomationElement.RootElement;
    string wndName="XXX";
    Wnd = desktop.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty,wndName));

    实例2:找到窗口中一个显示为“开始”的文本Label

    string ControlName="开始";
                var lbStart = Wnd.FindFirst(TreeScope.Descendants, new AndCondition(new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Text),new PropertyCondition(AutomationElement.NameProperty,ControlName)));

    实例3:有一个id为calendar的ComboBox,选择其中的July项

                string ComboItemName = "July";
                //找到id为calendar的ComboBox
                AutomationElement calendar = Wnd.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.AutomationIdProperty, "calendar"));
                
                //展开
                ExpandCollapsePattern ecPattern = (ExpandCollapsePattern)calendar.GetCurrentPattern(ExpandCollapsePattern.Pattern);
                ecPattern.Expand();
    
                //找到名称为July的项
                var typeCond = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.ListItem);
                
                AutomationElementCollection items = calendar.FindAll(TreeScope.Descendants, typeCond);
                AutomationElement itemToSelect = items[6];
    
                Object selectPattern = null;
                if (itemToSelect.TryGetCurrentPattern(SelectionItemPattern.Pattern, out selectPattern))
                {
                    ((SelectionItemPattern)selectPattern).Select();
                }

     实例4:点击一个无法Invoke的控件

    首先导入win32 api

            [DllImport("user32.dll", EntryPoint = "SetCursorPos")]
            public static extern int SetCursorPos(int x, int y);
    
    
            [DllImport("user32.dll", EntryPoint = "ShowWindow", CharSet = CharSet.Auto)]
            public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);
    
    
            [DllImport("user32.dll", EntryPoint = "SetForegroundWindow", SetLastError = true)]
            public static extern void SetForegroundWindow(IntPtr hwnd);
    
    
            [DllImport("user32.dll")]
            public static extern void mouse_event(uint dwFlags, int dx, int dy, uint dwData, UIntPtr dwExtraInfo);
    View Code
                var itemSeq = Wnd.FindFirst(TreeScope.Descendants, new AndCondition(c1, c2));
                if (itemSeq == null)
                {
                    MessageBox.Show("No ListItem序列found");
                }else
                {
                    System.Windows.Point p=new Point();
                    WindowsApi.SetForegroundWindow((IntPtr)Wnd.Current.NativeWindowHandle);
                    WindowsApi.ShowWindow((IntPtr)Wnd.Current.NativeWindowHandle, 3);
                    if (itemSeq.TryGetClickablePoint(out p))
                    {
                        WindowsApi.SetCursorPos((int)p.X, (int)p.Y);
                        WindowsApi.mouse_event(WindowsApi.MOUSEEVENTF_LEFTDOWN | WindowsApi.MOUSEEVENTF_LEFTUP, 0, 0, 0, (UIntPtr)0);
    
                    }
    
                }

    ShowWindow的参数 1,表示常规,2表示最小化,3表示最大化。

    通过SetCursorPos和mouse_event的方法,还可参考https://www.cnblogs.com/T-ARF/archive/2004/01/13/12172997.html

    实例5:向文本框发送回车键

    box = Wnd.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.AutomationIdProperty, "textBox2"));
    
    WindowsApi.SetForegroundWindow((IntPtr)Wnd.Current.NativeWindowHandle);
    box.SetFocus();
    SendKeys.SendWait("{ENTER}");

    实例6:向文本框发送回车键  方法2

                WindowsApi.SetForegroundWindow((IntPtr)Wnd.Current.NativeWindowHandle);
                box.SetFocus();
                WindowsApi.PostMessage((IntPtr)box.Current.NativeWindowHandle, 0x0100, 0x0D, 0);

    导入win32 api

        [DllImport("user32.dll")]
        static extern bool PostMessage(IntPtr hWnd, UInt32 Msg, int wParam, int lParam);

    0x0100表示WM_KEYDOWN,0x0D表示回车键,完整列表http://www.kbdedit.com/manual/low_level_vk_list.html

    来源:https://stackoverflow.com/questions/3047375/simulating-key-press-c-sharp

     一个模拟测试程序

  • 相关阅读:
    ZOJ 3278 8G Island 二分+二分
    POJ 2785 4 Values whose Sum is 0 二分
    POJ 3063 Sherlock Holmes 随机化
    UVA 10881 Piortr‘s Ants 思维 模拟
    UVA 1388 Graveyard
    Codeforces Round #410 (Div. 2) B. Mike and strings
    Codeforces 821B
    51nod 1103 N的倍数 抽屉原理
    Codeforces Round #427 (Div. 2) 835D
    Codeforces Round #427 (Div. 2) 835C-Star sky 二维前缀和
  • 原文地址:https://www.cnblogs.com/noigel/p/13958186.html
Copyright © 2020-2023  润新知