完善主體資料,免費(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

    二次開發(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

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

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

    主站蜘蛛池模板: 亚欧在线精品免费观看一区| 色妞AV永久一区二区国产AV| 中文字幕日韩精品一区二区三区| 国产AV一区二区三区无码野战| 国产日韩精品一区二区三区在线| 中文字幕在线观看一区二区三区| 亲子乱AV视频一区二区| 日韩一区精品视频一区二区| 亚洲午夜精品一区二区公牛电影院 | 国产一区二区三区露脸| 亚洲第一区精品观看| 精品国产福利第一区二区三区| 乱码精品一区二区三区| 亚洲AV一区二区三区四区| 日本韩国黄色一区二区三区| 国产精品女同一区二区| 无码人妻精品一区二区三区久久久 | 香蕉久久av一区二区三区| 中文字幕AV一区二区三区人妻少妇| 日本一区二区三区在线视频 | 美女免费视频一区二区| 亚洲一区二区三区AV无码| 99精品国产高清一区二区麻豆| 免费人妻精品一区二区三区| 无码人妻久久一区二区三区免费| 国产精品一区二区三区久久| 久久国产精品一区免费下载| 国产天堂在线一区二区三区| 亚洲国产综合精品中文第一区| 国产精品美女一区二区 | 久久久无码精品人妻一区| 久久精品无码一区二区app| AV鲁丝一区鲁丝二区鲁丝三区 | 国产高清不卡一区二区| 国产一区韩国女主播| 99久久无码一区人妻a黑| 最美女人体内射精一区二区| 日韩精品一区在线| 亚洲一区二区三区久久久久| 精品国产乱子伦一区二区三区| 免费无码一区二区|