UI Automation Test

UI Automation test is based on the windows API. U can find the UI Automation MSDN file from http://msdn.microsoft.com/en-us/library/ms753107.aspx link.

The important part is Using UI Automation fir Automated Testing.

As a new tester of UI Automation. I can give a demo that U can understand the UI automation better.

1st. Adding the refer reference. The reference U need add include(UIAutomation.dll, UIAutomationClientSideProvider.dll, UIAutomationTypes.dll). if U can not find these DLL U can find these in your computer C disc.

 

2nd. Add the using system.(System.Window.Automation).

 

3rd. Find the control. U must find the control which U using such as Button, textbox or label.

  var desktop = AutomationElement.RootElement; // find the root element, can be consider as desktop
  var condition = new PropertyCondition(AutomationElement.NameProperty, "test"); // define the string that should be find,name as "test"
  var window = desktop.FindFirst(TreeScope.Children, condition); //find the desktop child control which confirm the string control

UI Automation  need a tool to find the control property and the event. The tool is the UI Spy.

If U want managemet the more properties, U can use the AndContion object.  Such as click the "OK" button.

  var btnCondition = new AndCondition(
                new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Button),
                new PropertyCondition(AutomationElement.NameProperty, "ok"));

4th. How to trigger the control. Like Button click event, window drag event and so on. An easy example Button click:

  var button = window.FindFirst(TreeScope.Children, btnCondition);
  var clickPattern = (InvokePattern)button.GetCurrentPattern(InvokePattern.Pattern);
  clickPattern.Invoke();

 How to find the control contains which Patter?  See the UI Spy.

UI Automation Control Patterns Overview 

Summary:

  Read more MDSN file, test mare.

