使用UI Automation实现自动化测试 -2
2010-03-31 11:26
411 查看
本文通过一个实例来介绍怎样使用UI Automation实现软件的自动化测试。
1. 首先建立一个待测试的winform程序,即UI Automation的服务端。
下面是button事件处理程序。
2. 建立一个测试程序,做UI Automaion的客户端。
添加引用:UIAutomationClient.dll 和 UIAutomationTypes.dll
MS提供的控件Pattern
DockPattern ExpandCollapsePattern
GridPattern GridItemPattern
InvokePattern MultipleViewPattern
RangeValuePattern ScrollPattern
ScrollItemPattern SelectionPattern
SelectionItemPattern TablePattern
TableItemPattern TextPattern
TogglePattern TransformPattern
ValuePattern WindowPattern
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
using System; using System.Diagnostics; using System.Threading; using System.Windows.Automation.Provider; using System.Windows.Automation.Text; using System.Windows.Automation; namespace UIAutomationTest { class Program { static void Main(string[] args) { try { Console.WriteLine("/nBegin WinForm UIAutomation test run/n"); // launch Form1 application // get refernce to main Form control // get references to user controls // manipulate application // check resulting state and determine pass/fail Console.WriteLine("/nBegin WinForm UIAutomation test run/n"); Console.WriteLine("Launching WinFormTest application"); //启动被测试的程序 Process p = Process.Start(@"E:/Project/WinFormTest/WinFormTest/bin/Debug/WinFormTest.exe"); //自动化根元素 AutomationElement aeDeskTop = AutomationElement.RootElement; Thread.Sleep(2000); AutomationElement aeForm = AutomationElement.FromHandle(p.MainWindowHandle); //获得对主窗体对象的引用,该对象实际上就是 Form1 应用程序(方法一) //if (null == aeForm) //{ // Console.WriteLine("Can not find the WinFormTest from."); //} //获得对主窗体对象的引用,该对象实际上就是 Form1 应用程序(方法二) int numWaits = 0; do { Console.WriteLine("Looking for WinFormTest……"); //查找第一个自动化元素 aeForm = aeDeskTop.FindFirst(TreeScope.Children, new PropertyCondition( AutomationElement.NameProperty, "Form1")); ++numWaits; Thread.Sleep(100); } while (null == aeForm && numWaits < 50); if (null == aeForm) throw new NullReferenceException("Failed to find WinFormTest."); else Console.WriteLine("Found it!"); Console.WriteLine("Finding all user controls"); //找到第一次出现的Button控件 AutomationElement aeButton = aeForm.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "button1")); //找到所有的TextBox控件 AutomationElementCollection aeAllTextBoxes = aeForm.FindAll(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit)); // 控件初始化的顺序是先初始化后添加到控件 // this.Controls.Add(this.textBox3); // this.Controls.Add(this.textBox2); // this.Controls.Add(this.textBox1); AutomationElement aeTextBox1 = aeAllTextBoxes[2]; AutomationElement aeTextBox2 = aeAllTextBoxes[1]; AutomationElement aeTextBox3 = aeAllTextBoxes[0]; Console.WriteLine("Settiing input to '30'"); //通过ValuePattern设置TextBox1的值 ValuePattern vpTextBox1 = (ValuePattern)aeTextBox1.GetCurrentPattern(ValuePattern.Pattern); vpTextBox1.SetValue("30"); Console.WriteLine("Settiing input to '50'"); //通过ValuePattern设置TextBox2的值 ValuePattern vpTextBox2 = (ValuePattern)aeTextBox2.GetCurrentPattern(ValuePattern.Pattern); vpTextBox2.SetValue("50"); Thread.Sleep(1500); Console.WriteLine("Clickinig on button1 Button."); //通过InvokePattern模拟点击按钮 InvokePattern ipClickButton1 = (InvokePattern)aeButton.GetCurrentPattern(InvokePattern.Pattern); ipClickButton1.Invoke(); Thread.Sleep(1500); //验证计算的结果与预期的结果是否相符合 Console.WriteLine("Checking textBox3 for '80'"); TextPattern tpTextBox3 = (TextPattern)aeTextBox3.GetCurrentPattern(TextPattern.Pattern); string result = tpTextBox3.DocumentRange.GetText(-1);//获取textbox3中的值 //获取textbox3中的值 //string result = (string)aeTextBox2.GetCurrentPropertyValue(ValuePattern.ValueProperty); if ("80" == result) { Console.WriteLine("Found it."); Console.WriteLine("TTest scenario: *PASS*"); } else { Console.WriteLine("Did not find it."); Console.WriteLine("Test scenario: *FAIL*"); } Console.WriteLine("Close application in 5 seconds."); Thread.Sleep(5000); //实现关闭被测试程序 WindowPattern wpCloseForm = (WindowPattern)aeForm.GetCurrentPattern(WindowPattern.Pattern); wpCloseForm.Close(); Console.WriteLine("/nEnd test run/n"); } catch (Exception ex) { Console.WriteLine("Fatal error: " + ex.Message); } } }
MS提供的控件Pattern
DockPattern ExpandCollapsePattern
GridPattern GridItemPattern
InvokePattern MultipleViewPattern
RangeValuePattern ScrollPattern
ScrollItemPattern SelectionPattern
SelectionItemPattern TablePattern
TableItemPattern TextPattern
TogglePattern TransformPattern
ValuePattern WindowPattern
相关文章推荐
- 使用 UI Automation 实现自动化测试--4.2
- 使用UI Automation实现自动化测试 -3
- 使用UI Automation实现自动化测试--4.4 (ValuePattern)
- 使用UI Automation实现自动化测试--4.6.1 (SelectionItemPattern)
- 使用UI Automation实现自动化测试--2
- 使用UI Automation实现自动化测试--6 (模拟鼠标在自动化测试中的应用)
- 【转】使用UI Automation实现自动化测试--2
- 使用UI Automation实现自动化测试--2
- 使用UI Automation实现自动化测试--4.8 (GridPattern)
- 使用UI Automation实现自动化测试 -1
- 使用UI Automation实现自动化测试--4.5 (WindowPattern)
- 使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)
- 使用UI Automation实现自动化测试--7.1 (模拟键盘输入数据在自动化测试中的应用)
- 【转】使用UI Automation实现自动化测试-1
- 【搜集】使用UI Automation实现自动化测试系列
- 使用 UI Automation 实现自动化测试--4.3
- 使用UI Automation实现自动化测试 -4.1
- 使用UI Automation实现自动化测试--7.2 (模拟键盘复杂操作在自动化测试中的应用)
- 使用 UI Automation 实现自动化测试--1
- 使用 UI Automation 实现自动化测试--4.4