天天看點

使用 UI Automation 實作自動化測試--2

本文通過一個執行個體來介紹怎樣使用UI Automation實作軟體的自動化測試。

1. 首先建立一個待測試的winform程式,即UI Automation的服務端。

使用 UI Automation 實作自動化測試--2

下面是button事件處理程式。

  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3.     int i = int.Parse(textBox1.Text);
  4.     int j = int.Parse(textBox2.Text);
  5.     textBox3.Text = (i + j).ToString();
  6. }

複制代碼 2. 建立一個測試程式,做UI Automaion的用戶端。

添加引用:UIAutomationClient.dll 和 UIAutomationTypes.dll

  1.   1using System;
  2.   2using System.Diagnostics;
  3.   3using System.Threading;
  4.   4using System.Windows.Automation.Provider;
  5.   5using System.Windows.Automation.Text;
  6.   6using System.Windows.Automation;
  7.   7
  8.   8namespace UIAutomationTest
  9.   9{
  10. 10    class Program
  11. 11    {
  12. 12        static void Main(string[] args)
  13. 13        {
  14. 14            try
  15. 15            {
  16. 16                Console.WriteLine("\nBegin WinForm UIAutomation test run\n");
  17. 17                // launch Form1 application
  18. 18                // get refernce to main Form control
  19. 19                // get references to user controls
  20. 20                // manipulate application
  21. 21                // check resulting state and determine pass/fail
  22. 22
  23. 23                Console.WriteLine("\nBegin WinForm UIAutomation test run\n");
  24. 24                Console.WriteLine("Launching WinFormTest application");
  25. 25                //啟動被測試的程式
  26. 26                Process p = Process.Start(@"E:\Project\WinFormTest\WinFormTest\bin\Debug\WinFormTest.exe");
  27. 27
  28. 28                //自動化根元素
  29. 29                AutomationElement aeDeskTop = AutomationElement.RootElement;
  30. 30
  31. 31                Thread.Sleep(2000);
  32. 32                AutomationElement aeForm = AutomationElement.FromHandle(p.MainWindowHandle);
  33. 33                //獲得對主窗體對象的引用,該對象實際上就是 Form1 應用程式(方法一)
  34. 34                //if (null == aeForm)
  35. 35                //{
  36. 36                //    Console.WriteLine("Can not find the WinFormTest from.");
  37. 37                //}
  38. 38
  39. 39                //獲得對主窗體對象的引用,該對象實際上就是 Form1 應用程式(方法二)
  40. 40                int numWaits = 0;
  41. 41                do
  42. 42                {
  43. 43                    Console.WriteLine("Looking for WinFormTest……");
  44. 44                    //查找第一個自動化元素
  45. 45                    aeForm = aeDeskTop.FindFirst(TreeScope.Children, new PropertyCondition(
  46. 46                        AutomationElement.NameProperty, "Form1"));
  47. 47                    ++numWaits;
  48. 48                    Thread.Sleep(100);
  49. 49                } while (null == aeForm && numWaits < 50);
  50. 50                if (null == aeForm)
  51. 51                    throw new NullReferenceException("Failed to find WinFormTest.");
  52. 52                else
  53. 53                    Console.WriteLine("Found it!");
  54. 54
  55. 55                Console.WriteLine("Finding all user controls");
  56. 56                //找到第一次出現的Button控件
  57. 57                AutomationElement aeButton = aeForm.FindFirst(TreeScope.Children,
  58. 58                  new PropertyCondition(AutomationElement.NameProperty, "button1"));
  59. 59
  60. 60                //找到所有的TextBox控件
  61. 61                AutomationElementCollection aeAllTextBoxes = aeForm.FindAll(TreeScope.Children,
  62. 62                    new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit));
  63. 63
  64. 64                // 控件初始化的順序是先初始化後添加到控件
  65. 65                // this.Controls.Add(this.textBox3);                 
  66. 66                // this.Controls.Add(this.textBox2);
  67. 67                // this.Controls.Add(this.textBox1);
  68. 68
  69. 69                AutomationElement aeTextBox1 = aeAllTextBoxes[2];
  70. 70                AutomationElement aeTextBox2 = aeAllTextBoxes[1];
  71. 71                AutomationElement aeTextBox3 = aeAllTextBoxes[0];
  72. 72
  73. 73                Console.WriteLine("Settiing input to '30'");
  74. 74                //通過ValuePattern設定TextBox1的值
  75. 75                ValuePattern vpTextBox1 = (ValuePattern)aeTextBox1.GetCurrentPattern(ValuePattern.Pattern);
  76. 76                vpTextBox1.SetValue("30");
  77. 77                Console.WriteLine("Settiing input to '50'");
  78. 78                //通過ValuePattern設定TextBox2的值
  79. 79                ValuePattern vpTextBox2 = (ValuePattern)aeTextBox2.GetCurrentPattern(ValuePattern.Pattern);
  80. 80                vpTextBox2.SetValue("50");
  81. 81                Thread.Sleep(1500);
  82. 82                Console.WriteLine("Clickinig on button1 Button.");
  83. 83                //通過InvokePattern模拟點選按鈕
  84. 84                InvokePattern ipClickButton1 = (InvokePattern)aeButton.GetCurrentPattern(InvokePattern.Pattern);
  85. 85                ipClickButton1.Invoke();
  86. 86                Thread.Sleep(1500);
  87. 87
  88. 88                //驗證計算的結果與預期的結果是否相符合
  89. 89                Console.WriteLine("Checking textBox3 for '80'");
  90. 90                TextPattern tpTextBox3 = (TextPattern)aeTextBox3.GetCurrentPattern(TextPattern.Pattern);
  91. 91                string result = tpTextBox3.DocumentRange.GetText(-1);//擷取textbox3中的值
  92. 92                //擷取textbox3中的值
  93. 93                //string result = (string)aeTextBox2.GetCurrentPropertyValue(ValuePattern.ValueProperty);
  94. 94                if ("80" == result)
  95. 95                {
  96. 96                    Console.WriteLine("Found it.");
  97. 97                    Console.WriteLine("TTest scenario: *PASS*");
  98. 98                }
  99. 99                else
  100. 100                {
  101. 101                    Console.WriteLine("Did not find it.");
  102. 102                    Console.WriteLine("Test scenario: *FAIL*");
  103. 103                }
  104. 104
  105. 105                Console.WriteLine("Close application in 5 seconds.");
  106. 106                Thread.Sleep(5000);
  107. 107                //實作關閉被測試程式
  108. 108                WindowPattern wpCloseForm = (WindowPattern)aeForm.GetCurrentPattern(WindowPattern.Pattern);
  109. 109                wpCloseForm.Close();
  110. 110
  111. 111                Console.WriteLine("\nEnd test run\n");
  112. 112            }
  113. 113            catch (Exception ex)
  114. 114            {
  115. 115                Console.WriteLine("Fatal error: " + ex.Message);
  116. 116            }
  117. 117        }
  118. 118    }
  119. 119}
  120. 120

複制代碼 ( 文/ 開着拖拉機)

MS提供的控件Pattern

DockPattern                                ExpandCollapsePattern

GridPattern                                  GridItemPattern

InvokePattern                              MultipleViewPattern

RangeValuePattern                      ScrollPattern

ScrollItemPattern                        SelectionPattern

SelectionItemPattern                  TablePattern

TableItemPattern                        TextPattern

TogglePattern                            TransformPattern

ValuePattern                              WindowPattern

繼續閱讀