C# 初探UI Automation


最近研究自动化测试,看了一下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;
        }
    }


  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值