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的方法:
1
using System;2
using System.Text;3
using System.Diagnostics;4
using System.Threading;5
using System.Windows.Automation;6

7
namespace UIATest8


{9
class Program10

{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 Test18
19
AutomationElement element = FindElementById(processId, "listview1");20
GridPattern pattern = GetGridPattern(element);21
//Get cell element which row and column is 122
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
#endregion30
}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
try42

{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
else57

{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 helper78

/**//// <summary>79
/// Get GridPattern80
/// </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
#endregion95
}96
}97

对应的XAML代码如下:
1
<Window x:Class="WpfApp.GridView"2
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"3
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"4
Title="GridView" Height="426" Width="558">5
<Grid>6
<!--将鼠标放在方框的边缘点击就会产生相应的分割线生成Grid.RowDefinitions-->7
<Grid.RowDefinitions>8
<!--Auto,实际作用就是取实际控件所需的最小值;值为*或N*,实际作用就是取尽可能大的值;数字,绝对尺寸-->9
<RowDefinition Height="*" />10
<RowDefinition Height="auto" MinHeight="95" />11
<RowDefinition Height="22" />12
</Grid.RowDefinitions>13
<ListView Name="listview1" MinWidth="280" Grid.RowSpan="2" MouseMove="listview1_MouseMove">14
<ListView.View>15
<GridView x:Name="gridView1">16
<GridViewColumn Header="EmployeeID" DisplayMemberBinding="{Binding Path=EmployeeID}"></GridViewColumn>17
<GridViewColumn Header="FirstName" DisplayMemberBinding="{Binding Path=FirstName}"></GridViewColumn>18
<GridViewColumn Header="LastName" DisplayMemberBinding="{Binding Path=LastName}"></GridViewColumn>19
<GridViewColumn Header="Address" DisplayMemberBinding="{Binding Path=Address}"></GridViewColumn>20
</GridView>21
</ListView.View>22
</ListView>23
</Grid>24

25
</Window>26

GridView窗体后台代码如下:
1
using System;2
using System.Collections.Generic;3
using System.Linq;4
using System.Text;5
using System.Windows;6
using System.Windows.Controls;7
using System.Windows.Data;8
using System.Windows.Documents;9
using System.Windows.Input;10
using System.Windows.Media;11
using System.Windows.Media.Imaging;12
using System.Windows.Shapes;13
using System.Data.SqlClient;14
using System.Data;15

16
namespace WpfApp17


{18

/**//// <summary>19
/// Interaction logic for GridView.xaml20
/// </summary>21
public partial class GridView : Window22

{23
public GridView()24

{25
InitializeComponent();26
getData();27
}28
SqlDataAdapter sda;29
DataTable dt;30
void getData()31

{32
//Northwind database download path:http://download.csdn.net/down/845087/beyondchina12333
//init sqlconnection34
SqlConnectionStringBuilder connbuilder = new SqlConnectionStringBuilder();35
connbuilder.DataSource = ".";//本地服务器36
connbuilder.IntegratedSecurity = true;//Windows集成验证37
connbuilder.InitialCatalog = "Northwind";//数据库为Northwind38
SqlConnection conn = new SqlConnection(connbuilder.ConnectionString);39
sda = new SqlDataAdapter("select EmployeeID,FirstName,LastName,Address from Employees ", conn);40
SqlCommandBuilder commbuilder = new SqlCommandBuilder(sda);41
dt = new DataTable();42
sda.Fill(dt);43
listview1.ItemsSource = dt.DefaultView;44
}45
}46
}47

48

本文主要简单介绍了GridPattern以及GridPattern在测试中是使用方法。