完善主體資料,免費(fèi)贈(zèng)送VIP會(huì)員!
* 主體類型
* 企業(yè)名稱
* 信用代碼
* 所在行業(yè)
* 企業(yè)規(guī)模
* 所在職位
* 姓名
* 所在行業(yè)
* 學(xué)歷
* 工作性質(zhì)
請(qǐng)先選擇行業(yè)
您還可以選擇以下福利:
行業(yè)福利,領(lǐng)完即止!

下載app免費(fèi)領(lǐng)取會(huì)員

NULL

ad.jpg

二次開(kāi)發(fā)教程:C# 初探UI Automation

發(fā)布于:2019-07-24 16:45:29

網(wǎng)友投稿

更多

最近研究自動(dòng)化測(cè)試,看了一下UI Automation的微軟例子,表示太老了,遇到各種問(wèn)題,


UI Spy 好像已經(jīng)被放棄了,可以用inspect.exe來(lái)代替,win10 的路徑為:"C:\Program Files (x86)\Windows Kits\8.1\bin\x64\inspect.exe"


這個(gè)用來(lái)查詢automationId,


官網(wǎng)是以計(jì)算器例子,下面是在win10 修改后能運(yùn)行版本




    class CalcAutomationClient

    {


         AutomationElement calWindow = null;//計(jì)算器窗口主窗口元素



         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);//此處必須等待一下,應(yīng)該是計(jì)算器的等待計(jì)算器完全加載,不然控件 找不到

            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=="計(jì)算器")

                {

                    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;

        }

    }

本文版權(quán)歸腿腿教學(xué)網(wǎng)及原創(chuàng)作者所有,未經(jīng)授權(quán),謝絕轉(zhuǎn)載。

未標(biāo)題-1.jpg

上一篇:二次開(kāi)發(fā)教程:C# 動(dòng)態(tài)生成程序集

下一篇:二次開(kāi)發(fā)教程:C# 反射性能

60acb4e0ef112.png