使用UI Automation实现自动化测试--4.7 (TogglePattern)
TogglePattern
支持TogglePattern的控件有CheckBox,TreeView中的button控件等。
1. TogglePattern的方法
Toggle方法用于操作可以循环通过的一组状态并在设置后保持某种状态。
2. TogglePattern属性
Current属性中的ToggleState有如下三种状态:
1. On
2. Off
3. Indeterminate
如下代码演示了使用TogglePattern来操作CheckBox控件。
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
16 Thread.Sleep(1000);
17 AutomationElement element = FindElementById(processId, "checkBox1");
18 TogglePattern togglePattern = GetTogglePattern(element);
19 togglePattern.Toggle();
20 }
21
22 /**//// <summary>
23 /// Get the automation elemention of current form.
24 /// </summary>
25 /// <param name="processId">Process Id</param>
26 /// <returns>Target element</returns>
27 public static AutomationElement FindWindowByProcessId(int processId)
28 {
29 AutomationElement targetWindow = null;
30 int count = 0;
31 try
32 {
33 Process p = Process.GetProcessById(processId);
34 targetWindow = AutomationElement.FromHandle(p.MainWindowHandle);
35 return targetWindow;
36 }
37 catch (Exception ex)
38 {
39 count++;
40 StringBuilder sb = new StringBuilder();
41 string message = sb.AppendLine(string.Format("Target window is not existing.try #{0}", count)).ToString();
42 if (count > 5)
43 {
44 throw new InvalidProgramException(message, ex);
45 }
46 else
47 {
48 return FindWindowByProcessId(processId);
49 }
50 }
51 }
52
53 /**//// <summary>
54 /// Get the automation element by automation Id.
55 /// </summary>
56 /// <param name="windowName">Window name</param>
57 /// <param name="automationId">Control automation Id</param>
58 /// <returns>Automatin element searched by automation Id</returns>
59 public static AutomationElement FindElementById(int processId, string automationId)
60 {
61 AutomationElement aeForm = FindWindowByProcessId(processId);
62 AutomationElement tarFindElement = aeForm.FindFirst(TreeScope.Descendants,
63 new PropertyCondition(AutomationElement.AutomationIdProperty, automationId));
64 return tarFindElement;
65 }
66
67 TogglePattern helper#region TogglePattern helper
68
69 /**//// <param name="element">AutomationElement instance</param>
70 /// <returns>TogglePattern instance</returns>
71 public static TogglePattern GetTogglePattern(AutomationElement element)
72 {
73 object currentPattern;
74 if (!element.TryGetCurrentPattern(TogglePattern.Pattern, out currentPattern))
75 {
76 throw new Exception(string.Format("Element with AutomationId '{0}' and Name '{1}' does not support the TogglePattern.",
77 element.Current.AutomationId, element.Current.Name));
78 }
79 return currentPattern as TogglePattern;
80 }
81
82 #endregion
83 }
84}
85
如下代码为对应的XAML: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
16 Thread.Sleep(1000);
17 AutomationElement element = FindElementById(processId, "checkBox1");
18 TogglePattern togglePattern = GetTogglePattern(element);
19 togglePattern.Toggle();
20 }
21
22 /**//// <summary>
23 /// Get the automation elemention of current form.
24 /// </summary>
25 /// <param name="processId">Process Id</param>
26 /// <returns>Target element</returns>
27 public static AutomationElement FindWindowByProcessId(int processId)
28 {
29 AutomationElement targetWindow = null;
30 int count = 0;
31 try
32 {
33 Process p = Process.GetProcessById(processId);
34 targetWindow = AutomationElement.FromHandle(p.MainWindowHandle);
35 return targetWindow;
36 }
37 catch (Exception ex)
38 {
39 count++;
40 StringBuilder sb = new StringBuilder();
41 string message = sb.AppendLine(string.Format("Target window is not existing.try #{0}", count)).ToString();
42 if (count > 5)
43 {
44 throw new InvalidProgramException(message, ex);
45 }
46 else
47 {
48 return FindWindowByProcessId(processId);
49 }
50 }
51 }
52
53 /**//// <summary>
54 /// Get the automation element by automation Id.
55 /// </summary>
56 /// <param name="windowName">Window name</param>
57 /// <param name="automationId">Control automation Id</param>
58 /// <returns>Automatin element searched by automation Id</returns>
59 public static AutomationElement FindElementById(int processId, string automationId)
60 {
61 AutomationElement aeForm = FindWindowByProcessId(processId);
62 AutomationElement tarFindElement = aeForm.FindFirst(TreeScope.Descendants,
63 new PropertyCondition(AutomationElement.AutomationIdProperty, automationId));
64 return tarFindElement;
65 }
66
67 TogglePattern helper#region TogglePattern helper
68
69 /**//// <param name="element">AutomationElement instance</param>
70 /// <returns>TogglePattern instance</returns>
71 public static TogglePattern GetTogglePattern(AutomationElement element)
72 {
73 object currentPattern;
74 if (!element.TryGetCurrentPattern(TogglePattern.Pattern, out currentPattern))
75 {
76 throw new Exception(string.Format("Element with AutomationId '{0}' and Name '{1}' does not support the TogglePattern.",
77 element.Current.AutomationId, element.Current.Name));
78 }
79 return currentPattern as TogglePattern;
80 }
81
82 #endregion
83 }
84}
85
Code
1<Window x:Class="WpfApp.Window1"
2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4 Title="Window1" Height="219" Width="353">
5 <Grid>
6 <CheckBox HorizontalAlignment="Right" Margin="0,75,10,89" Name="checkBox1" Width="120">CheckBox</CheckBox>
7 </Grid>
8</Window>
9
1<Window x:Class="WpfApp.Window1"
2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4 Title="Window1" Height="219" Width="353">
5 <Grid>
6 <CheckBox HorizontalAlignment="Right" Margin="0,75,10,89" Name="checkBox1" Width="120">CheckBox</CheckBox>
7 </Grid>
8</Window>
9
本文主要简单介绍了TogglePattern以及使用TogglePattern操作CheckBox的方法。