完善主體資料,免費贈送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乱码一区二区三区按摩| 国产成人高清精品一区二区三区| 一区二区三区观看| 精品一区二区三区东京热| 日本一区免费电影| 国产精品一区在线麻豆| 亚洲爆乳无码一区二区三区| 在线成人一区二区| 无码人妻精品一区二区蜜桃网站| 人妻AV中文字幕一区二区三区 | 亚洲性色精品一区二区在线| 中文字幕一区二区三区在线不卡| 北岛玲在线一区二区| 亚洲熟妇AV一区二区三区浪潮| 无码AV中文一区二区三区| 在线精品动漫一区二区无广告| 一级毛片完整版免费播放一区| 日本一区二区三区在线看| 日韩精品免费一区二区三区| 精品免费国产一区二区三区| 亚洲一区二区三区写真| 中文字幕乱码人妻一区二区三区| 精品无码人妻一区二区免费蜜桃| 无码一区二区三区| 少妇无码一区二区三区| 99久久精品国产一区二区成人 | 一区二区高清在线观看| 好吊妞视频一区二区| 成人无号精品一区二区三区| 福利国产微拍广场一区视频在线| 最新中文字幕一区| 国产在线观看一区二区三区| 日韩一区二区三区在线观看| 激情内射亚州一区二区三区爱妻| 中文字幕一区二区三区在线播放 | 蜜臀AV无码一区二区三区| 麻豆AV无码精品一区二区 | 国产熟女一区二区三区五月婷| 国产精品一区电影| 中文日韩字幕一区在线观看|