完善主體資料,免費贈送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# 反射性能

    60acb4e0ef112.png
    主站蜘蛛池模板: 国产伦精品一区二区三区视频猫咪 | 亚洲AV永久无码精品一区二区国产| 在线精品一区二区三区| 国产一区二区三区不卡在线看 | 亚洲一区二区观看播放| 无码精品前田一区二区| 一区二区在线观看视频| 国产精品乱码一区二区三区 | 国产成人一区二区三区高清 | 亚洲av无码天堂一区二区三区| 精品乱码一区二区三区四区| 波霸影院一区二区| 成人国产精品一区二区网站公司 | 国产精品免费大片一区二区| 国产精品成人免费一区二区| 91无码人妻精品一区二区三区L| 久久se精品一区二区| 国产日韩一区二区三免费高清| 91福利国产在线观看一区二区| 久久亚洲色一区二区三区| 国产激情一区二区三区 | 国产成人久久精品麻豆一区| 亚洲免费一区二区| 亚洲毛片αv无线播放一区| 一区二区三区四区国产| 中文字幕一区视频一线| 中文国产成人精品久久一区| 伊人精品视频一区二区三区| 国产精品亚洲产品一区二区三区| 国产成人无码精品一区不卡| 午夜DV内射一区区| 精品视频在线观看一区二区| 午夜精品一区二区三区在线视| 一级特黄性色生活片一区二区| 亚洲爽爽一区二区三区| 久夜色精品国产一区二区三区| 成人精品视频一区二区三区| 一本大道东京热无码一区| 无码人妻精品一区二区三区99仓本| 色噜噜狠狠一区二区三区果冻| 亚洲Av高清一区二区三区|