完善主體資料,免費贈送VIP會員!
    * 主體類型
    * 企業名稱
    * 信用代碼
    * 所在行業
    * 企業規模
    * 所在職位
    * 姓名
    * 所在行業
    * 學歷
    * 工作性質
    請先選擇行業
    您還可以選擇以下福利:
    行業福利,領完即止!

    下載app免費領取會員

    NULL

    ad.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# 反射性能

    主站蜘蛛池模板: 中文字幕乱码亚洲精品一区 | 波多野结衣一区二区三区高清av | 风流老熟女一区二区三区| jazzjazz国产精品一区二区| 亚洲狠狠狠一区二区三区| 亚洲av无码一区二区三区不卡 | 黑人大战亚洲人精品一区| 日韩精品电影一区| 99无码人妻一区二区三区免费| 国产一区二区三区乱码| 大屁股熟女一区二区三区| 国产一区二区三区在线电影 | 午夜天堂一区人妻| 亚洲V无码一区二区三区四区观看 亚洲爆乳精品无码一区二区三区 亚洲爆乳无码一区二区三区 | 国产日韩一区二区三免费高清| 无码日韩人妻AV一区二区三区| 国产精品一区二区香蕉| 日韩一区二区三区不卡视频 | 亚拍精品一区二区三区| 久久久精品人妻一区二区三区四| 无码人妻品一区二区三区精99| 日本成人一区二区| 中文字幕亚洲综合精品一区| 一区二区三区国产精品| 蜜臀AV一区二区| 亚洲AV无码一区二区乱子仑| 国产精品视频一区国模私拍| 精品国产一区二区三区久久狼| 国模一区二区三区| 亚洲一区二区三区乱码A| 无人码一区二区三区视频| 无码人妻精品一区二区三区99仓本| 日韩精品一区二区三区色欲AV| 精品无码一区二区三区在线| 无码人妻av一区二区三区蜜臀| 日韩国产一区二区| 亚洲av综合av一区| 亲子乱av一区区三区40岁| 精品女同一区二区| 亚洲AV综合色区无码一区爱AV| 亚洲AV无码一区二区三区久久精品 |