GridPattern
支持GridPattern的最常见的控件为GridView, 在WPF中使用ListView和GridView组合即可得到相应的GridView。
GridPattern的方法GetItem:此方法有两个参数,即DataGrid的Row和Column。
通过GridPattern的GetItem方法可以获取DataGrid中的某个确定的单元格,进而对单元进行操作。
对单元格的操作主要有以下几个方面:
1.编辑单元个中的数据。
2.获取单元格中的数据。
3.获取单元格中嵌套的AutomationElement(一般使用与自定义控件中)。
GridPattern的属性GridPattern的Current属性中有如下两个属性:
1.RowCount属性:GridPattern二维表格的行数。
2.ColumnCount属性:GridPattern二维表格列数。
下面我们通过一个实例来演示自动化测试中如何使用GridPattern来测试GridView的方法:
Code
1using System; 2using System.Text; 3using System.Diagnostics; 4using System.Threading; 5using System.Windows.Automation; 6 7namespace UIATest 8{ 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 Process process = Process.Start(@"F:\CSharpDotNet\AutomationTest\ATP\WpfApp\bin\Debug\WpfApp.exe"); 14 int processId = process.Id; 15 Thread.Sleep(1000); 16 17 GridPattern Test#region GridPattern Test 18 19 AutomationElement element = FindElementById(processId, "listview1"); 20 GridPattern pattern = GetGridPattern(element); 21 //Get cell element which row and column is 1 22 AutomationElement tempElement = pattern.GetItem(1, 1); 23 24 Console.WriteLine("Cell which row = '{0}', column = '{1}', cell value is '{2}'", 25 1, 1, tempElement.Current.Name); 26 Console.WriteLine("Grid row count = '{0}', column count = '{1}'", 27 pattern.Current.RowCount, pattern.Current.ColumnCount); 28 29 #endregion 30 } 31 32 /**//// <summary> 33 /// Get the automation elemention of current form. 34 /// </summary> 35 /// <param name="processId">Process Id</param> 36 /// <returns>Target element</returns> 37 public static AutomationElement FindWindowByProcessId(int processId) 38 { 39 AutomationElement targetWindow = null; 40 int count = 0; 41 try 42 { 43 Process p = Process.GetProcessById(processId); 44 targetWindow = AutomationElement.FromHandle(p.MainWindowHandle); 45 return targetWindow; 46 } 47 catch (Exception ex) 48 { 49 count++; 50 StringBuilder sb = new StringBuilder(); 51 string message = sb.AppendLine(string.Format("Target window is not existing.try #{0}", count)).ToString(); 52 if (count > 5) 53 { 54 throw new InvalidProgramException(message, ex); 55 } 56 else 57 { 58 return FindWindowByProcessId(processId); 59 } 60 } 61 } 62 63 /**//// <summary> 64 /// Get the automation element by automation Id. 65 /// </summary> 66 /// <param name="windowName">Window name</param> 67 /// <param name="automationId">Control automation Id</param> 68 /// <returns>Automatin element searched by automation Id</returns> 69 public static AutomationElement FindElementById(int processId, string automationId) 70 { 71 AutomationElement aeForm = FindWindowByProcessId(processId); 72 AutomationElement tarFindElement = aeForm.FindFirst(TreeScope.Descendants, 73 new PropertyCondition(AutomationElement.AutomationIdProperty, automationId)); 74 return tarFindElement; 75 } 76 77 GridPattern helper#region GridPattern helper 78 /**//// <summary> 79 /// Get GridPattern 80 /// </summary> 81 /// <param name="element">AutomationElement instance</param> 82 /// <returns>GridPattern instance</returns> 83 public static GridPattern GetGridPattern(AutomationElement element) 84 { 85 object currentPattern; 86 if (!element.TryGetCurrentPattern(GridPattern.Pattern, out currentPattern)) 87 { 88 throw new Exception(string.Format("Element with AutomationId '{0}' and Name '{1}' does not support the GridPattern.", 89 element.Current.AutomationId, element.Current.Name)); 90 } 91 return currentPattern as GridPattern; 92 } 93 94 #endregion 95 } 96} 97 |