完善主體資料,免費(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高清在线观看一区二区| 久久se精品一区精品二区国产| 竹菊影视欧美日韩一区二区三区四区五区| 伊人色综合网一区二区三区| 精品一区二区三区3d动漫| 亚洲福利秒拍一区二区| 男人免费视频一区二区在线观看 | 亚洲图片一区二区| 日韩精品一区二区三区毛片| 日产亚洲一区二区三区| 久99精品视频在线观看婷亚洲片国产一区一级在线 | 精品国产精品久久一区免费式| 亚洲一区二区三区四区在线观看| 国产一区二区四区在线观看| 一区二区日韩国产精品| 日韩免费无码一区二区视频| 中文字幕在线一区二区在线| 日本不卡在线一区二区三区视频| 三上悠亚国产精品一区| 精品一区二区三区视频在线观看| 婷婷亚洲综合一区二区| 精品国产精品久久一区免费式| 日本免费电影一区| 天海翼一区二区三区高清视频| 怡红院一区二区在线观看| 亚洲欧美国产国产综合一区| 2021国产精品一区二区在线| 久久久久人妻一区精品色| 精品无码人妻一区二区免费蜜桃 | 3d动漫精品啪啪一区二区免费| 亚洲天堂一区二区三区四区| 蜜芽亚洲av无码一区二区三区| 国产福利酱国产一区二区| 中文字幕在线无码一区| 亚洲熟妇av一区二区三区 |