Test Run:MS UI Automation Library
2010-05-23 12:21
489 查看
1. Refer to : http://msdn.microsoft.com/en-us/magazine/cc163288.aspx
2. Tools: MS Spy++
3. Key MS Library: UIAutomationClient.dll & UIAutomationTypes.dll
4. Required Preperation: Before debugging you should turn off the managed Debugging Assistant for the "NonComVisibleBaseClass" exception. In Visual Studio,
1. Navigate to Debug->Exceptions...
2. Expand "Managed Debugging Assistants"
3. Uncheck the NonComVisibleBaseClass Thrown option.
4. Click [Ok]
Sample Demo codes by myself as follows:
-----------------------Form1.cs (server role) ----------------------------------------------
------------------------AutoTestForm1.cs---------------------------------------------
2. Tools: MS Spy++
3. Key MS Library: UIAutomationClient.dll & UIAutomationTypes.dll
4. Required Preperation: Before debugging you should turn off the managed Debugging Assistant for the "NonComVisibleBaseClass" exception. In Visual Studio,
1. Navigate to Debug->Exceptions...
2. Expand "Managed Debugging Assistants"
3. Uncheck the NonComVisibleBaseClass Thrown option.
4. Click [Ok]
Sample Demo codes by myself as follows:
-----------------------Form1.cs (server role) ----------------------------------------------
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace StrartCalcu { public partial class Form1 : Form { public Form1() { InitializeComponent(); this.button1.Click+=new EventHandler(Click_CalculateBtn); } protected void Click_CalculateBtn(Object sender, EventArgs e) { String inputTxt = this.textBox1.Text; String[] strArray = null; // char[] sep = { ' ' }; if (inputTxt == "" || inputTxt == null) { return; } else { strArray = inputTxt.Split(' '); double pValue1 = Double.Parse(strArray[0]); double pValue2 = Double.Parse(strArray[1]); if (radioButton1.Checked) { //textResult.Text="btn1"; // add textResult.Text = (pValue1 + pValue2).ToString(); } if (radioButton2.Checked) { // textResult.Text = "btn2"; //sub textResult.Text = (pValue1 - pValue2).ToString(); } if (radioButton3.Checked) { //textResult.Text="btn3"; // multi textResult.Text = (pValue1 * pValue2).ToString(); } } } private void testBtn_Click(object sender, EventArgs e) { this.Close(); } } }
------------------------AutoTestForm1.cs---------------------------------------------
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Automation; using System.Diagnostics; using System.Threading; namespace AutoTestForm { class Program { static void Main(string[] args) { try { Console.WriteLine("/nBegin WPF UIAutomation test run/n"); // launch StatCalc application Process p = Process.Start("E://VS2008Works//StrartCalcu//StrartCalcu//bin//Debug//StrartCalcu.exe"); //fetch a reference to the host machine's Desktop window as an AutomationElement: AutomationElement aeDesktop = AutomationElement.RootElement; // get a reference to the main Form object System.Console.WriteLine("Start looking for Form1"); AutomationElement aeForm = null; int numWaits = 0; do{ aeForm=aeDesktop.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "Form1")); numWaits++; Thread.Sleep(200); }while(numWaits<30&&aeForm==null); if (aeForm == null) System.Console.WriteLine("Fail to find Form1"); else System.Console.WriteLine("Form1 find!"); // get references to user controls System.Console.Out.WriteLine("Finding all user controls"); //AutomationElement aeTxtBox1=aeForm.FindFirst(TreeScope.Children,new PropertyCondition(AutomationElement.NameProperty, "")); AutomationElementCollection txtBoxs = aeForm.FindAll(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit)); AutomationElement aeTxtResult = txtBoxs[0]; AutomationElement aeTxtInput = txtBoxs[1]; AutomationElement aeBtnClose = aeForm.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "Close")); AutomationElement aeBtnCalcu = aeForm.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "Calculate")); AutomationElement aeMeansGroup=aeForm.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty,"meansBox")); AutomationElementCollection aeRadioBtns = aeMeansGroup.FindAll(TreeScope.Subtree, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.RadioButton)); Console.WriteLine("/n Setting input to '12 34'"); ValuePattern vpTxtInput = (ValuePattern)aeTxtInput.GetCurrentPattern(ValuePattern.Pattern); vpTxtInput.SetValue("12 34"); // manipulate application Console.WriteLine("Select all three means circally "); for (int i = 2; i >-1; i--) { SelectionItemPattern ipSelRadioBtn1 = (SelectionItemPattern)aeRadioBtns[i].GetCurrentPattern(SelectionItemPattern.Pattern); ipSelRadioBtn1.Select(); Thread.Sleep(3000); Console.WriteLine("Clicking on Calculate button"); InvokePattern ipClickCalcuBtn = (InvokePattern)aeBtnCalcu.GetCurrentPattern(InvokePattern.Pattern); ipClickCalcuBtn.Invoke(); Thread.Sleep(5000); // check resulting state and determine pass/fail Console.WriteLine("/n Checking resultTxt "); TextPattern tpTxtResult = (TextPattern)aeTxtResult.GetCurrentPattern(TextPattern.Pattern); string resultTxt = tpTxtResult.DocumentRange.GetText(-1); } InvokePattern ipClickClose = (InvokePattern)aeBtnClose.GetCurrentPattern(InvokePattern.Pattern); ipClickClose.Invoke(); Console.WriteLine("/nEnd test run/n"); Console.ReadKey(); } catch (Exception ex) { Console.WriteLine("Fatal error: " + ex.Message); } } } }
相关文章推荐
- MS UI Automation简介
- Use Python to run REST API Automation Test
- Coded UI Test在Windows Vista、2003和2008上需要Windows Automation API 3.0(转)
- MS UI Automation简介
- MS UI Automation简介
- Lightweight UI Test Automation with .NET
- VS2010 web UI automation test
- UI Automation Test
- week4、run 4 automation test cases of bright
- IOS自动化测试之UIAutomation Test学习——first step:Instrument
- MS UI Automation原来如此
- UI Automation test
- MS UI Automation Introduction
- Run Automation Test by MSTest.exe on 64bit OS
- Lightweight UI Test Automation for ASP.NET Web Apps
- MS UI Automation简介
- MS UI Automation
- UI automation test
- Coded UI Test在Windows Vista、2003和2008上需要Windows Automation API 3.0
- UI Automation Test(Winform,WPF)