C# 初探UI Automation

来源:互联网 发布:软件项目报价表 编辑:程序博客网 时间:2024/05/22 12:00


最近研究自动化测试,看了一下UI Automation的微软例子,表示太老了,遇到各种问题,

UI Spy 好像已经被放弃了,可以用inspect.exe来代替,win10 的路径为:"C:\Program Files (x86)\Windows Kits\8.1\bin\x64\inspect.exe"

这个用来查询automationId,

官网是以计算器例子,下面是在win10 修改后能运行版本


    class CalcAutomationClient
    {

         AutomationElement calWindow = null;//计算器窗口主窗口元素


         string resultTextAutoID = "CalculatorResults";
         string btn5AutoID = "num5Button";
         string btn3AutoID = "num3Button";
         string btn2AutoID = "num2Button";
         string btnPlusAutoID = "plusButton";
         string btnSubAutoId = "94";
         string btnEqualAutoID = "equalButton";
        static void Main(string[] args)
        {
            CalcAutomationClient autoClient = new CalcAutomationClient();
            AutomationEventHandler eventHandler = new AutomationEventHandler(autoClient.OnWindowOpenOrClose);
            Automation.AddAutomationEventHandler(WindowPattern.WindowOpenedEvent, AutomationElement.RootElement, TreeScope.Children, eventHandler);
            Process.Start("calc.exe");
            Console.ReadLine();
        }

        private void OnWindowOpenOrClose(object sender, AutomationEventArgs e)
        {
            if (calWindow != null)
                return;
            if(e.EventId!=WindowPattern.WindowOpenedEvent)
            {
                return;
            }
            if(sender ==null)
            {
                Console.WriteLine("sender is null");
                return;
            }
            Thread.Sleep(1000);//此处必须等待一下,应该是计算器的等待计算器完全加载,不然控件 找不到
            AutomationElement sourceElement = null;
            sourceElement = sender as AutomationElement;
            Console.WriteLine(sourceElement.Current.Name);
            try
            {
                sourceElement = sender as AutomationElement;
                Console.WriteLine(sourceElement.Current.Name);
                if (sourceElement.Current.Name=="计算器")
                {
                    calWindow = sourceElement;
                }
            }
            catch(Exception ex)
            {
                Console.WriteLine("ex:" + ex.Message);
                return;
            }
            if(calWindow == null)
            {
                return;
            }
            ExcuteTest();
        }
        private  void ExcuteTest()
        {
            ExcuteButtonInvoke(btn2AutoID);
            ExcuteButtonInvoke(btnPlusAutoID);
            ExcuteButtonInvoke(btn3AutoID);
            ExcuteButtonInvoke(btnEqualAutoID);
            string rs = GetCurrentResult();
            Console.WriteLine(rs);
        }
        private  void ExcuteButtonInvoke(string automationId)
        {
            Condition conditions = new AndCondition(
                new PropertyCondition(AutomationElement.AutomationIdProperty,automationId),
                new PropertyCondition(AutomationElement.ControlTypeProperty,ControlType.Button));
            if (calWindow == null)
                return;
            AutomationElementCollection collection = calWindow.FindAll(TreeScope.Descendants, conditions);
            if (collection == null || collection.Count == 0)
                return;
            AutomationElement btn = collection[0];
            if (btn != null)
            {
                InvokePattern invokeptn = (InvokePattern)btn.GetCurrentPattern(InvokePattern.Pattern);
                invokeptn.Invoke();
            }
            Thread.Sleep(1000);
        }
        private string GetCurrentResult()
        {
            Condition conditions = new AndCondition(
                new PropertyCondition(AutomationElement.AutomationIdProperty, resultTextAutoID),
                new PropertyCondition(AutomationElement.ControlTypeProperty,ControlType.Text));
            AutomationElement text = calWindow.FindAll(TreeScope.Descendants, conditions)[0];
            return text.Current.Name;
        }
    }


原创粉丝点击