1. 首先建立一个待测试的winform程序,即UI Automation的服务端。

下面是button事件处理程序。

 private void CalculateButton_Click(object sender, EventArgs e)
        {
            int num1 = 0;
            int num2 = 0;
            if (!int.TryParse(textBox1.Text.Trim(), out num1) || !int.TryParse(textBox2.Text.Trim(), out num2))
            {
                MessageBox.Show("Please input a correct number!");
                return;
            }
            textBox3.Text = (num1 + num2).ToString();
        }

 

  2. 建立一个测试程序,做UI Automaion的客户端。

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

public static void TestWinFrom(int num1, int num2)
        {
            string winfromPath = @"C:\Users\v-lix\Documents\Visual Studio 2010\Projects\UIAutomationDemo\WinformForTesting\bin\Debug\WinformForTesting.exe";
            Console.WriteLine("Begin WinForm UIAutomation test runn");
            Console.WriteLine("Launching WinFormTest application");
            Process p = Process.Start(winfromPath);
            Thread.Sleep(TimeSpan.FromSeconds(2));
            Console.WriteLine("Get desktop");
            var desktop = AutomationElement.RootElement;

            // Method 1 by windowhandle
            AutomationElement testForm = AutomationElement.FromHandle(p.MainWindowHandle);

            // Method 2 by name property
            //PropertyCondition proCon = new PropertyCondition(AutomationElement.NameProperty,"TestForm1");
            //testForm = desktop.FindFirst(TreeScope.Children,proCon);

            if (testForm == null)
            {
                Console.WriteLine("Could find testform!");
                return;
            }

            Console.WriteLine("Try to find the button control in testForm");
            AutomationElement button = testForm.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "Calculate"));

            Console.WriteLine("Try to find all the textbox in testform!");
            AutomationElementCollection aeAllTextBoxes = testForm.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'");
            //set textboxs' value by ValuePattern
            ValuePattern vpTextBox1 = (ValuePattern)aeTextBox1.GetCurrentPattern(ValuePattern.Pattern);
            vpTextBox1.SetValue(num1.ToString());
            ValuePattern vpTextBox2 = (ValuePattern)aeTextBox2.GetCurrentPattern(ValuePattern.Pattern);
            vpTextBox2.SetValue(num2.ToString());
            Thread.Sleep(150);
            Console.WriteLine("Clickinig on button1 Button.");
            InvokePattern ipButton = (InvokePattern)button.GetCurrentPattern(InvokePattern.Pattern);
            ipButton.Invoke();
            Thread.Sleep(1000);

            ValuePattern vpTextBox3 = (ValuePattern)aeTextBox3.GetCurrentPattern(ValuePattern.Pattern);
            if (int.Parse(vpTextBox3.Current.Value.Trim()) != (num1 + num2))
            {
                Console.WriteLine("Failed");
            }
            else
            {
                Console.WriteLine("Pass");
            }

            Thread.Sleep(5000);
            //close testform
            WindowPattern wpCloseForm = (WindowPattern)testForm.GetCurrentPattern(WindowPattern.Pattern);
            wpCloseForm.Close();
        }