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

      主站蜘蛛池模板: 无码人妻品一区二区三区精99| 在线观看国产一区二区三区| 一区二区福利视频| 亚洲国产老鸭窝一区二区三区| 好吊妞视频一区二区| 国产福利精品一区二区| 欧洲精品码一区二区三区免费看| 日本高清一区二区三区| 精品一区二区三区四区| 日韩一区二区三区在线精品| 成人一区二区三区视频在线观看| 麻豆AV无码精品一区二区 | 精品动漫一区二区无遮挡| 亚洲综合av永久无码精品一区二区| 国产在线精品一区二区| 精品无人乱码一区二区三区| 精品国产AV一区二区三区| 久久精品黄AA片一区二区三区 | 日本一区二区在线| 中文日韩字幕一区在线观看| 精品一区二区三区无码免费直播| 日韩精品一区二区三区国语自制 | 国产成人高清视频一区二区 | 国产精品福利一区二区| 精品无人区一区二区三区| 久久国产免费一区| 日韩亚洲AV无码一区二区不卡 | 国产精品福利一区二区久久| 久久精品无码一区二区无码| 亚洲一区动漫卡通在线播放| 97久久精品无码一区二区天美 | 亚洲欧洲精品一区二区三区| 日本视频一区在线观看免费 | 日本精品一区二区久久久| 一区二区高清在线观看| 国产在线精品一区二区三区直播| 国产成人AV一区二区三区无码 | 精品乱子伦一区二区三区| 亚洲乱码国产一区网址| 国产精品无圣光一区二区| 一区二区三区四区免费视频 |