本文通过一个实例来介绍怎样使用UI Automation实现软件的自动化测试。
1. 首先建立一个待测试的winform程序,即UI Automation的服务端。
下面是button事件处理程序。
private void button1_Click(object sender, EventArgs e) { int i = int.Parse(textBox1.Text); int j = int.Parse(textBox2.Text); textBox3.Text = (i + j).ToString(); } |
2. 建立一个测试程序,做UI Automaion的客户端。
添加引用:UIAutomationClient.dll 和 UIAutomationTypes.dll
1using System; 2using System.Diagnostics; 3using System.Threading; 4using System.Windows.Automation.Provider; 5using System.Windows.Automation.Text; 6using System.Windows.Automation; 7 8namespace UIAutomationTest 9{ 10 class Program 11 { 12 static void Main(string[] args) 13 { 14 try 15 { 16 Console.WriteLine("\nBegin WinForm UIAutomation test run\n"); 17 // launch Form1 application 18 // get refernce to main Form control 19 // get references to user controls 20 // manipulate application 21 // check resulting state and determine pass/fail 22 23 Console.WriteLine("\nBegin WinForm UIAutomation test run\n"); 24 Console.WriteLine("Launching WinFormTest application"); 25 //启动被测试的程序 26 Process p = Process.Start(@"E:\Project\WinFormTest\WinFormTest\bin\Debug\WinFormTest.exe"); 27 28 //自动化根元素 29 AutomationElement aeDeskTop = AutomationElement.RootElement; 30 31 Thread.Sleep(2000); 32 AutomationElement aeForm = AutomationElement.FromHandle(p.MainWindowHandle); 33 //获得对主窗体对象的引用,该对象实际上就是 Form1 应用程序(方法一) 34 //if (null == aeForm) 35 //{ 36 // Console.WriteLine("Can not find the WinFormTest from."); 37 //} 38 39 //获得对主窗体对象的引用,该对象实际上就是 Form1 应用程序(方法二) 40 int numWaits = 0; 41 do 42 { 43 Console.WriteLine("Looking for WinFormTest……"); 44 //查找第一个自动化元素 45 aeForm = aeDeskTop.FindFirst(TreeScope.Children, new PropertyCondition( 46 AutomationElement.NameProperty, "Form1")); 47 ++numWaits; 48 Thread.Sleep(100); 49 } while (null == aeForm && numWaits < 50); 50 if (null == aeForm) 51 throw new NullReferenceException("Failed to find WinFormTest."); 52 else 53 Console.WriteLine("Found it!"); 54 55 Console.WriteLine("Finding all user controls"); 56 //找到第一次出现的Button控件 57 AutomationElement aeButton = aeForm.FindFirst(TreeScope.Children, 58 new PropertyCondition(AutomationElement.NameProperty, "button1")); 59 60 //找到所有的TextBox控件 61 AutomationElementCollection aeAllTextBoxes = aeForm.FindAll(TreeScope.Children, 62 new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit)); 63 64 // 控件初始化的顺序是先初始化后添加到控件 65 // this.Controls.Add(this.textBox3); 66 // this.Controls.Add(this.textBox2); 67 // this.Controls.Add(this.textBox1); 68 69 AutomationElement aeTextBox1 = aeAllTextBoxes[2]; 70 AutomationElement aeTextBox2 = aeAllTextBoxes[1]; 71 AutomationElement aeTextBox3 = aeAllTextBoxes[0]; 72 73 Console.WriteLine("Settiing input to '30'"); 74 //通过ValuePattern设置TextBox1的值 75 ValuePattern vpTextBox1 = (ValuePattern)aeTextBox1.GetCurrentPattern(ValuePattern.Pattern); 76 vpTextBox1.SetValue("30"); 77 Console.WriteLine("Settiing input to '50'"); 78 //通过ValuePattern设置TextBox2的值 79 ValuePattern vpTextBox2 = (ValuePattern)aeTextBox2.GetCurrentPattern(ValuePattern.Pattern); 80 vpTextBox2.SetValue("50"); 81 Thread.Sleep(1500); 82 Console.WriteLine("Clickinig on button1 Button."); 83 //通过InvokePattern模拟点击按钮 84 InvokePattern ipClickButton1 = (InvokePattern)aeButton.GetCurrentPattern(InvokePattern.Pattern); 85 ipClickButton1.Invoke(); 86 Thread.Sleep(1500); 87 88 //验证计算的结果与预期的结果是否相符合 89 Console.WriteLine("Checking textBox3 for '80'"); 90 TextPattern tpTextBox3 = (TextPattern)aeTextBox3.GetCurrentPattern(TextPattern.Pattern); 91 string result = tpTextBox3.DocumentRange.GetText(-1);//获取textbox3中的值 92 //获取textbox3中的值 93 //string result = (string)aeTextBox2.GetCurrentPropertyValue(ValuePattern.ValueProperty); 94 if ("80" == result) 95 { 96 Console.WriteLine("Found it."); 97 Console.WriteLine("TTest scenario: *PASS*"); 98 } 99 else 100 { 101 Console.WriteLine("Did not find it."); 102 Console.WriteLine("Test scenario: *FAIL*"); 103 } 104 105 Console.WriteLine("Close application in 5 seconds."); 106 Thread.Sleep(5000); 107 //实现关闭被测试程序 108 WindowPattern wpCloseForm = (WindowPattern)aeForm.GetCurrentPattern(WindowPattern.Pattern); 109 wpCloseForm.Close(); 110 111 Console.WriteLine("\nEnd test run\n"); 112 } 113 catch (Exception ex) 114 { 115 Console.WriteLine("Fatal error: " + ex.Message); 116 } 117 } 118 } 119} 120 |
* MS提供的控件Pattern
DockPattern ExpandCollapsePattern
GridPattern GridItemPattern
InvokePattern MultipleViewPattern
RangeValuePattern ScrollPattern
ScrollItemPattern SelectionPattern
SelectionItemPattern TablePattern
TableItemPattern TextPattern
TogglePattern TransformPattern
ValuePattern WindowPattern
相关阅读: