完善主体资料,免费赠送VIP会员!
* 主体类型
* 企业名称
* 信用代码
* 所在行业
* 企业规模
* 所在职位
* 姓名
* 所在行业
* 学历
* 工作性质
请先选择行业
您还可以选择以下福利:
行业福利,领完即止!

下载app免费领取会员

NULL

5cdd2dc095060.jpg

二次开发教程:C# 初探UI Automation

发布于:2019-07-24 16:45:29

网友投稿

更多

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

        }

    }

本文版权归腿腿教学网及原创作者所有,未经授权,谢绝转载。

未标题-1.jpg

上一篇:二次开发教程:C# 动态生成程序集

下一篇:二次开发教程:C# 反射性能

60acb4e0ef112.png