使用UI Automation实现自动化测试--2

 

      本文通过一个实例来介绍怎样使用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

 


ContractedBlock.gif ExpandedBlockStart.gif Code
  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
  9ExpandedBlockStart.gifContractedBlock.gif{
 10    class Program
 11ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 12        static void Main(string[] args)
 13ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 14            try
 15ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 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
 42ExpandedSubBlockStart.gifContractedSubBlock.gif                {
 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)
 95ExpandedSubBlockStart.gifContractedSubBlock.gif                {
 96                    Console.WriteLine("Found it.");
 97                    Console.WriteLine("TTest scenario: *PASS*");
 98                }

 99                else
100ExpandedSubBlockStart.gifContractedSubBlock.gif                {
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)
114ExpandedSubBlockStart.gifContractedSubBlock.gif            {
115                Console.WriteLine("Fatal error: " + ex.Message);
116            }

117        }

118    }

119}

120

DockPattern                                 ExpandCollapsePattern

GridPattern                                  GridItemPattern

InvokePattern                              MultipleViewPattern

RangeValuePattern                      ScrollPattern

ScrollItemPattern                        SelectionPattern

SelectionItemPattern                   TablePattern

TableItemPattern                        TextPattern

TogglePattern                             TransformPattern

ValuePattern                               WindowPattern


 

 

转载于:https://www.cnblogs.com/kangyi/archive/2009/05/22/1487140.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值