• UI Automation By Microsoft


    Getting start with UI test automation using the new Microsoft UI Automation library, UIWalker.dll is written by Qijie Xue (qijiexue@outlook.com). Feel free to use below codes in your UI automation testing, please mark where you get this codes if you'd like to re-post.

      1 using System;
      2 using System.Collections.Generic;
      3 using System.Windows.Automation;
      4 using System.Diagnostics;
      5 using System.Threading;
      6 
      7 ///By Qijie Xue on 4.10.2013
      8 namespace UIWalker
      9 {
     10     public class UIApp
     11     {
     12         /// <summary>
     13         /// Start Executable Application
     14         /// </summary>
     15         /// <param name="args">.exe or .msi file</param>
     16         /// <returns>Application Process</returns>
     17         public static Process StartApp(string args)
     18         {
     19             string appString = string.Empty;
     20             appString = args;
     21 
     22             Process proc=Process.Start(appString);
     23             Thread .Sleep (2000);
     24             return proc;
     25             //using (Process appProcess = new Process())
     26             //{                
     27             //    appProcess.StartInfo.FileName = appString;
     28             //    appProcess.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
     29             //    appProcess.Start();
     30             //    Thread.Sleep(2000);
     31 
     32             //    return appProcess;
     33             //}
     34         }
     35     }
     36 
     37     public class UIEngine
     38     {
     39         static List<UIStruct> Elements = new List<UIStruct>();
     40         static List<AutomationElement> MenusElements = new List<AutomationElement>();
     41 
     42         /// <summary>
     43         /// Fetch all UI Elements in the window
     44         /// </summary>
     45         /// <param name="window">main window</param>
     46         /// <param name="recurse">whether fetch by recurse</param>
     47         /// <returns>a list of elements as UIStruc</returns>
     48         public static List<UIStruct> FetchElements(AutomationElement window, bool recurse)
     49         {
     50             FetchElements(window, recurse, 0);
     51             if (Elements != null)
     52             {
     53                 return Elements;
     54             }
     55             else
     56                 return null;
     57         }
     58 
     59         private static void FetchElements(AutomationElement ae, bool recurse, int level)
     60         {
     61             if (ae != null)
     62             {
     63                 Elements.Add(new UIStruct(ae, level));
     64             }
     65 
     66             if (recurse)
     67             {
     68                 foreach (AutomationElement item in ae.FindAll (TreeScope.Children ,Condition.TrueCondition))
     69                 {
     70                     FetchElements(item, recurse, level + 1);
     71                 }
     72             }
     73         }
     74 
     75         /// <summary>
     76         /// Fetch menus in the window
     77         /// </summary>
     78         /// <param name="window">main window</param>
     79         /// <returns>top menus as AutomatinElement</returns>
     80         public static List<AutomationElement> FetchMenus(AutomationElement window)
     81         {
     82             AutomationElementCollection menusBars = window.FindAll(TreeScope.Children, new PropertyCondition(
     83                 AutomationElement.ControlTypeProperty, ControlType.MenuBar));
     84             if (menusBars.Count == 0)
     85                 return null;
     86             AutomationElement mainMenuItem = menusBars[0];
     87             AutomationElementCollection menus = mainMenuItem.FindAll(TreeScope.Children, new PropertyCondition(
     88                 AutomationElement.ControlTypeProperty, ControlType.MenuItem));
     89             if (menus.Count == 0)
     90                 return null;
     91 
     92             for (int i = 0; i < menus.Count; i++)
     93             {
     94                 MenusElements.Add(menus[i]);
     95             }
     96 
     97             return MenusElements;
     98         }
     99     }
    100 
    101     public class UIAction
    102     {
    103         public static void SetSelectedComboBoxItem(AutomationElement comboBox, string item)
    104         {
    105             if (comboBox.Current.ControlType == ControlType.ComboBox)
    106             {
    107                 AutomationPattern automationPatternFromElement = GetSpecifiedPattern(comboBox, "ExpandCollapsePatternIdentifiers.Pattern");
    108 
    109                 ExpandCollapsePattern expandCollapsePattern = comboBox.GetCurrentPattern(automationPatternFromElement) as ExpandCollapsePattern;
    110 
    111                 expandCollapsePattern.Expand();
    112                 expandCollapsePattern.Collapse();
    113 
    114                 AutomationElement listItem = comboBox.FindFirst(TreeScope.Subtree, new PropertyCondition(AutomationElement.NameProperty, item));
    115 
    116                 automationPatternFromElement = GetSpecifiedPattern(listItem, "SelectionItemPatternIdentifiers.Pattern");
    117 
    118                 SelectionItemPattern selectionItemPattern = listItem.GetCurrentPattern(automationPatternFromElement) as SelectionItemPattern;
    119 
    120                 selectionItemPattern.Select();
    121             }
    122         }
    123 
    124         private static AutomationPattern GetSpecifiedPattern(AutomationElement element, string patternName)
    125         {
    126             AutomationPattern[] supportedPattern = element.GetSupportedPatterns();
    127 
    128             foreach (AutomationPattern pattern in supportedPattern)
    129             {
    130                 if (pattern.ProgrammaticName == patternName)
    131                     return pattern;
    132             }
    133 
    134             return null;
    135         }
    136 
    137         /// <summary>
    138         /// Invoke item in menu
    139         /// </summary>
    140         /// <param name="menu">top menu item</param>
    141         /// <param name="item">the item to invoke in the menu</param>
    142         public static void SelectMenu(AutomationElement menu, string item)
    143         {
    144             AutomationPattern automationPatternFromElement = GetSpecifiedPattern(menu, "InvokePatternIdentifiers.Pattern");
    145 
    146             if (automationPatternFromElement != null)
    147             {
    148                 InvokePattern invokeItemPattern = menu.GetCurrentPattern(automationPatternFromElement) as InvokePattern;
    149                 invokeItemPattern.Invoke();
    150 
    151                 //ExpandCollapsePattern expandCollapsePattern = menu.GetCurrentPattern(ExpandCollapsePattern.Pattern) as ExpandCollapsePattern;
    152 
    153                 //expandCollapsePattern.Expand();
    154                 //expandCollapsePattern.Collapse();
    155 
    156                 System.Threading.Thread.Sleep(2000);
    157 
    158                 AutomationElement menuItem = menu.FindFirst(TreeScope.Subtree, new PropertyCondition(AutomationElement.NameProperty, item));
    159                 automationPatternFromElement = GetSpecifiedPattern(menuItem, "InvokePatternIdentifiers.Pattern");
    160                 if (automationPatternFromElement != null)
    161                 {
    162                     //SelectionItemPattern selectionItemPattern = menu.GetCurrentPattern(automationPatternFromElement) as SelectionItemPattern;
    163 
    164                     //selectionItemPattern.Select();
    165 
    166                     invokeItemPattern = menuItem.GetCurrentPattern(automationPatternFromElement) as InvokePattern;
    167                     invokeItemPattern.Invoke();
    168                 }
    169             }
    170         }
    171 
    172         public static void ClickButton(AutomationElement button)
    173         {
    174             AutomationPattern automationPatternFromElement = GetSpecifiedPattern(button, "InvokePatternIdentifiers.Pattern");
    175             if (automationPatternFromElement != null)
    176             {
    177                 InvokePattern invokeItemPattern = button.GetCurrentPattern(automationPatternFromElement) as InvokePattern;
    178                 invokeItemPattern.Invoke();
    179                 Thread.Sleep(1000);
    180             }
    181         }
    182 
    183         public static void SetValue(AutomationElement textBox, string value)
    184         {
    185             if (textBox.Current.ControlType == ControlType.Edit)
    186             {
    187                 ValuePattern valuePattern = (ValuePattern)textBox.GetCurrentPattern(ValuePattern.Pattern);
    188                 valuePattern.SetValue(value);
    189             }
    190         }
    191 
    192         public static void SelectRadioBox(AutomationElement radioBox)
    193         {
    194             if (radioBox.Current.ControlType == ControlType.RadioButton) ;
    195             {
    196                 SelectionItemPattern radioButtonPattern = (SelectionItemPattern)radioBox.GetCurrentPattern(SelectionItemPattern.Pattern);
    197                 radioButtonPattern.Select();
    198             }
    199         }
    200 
    201         public static string GetTextBoxValue(AutomationElement textBox)
    202         {
    203             if (textBox.Current.ControlType == ControlType.Edit)
    204             {
    205                 TextPattern textPattern = (TextPattern)textBox.GetCurrentPattern(TextPattern.Pattern);
    206                 string result = textPattern.DocumentRange.GetText(-1);
    207                 if (result == null)
    208                 {
    209                     result = string.Empty;
    210                 }
    211                 return result;
    212             }
    213             else
    214                 return string.Empty;
    215         }
    216 
    217         public static void CloseForm(AutomationElement window)
    218         {
    219             if (window.Current.ControlType == ControlType.Window)
    220             {
    221                 WindowPattern windowPattern = (WindowPattern)window.GetCurrentPattern(WindowPattern.Pattern);
    222                 windowPattern.Close(); 
    223             }
    224         }
    225     }
    226 
    227     public class UIStruct
    228     {
    229         public AutomationElement  Element { get; set; }
    230         public int Level { get; set; }
    231 
    232         public UIStruct(AutomationElement ae, int level)
    233         {
    234             this.Element = ae;
    235             this.Level = level;
    236         }
    237     }
    238 }
  • 相关阅读:
    SQL SEREVR IO
    INTEL
    windows performance
    The DiskSpd Storage Performance Tool
    machine Learning
    NOSQL
    X64 Deep Dive
    Debugging and performance,ETW
    Disk Performance
    WCF transport-and-message-security
  • 原文地址:https://www.cnblogs.com/qixue/p/3012777.html
Copyright © 2020-2023  润新知