Next give a demo of UI Automation of Notepad Test.

 

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Windows.Automation;
  6 using System.Threading;
  7 using System.Diagnostics;
  8 using System.IO;
  9 using System.Windows.Automation.Text;
 10 
 11 namespace ConsoleApplication2
 12 {
 13     class Program
 14     {
 15         static void Main(string[] args)
 16         {
 17             #region step 0: create a txt file and name it with Demon.txt.
 18             if (!File.Exists("C:\\Users\\MBSUser\\Documents\\Visual Studio 2010\\Projects\\ConsoleApplication2\\Demo.txt"))
 19             {
 20                 FileStream fs = new FileStream("C:\\Users\\MBSUser\\Documents\\Visual Studio 2010\\Projects\\ConsoleApplication2\\ConsoleApplication2\\Demo.txt", FileMode.Create, FileAccess.Write);
 21                 StreamWriter sw = new StreamWriter(fs);
 22 
 23 
 24                 //FileStream fs = new FileStream(@"C:\Users\MBSUser\Documents\Visual Studio 2010\Projects\ConsoleApplication2", FileMode.Create, FileAccess.Write);
 25                 //StreamWriter sw = new StreamWriter(fs);
 26                 sw.BaseStream.Seek(0, SeekOrigin.Begin);
 27                 sw.WriteLine("This is just a txt demo, Test World!.");
 28                 sw.Close();
 29             }
 30             else
 31             {
 32                 FileStream fsexist = new FileStream("C:\\Users\\MBSUser\\Documents\\Visual Studio 2010\\Projects\\ConsoleApplication2\\ConsoleApplication2\\Demo.txt", FileMode.Open, FileAccess.Write);
 33                 StreamWriter swexist = new StreamWriter(fsexist);
 34                 swexist.WriteLine("This is just a txt demo, Test World!.");
 35                 swexist.Close();
 36                 fsexist.Close();
 37             }
 38             #endregion
 39 
 40 
 41             #region step 1: Open 1 txt file by notepad
 42             ProcessStartInfo psi = new ProcessStartInfo("C:\\Users\\MBSUser\\Documents\\Visual Studio 2010\\Projects\\ConsoleApplication2\\ConsoleApplication2\\Demo.txt");
 43             psi.WindowStyle = ProcessWindowStyle.Normal;
 44             psi.UseShellExecute = true;
 45 
 46             Console.WriteLine("1.open 1 txt file by notepad.");
 47             Process p = Process.Start(psi);
 48 
 49             Thread.Sleep(5000);
 50 
 51             IntPtr windowHandle = p.MainWindowHandle;
 52             AutomationElement notepad = AutomationElement.FromHandle(windowHandle);
 53             #endregion
 54 
 55             #region Step2: Click "Edit->Find" menu.
 56             // Step2-1: Expand Edit Menu.
 57             string editMenuAutomationId = "Item 2";
 58             Console.WriteLine("Step2: Click Find menu.");
 59             PropertyCondition propertyCondition = new PropertyCondition(
 60                 AutomationElement.AutomationIdProperty,
 61                 editMenuAutomationId);
 62             AutomationElement editMenu = notepad.FindFirst(TreeScope.Element | TreeScope.Descendants, propertyCondition);
 63 
 64             if (editMenu == null)
 65             {
 66                 Console.WriteLine(editMenuAutomationId.ToString() + " could not be found.");
 67                 return;
 68             }
 69             if ((bool)editMenu.GetCurrentPropertyValue(AutomationElement.IsEnabledProperty) == false)
 70             {
 71                 Console.WriteLine("Element not enabled.");
 72                 return;
 73             }
 74             ExpandCollapsePattern expandEdit = editMenu.GetCurrentPattern(ExpandCollapsePattern.Pattern)
 75                 as ExpandCollapsePattern;
 76             expandEdit.Expand();
 77             Console.WriteLine(editMenuAutomationId.ToString() + " expanded.");
 78 
 79             // Step2-2: Click Find menu item.
 80             string findMenuItemAutomationId = "Item 21";
 81             propertyCondition = new PropertyCondition(
 82                 AutomationElement.AutomationIdProperty,
 83                 findMenuItemAutomationId);
 84             AutomationElement findMenuItem = notepad.FindFirst(TreeScope.Element | TreeScope.Descendants, propertyCondition);
 85 
 86             if (findMenuItem == null)
 87             {
 88                 Console.WriteLine(findMenuItemAutomationId.ToString() + " could not be found.");
 89                 return;
 90             }
 91             if ((bool)findMenuItem.GetCurrentPropertyValue(AutomationElement.IsEnabledProperty) == false)
 92             {
 93                 Console.WriteLine("Element not enabled.");
 94                 return;
 95             }
 96             InvokePattern invokePattern = findMenuItem.GetCurrentPattern(InvokePattern.Pattern)
 97                 as InvokePattern;
 98             invokePattern.Invoke();
 99 
100             // Wait for Find dialog pop up. Can use verify Find dialog existing to avoid hardcode sleep time.
101             Thread.Sleep(1000);
102             Console.WriteLine(findMenuItemAutomationId.ToString() + " invoked.");
103             #endregion
104 
105             #region Step3: Set text: "World" in Find Dialog.
106             Console.WriteLine("Step3: Set text in Find Dialog.");
107             
108             string findTextAutomationId = "1152";
109             string findText = "World";
110             propertyCondition = new PropertyCondition(
111                 AutomationElement.AutomationIdProperty,
112                 findTextAutomationId);
113             AutomationElement findTextField = notepad.FindFirst(TreeScope.Element | TreeScope.Descendants, propertyCondition);
114             if (findTextField == null)
115             {
116                 Console.WriteLine(findTextAutomationId.ToString() + " could not be found.");
117                 return;
118             }
119             if ((bool)findTextField.GetCurrentPropertyValue(AutomationElement.IsEnabledProperty) == false)
120             {
121                 Console.WriteLine("Element not enabled.");
122                 return;
123             }
124             ValuePattern valuePattern =
125                 findTextField.GetCurrentPattern(ValuePattern.Pattern)
126                 as ValuePattern;
127             valuePattern.SetValue(findText);
128             Console.WriteLine(findTextAutomationId.ToString() + " value changed.");
129             #endregion
130 
131             #region Step4: Click "Find Next".
132             Console.WriteLine("Step3: Click Find Next button in Find Dialog.");
133             
134             string findNextAutomationId = "1";
135             propertyCondition = new PropertyCondition(
136                 AutomationElement.AutomationIdProperty,
137                 findNextAutomationId);
138             AutomationElement findNextButton = notepad.FindFirst(TreeScope.Element | TreeScope.Descendants, propertyCondition);
139 
140             if (findNextButton == null)
141             {
142                 Console.WriteLine(findNextAutomationId.ToString() + " could not be found.");
143                 return;
144             }
145             if ((bool)findNextButton.GetCurrentPropertyValue(AutomationElement.IsEnabledProperty) == false)
146             {
147                 Console.WriteLine("Element not enabled.");
148                 return;
149             }
150             invokePattern =
151                 findNextButton.GetCurrentPattern(InvokePattern.Pattern)
152                 as InvokePattern;
153             invokePattern.Invoke();
154             Console.WriteLine(findNextAutomationId.ToString() + " invoked.");
155             #endregion
156 
157             #region Step5: Click "Cancel" to close the Find dialog.
158             Console.WriteLine("Step3: Click Cancel button to close the Find dialog.");
159             
160             string cancelAutomationId = "2";
161             propertyCondition = new PropertyCondition(
162                 AutomationElement.AutomationIdProperty,
163                 cancelAutomationId);
164             AutomationElement cancelButton = notepad.FindFirst(TreeScope.Element | TreeScope.Descendants, propertyCondition);
165 
166             if (cancelButton == null)
167             {
168                 Console.WriteLine(cancelAutomationId.ToString() + " could not be found.");
169                 return;
170             }
171             if ((bool)cancelButton.GetCurrentPropertyValue(AutomationElement.IsEnabledProperty) == false)
172             {
173                 Console.WriteLine("Element not enabled.");
174                 return;
175             }
176             invokePattern =
177                 cancelButton.GetCurrentPattern(InvokePattern.Pattern)
178                 as InvokePattern;
179             invokePattern.Invoke();
180             Console.WriteLine(cancelAutomationId.ToString() + " invoked.");
181             #endregion
182 
183             #region Step6: Verify the string is found.
184             Console.WriteLine("Step6: Verify the string is found.");
185                         
186             string actualResult = "";
187             string expectedResult = findText;
188             string editAreaAutomationId = "15";
189             propertyCondition = new PropertyCondition(
190                 AutomationElement.AutomationIdProperty,
191                 editAreaAutomationId);
192             AutomationElement editArea = notepad.FindFirst(TreeScope.Element | TreeScope.Descendants, propertyCondition);
193 
194             if (editArea == null)
195             {
196                 Console.WriteLine(editAreaAutomationId.ToString() + " could not be found.");
197                 return;
198             }
199             if ((bool)editArea.GetCurrentPropertyValue(AutomationElement.IsEnabledProperty) == false)
200             {
201                 Console.WriteLine("Element not enabled.");
202                 return;
203             }
204             TextPattern textPattern =
205                 editArea.GetCurrentPattern(TextPattern.Pattern)
206                 as TextPattern;
207             actualResult = textPattern.DocumentRange.GetText(-1);
208             TextPatternRange[] searchRange = textPattern.GetSelection();
209             int startPoints = textPattern.DocumentRange.CompareEndpoints(
210                 TextPatternRangeEndpoint.Start,
211                 searchRange[0],
212                 TextPatternRangeEndpoint.Start);
213             int endPoints = textPattern.DocumentRange.CompareEndpoints(
214                 TextPatternRangeEndpoint.End,
215                 searchRange[0],
216                 TextPatternRangeEndpoint.End);
217             actualResult = actualResult.Substring(actualResult.Length + startPoints, -(startPoints + endPoints));
218 
219 
220             if (expectedResult == actualResult)
221             {
222                 Console.WriteLine("Pass. The expected string is found.");
223             }
224             else
225             {
226                 Console.WriteLine(String.Format("Fail. The expected string is {0}; the actual string is {1}", expectedResult, actualResult));
227             }
228 
229             #endregion
230 
231             #region Test Cleanup: Close notepad.
232             Console.WriteLine("Test Cleanup: Close notepad.");
233             p.CloseMainWindow();
234             #endregion
235         }
236     }
237 }

 

posted @ 2016-07-14 10:43  边缘者  阅读(454)  评论(0编辑  收藏  举报