TogglePattern

支持TogglePattern的控件有CheckBoxTreeView中的button控件等。

1.       TogglePattern的方法

Toggle方法用于操作可以循环通过的一组状态并在设置后保持某种状态。

2.       TogglePattern属性

Current属性中的ToggleState有如下三种状态:

1.         On

2.         Off

3.         Indeterminate

    如下代码演示了使用TogglePattern来操作CheckBox控件。


使用UI Automation实现自动化测试--4.7 (TogglePattern)_microsoft使用UI Automation实现自动化测试--4.7 (TogglePattern)_xml_02Code
 1使用UI Automation实现自动化测试--4.7 (TogglePattern)_IT_03using System;
 2使用UI Automation实现自动化测试--4.7 (TogglePattern)_IT_03using System.Text;
 3使用UI Automation实现自动化测试--4.7 (TogglePattern)_IT_03using System.Diagnostics;
 4使用UI Automation实现自动化测试--4.7 (TogglePattern)_IT_03using System.Threading;
 5使用UI Automation实现自动化测试--4.7 (TogglePattern)_IT_03using System.Windows.Automation;
 6使用UI Automation实现自动化测试--4.7 (TogglePattern)_IT_03
 7使用UI Automation实现自动化测试--4.7 (TogglePattern)_IT_03namespace UIATest
 8使用UI Automation实现自动化测试--4.7 (TogglePattern)_控件_10使用UI Automation实现自动化测试--4.7 (TogglePattern)_UI Automation_11使用UI Automation实现自动化测试--4.7 (TogglePattern)_microsoft_12{
 9使用UI Automation实现自动化测试--4.7 (TogglePattern)_microsoft_13    class Program
10使用UI Automation实现自动化测试--4.7 (TogglePattern)_xml_14使用UI Automation实现自动化测试--4.7 (TogglePattern)_xml_15    使用UI Automation实现自动化测试--4.7 (TogglePattern)_microsoft_12{
11使用UI Automation实现自动化测试--4.7 (TogglePattern)_microsoft_13        static void Main(string[] args)
12使用UI Automation实现自动化测试--4.7 (TogglePattern)_xml_14使用UI Automation实现自动化测试--4.7 (TogglePattern)_xml_15        使用UI Automation实现自动化测试--4.7 (TogglePattern)_microsoft_12{
13使用UI Automation实现自动化测试--4.7 (TogglePattern)_microsoft_13            Process process = Process.Start(@"F:\CSharpDotNet\AutomationTest\ATP\WpfApp\bin\Debug\WpfApp.exe");
14使用UI Automation实现自动化测试--4.7 (TogglePattern)_microsoft_13            int processId = process.Id;
15使用UI Automation实现自动化测试--4.7 (TogglePattern)_microsoft_13
16使用UI Automation实现自动化测试--4.7 (TogglePattern)_microsoft_13            Thread.Sleep(1000);
17使用UI Automation实现自动化测试--4.7 (TogglePattern)_microsoft_13            AutomationElement element = FindElementById(processId, "checkBox1");
18使用UI Automation实现自动化测试--4.7 (TogglePattern)_microsoft_13            TogglePattern togglePattern = GetTogglePattern(element);
19使用UI Automation实现自动化测试--4.7 (TogglePattern)_microsoft_13            togglePattern.Toggle();
20使用UI Automation实现自动化测试--4.7 (TogglePattern)_控件_28        }

21使用UI Automation实现自动化测试--4.7 (TogglePattern)_microsoft_13
22使用UI Automation实现自动化测试--4.7 (TogglePattern)_xml_14使用UI Automation实现自动化测试--4.7 (TogglePattern)_xml_15        /**//// <summary>
23使用UI Automation实现自动化测试--4.7 (TogglePattern)_microsoft_13        /// Get the automation elemention of current form.
24使用UI Automation实现自动化测试--4.7 (TogglePattern)_microsoft_13        /// </summary>
25使用UI Automation实现自动化测试--4.7 (TogglePattern)_microsoft_13        /// <param name="processId">Process Id</param>
26使用UI Automation实现自动化测试--4.7 (TogglePattern)_控件_28        /// <returns>Target element</returns>

27使用UI Automation实现自动化测试--4.7 (TogglePattern)_microsoft_13        public static AutomationElement FindWindowByProcessId(int processId)
28使用UI Automation实现自动化测试--4.7 (TogglePattern)_xml_14使用UI Automation实现自动化测试--4.7 (TogglePattern)_xml_15        使用UI Automation实现自动化测试--4.7 (TogglePattern)_microsoft_12{
29使用UI Automation实现自动化测试--4.7 (TogglePattern)_microsoft_13            AutomationElement targetWindow = null;
30使用UI Automation实现自动化测试--4.7 (TogglePattern)_microsoft_13            int count = 0;
31使用UI Automation实现自动化测试--4.7 (TogglePattern)_microsoft_13            try
32使用UI Automation实现自动化测试--4.7 (TogglePattern)_xml_14使用UI Automation实现自动化测试--4.7 (TogglePattern)_xml_15            使用UI Automation实现自动化测试--4.7 (TogglePattern)_microsoft_12{
33使用UI Automation实现自动化测试--4.7 (TogglePattern)_microsoft_13                Process p = Process.GetProcessById(processId);
34使用UI Automation实现自动化测试--4.7 (TogglePattern)_microsoft_13                targetWindow = AutomationElement.FromHandle(p.MainWindowHandle);
35使用UI Automation实现自动化测试--4.7 (TogglePattern)_microsoft_13                return targetWindow;
36使用UI Automation实现自动化测试--4.7 (TogglePattern)_控件_28            }

37使用UI Automation实现自动化测试--4.7 (TogglePattern)_microsoft_13            catch (Exception ex)
38使用UI Automation实现自动化测试--4.7 (TogglePattern)_xml_14使用UI Automation实现自动化测试--4.7 (TogglePattern)_xml_15            使用UI Automation实现自动化测试--4.7 (TogglePattern)_microsoft_12{
39使用UI Automation实现自动化测试--4.7 (TogglePattern)_microsoft_13                count++;
40使用UI Automation实现自动化测试--4.7 (TogglePattern)_microsoft_13                StringBuilder sb = new StringBuilder();
41使用UI Automation实现自动化测试--4.7 (TogglePattern)_microsoft_13                string message = sb.AppendLine(string.Format("Target window is not existing.try #{0}", count)).ToString();
42使用UI Automation实现自动化测试--4.7 (TogglePattern)_microsoft_13                if (count > 5)
43使用UI Automation实现自动化测试--4.7 (TogglePattern)_xml_14使用UI Automation实现自动化测试--4.7 (TogglePattern)_xml_15                使用UI Automation实现自动化测试--4.7 (TogglePattern)_microsoft_12{
44使用UI Automation实现自动化测试--4.7 (TogglePattern)_microsoft_13                    throw new InvalidProgramException(message, ex);
45使用UI Automation实现自动化测试--4.7 (TogglePattern)_控件_28                }

46使用UI Automation实现自动化测试--4.7 (TogglePattern)_microsoft_13                else
47使用UI Automation实现自动化测试--4.7 (TogglePattern)_xml_14使用UI Automation实现自动化测试--4.7 (TogglePattern)_xml_15                使用UI Automation实现自动化测试--4.7 (TogglePattern)_microsoft_12{
48使用UI Automation实现自动化测试--4.7 (TogglePattern)_microsoft_13                    return FindWindowByProcessId(processId);
49使用UI Automation实现自动化测试--4.7 (TogglePattern)_控件_28                }

50使用UI Automation实现自动化测试--4.7 (TogglePattern)_控件_28            }

51使用UI Automation实现自动化测试--4.7 (TogglePattern)_控件_28        }

52使用UI Automation实现自动化测试--4.7 (TogglePattern)_microsoft_13
53使用UI Automation实现自动化测试--4.7 (TogglePattern)_xml_14使用UI Automation实现自动化测试--4.7 (TogglePattern)_xml_15        /**//// <summary>
54使用UI Automation实现自动化测试--4.7 (TogglePattern)_microsoft_13        /// Get the automation element by automation Id.
55使用UI Automation实现自动化测试--4.7 (TogglePattern)_microsoft_13        /// </summary>
56使用UI Automation实现自动化测试--4.7 (TogglePattern)_microsoft_13        /// <param name="windowName">Window name</param>
57使用UI Automation实现自动化测试--4.7 (TogglePattern)_microsoft_13        /// <param name="automationId">Control automation Id</param>
58使用UI Automation实现自动化测试--4.7 (TogglePattern)_控件_28        /// <returns>Automatin element searched by automation Id</returns>

59使用UI Automation实现自动化测试--4.7 (TogglePattern)_microsoft_13        public static AutomationElement FindElementById(int processId, string automationId)
60使用UI Automation实现自动化测试--4.7 (TogglePattern)_xml_14使用UI Automation实现自动化测试--4.7 (TogglePattern)_xml_15        使用UI Automation实现自动化测试--4.7 (TogglePattern)_microsoft_12{
61使用UI Automation实现自动化测试--4.7 (TogglePattern)_microsoft_13            AutomationElement aeForm = FindWindowByProcessId(processId);
62使用UI Automation实现自动化测试--4.7 (TogglePattern)_microsoft_13            AutomationElement tarFindElement = aeForm.FindFirst(TreeScope.Descendants,
63使用UI Automation实现自动化测试--4.7 (TogglePattern)_microsoft_13            new PropertyCondition(AutomationElement.AutomationIdProperty, automationId));
64使用UI Automation实现自动化测试--4.7 (TogglePattern)_microsoft_13            return tarFindElement;
65使用UI Automation实现自动化测试--4.7 (TogglePattern)_控件_28        }

66使用UI Automation实现自动化测试--4.7 (TogglePattern)_microsoft_13
67使用UI Automation实现自动化测试--4.7 (TogglePattern)_xml_15使用UI Automation实现自动化测试--4.7 (TogglePattern)_xml_14        TogglePattern helper#region TogglePattern helper
68使用UI Automation实现自动化测试--4.7 (TogglePattern)_microsoft_13
69使用UI Automation实现自动化测试--4.7 (TogglePattern)_xml_14使用UI Automation实现自动化测试--4.7 (TogglePattern)_xml_15        /**//// <param name="element">AutomationElement instance</param>
70使用UI Automation实现自动化测试--4.7 (TogglePattern)_控件_28        /// <returns>TogglePattern instance</returns>

71使用UI Automation实现自动化测试--4.7 (TogglePattern)_microsoft_13        public static TogglePattern GetTogglePattern(AutomationElement element)
72使用UI Automation实现自动化测试--4.7 (TogglePattern)_xml_14使用UI Automation实现自动化测试--4.7 (TogglePattern)_xml_15        使用UI Automation实现自动化测试--4.7 (TogglePattern)_microsoft_12{
73使用UI Automation实现自动化测试--4.7 (TogglePattern)_microsoft_13            object currentPattern;
74使用UI Automation实现自动化测试--4.7 (TogglePattern)_microsoft_13            if (!element.TryGetCurrentPattern(TogglePattern.Pattern, out currentPattern))
75使用UI Automation实现自动化测试--4.7 (TogglePattern)_xml_14使用UI Automation实现自动化测试--4.7 (TogglePattern)_xml_15            使用UI Automation实现自动化测试--4.7 (TogglePattern)_microsoft_12{
76使用UI Automation实现自动化测试--4.7 (TogglePattern)_microsoft_13                throw new Exception(string.Format("Element with AutomationId '{0}' and Name '{1}' does not support the TogglePattern.",
77使用UI Automation实现自动化测试--4.7 (TogglePattern)_microsoft_13                    element.Current.AutomationId, element.Current.Name));
78使用UI Automation实现自动化测试--4.7 (TogglePattern)_控件_28            }

79使用UI Automation实现自动化测试--4.7 (TogglePattern)_microsoft_13            return currentPattern as TogglePattern;
80使用UI Automation实现自动化测试--4.7 (TogglePattern)_控件_28        }

81使用UI Automation实现自动化测试--4.7 (TogglePattern)_microsoft_13
82使用UI Automation实现自动化测试--4.7 (TogglePattern)_控件_28        #endregion

83使用UI Automation实现自动化测试--4.7 (TogglePattern)_控件_28    }

84使用UI Automation实现自动化测试--4.7 (TogglePattern)_UI Automation_112}

85使用UI Automation实现自动化测试--4.7 (TogglePattern)_IT_03
     如下代码为对应的XAML
使用UI Automation实现自动化测试--4.7 (TogglePattern)_microsoft使用UI Automation实现自动化测试--4.7 (TogglePattern)_xml_02Code
1使用UI Automation实现自动化测试--4.7 (TogglePattern)_IT_03<Window x:Class="WpfApp.Window1"
2使用UI Automation实现自动化测试--4.7 (TogglePattern)_IT_03    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3使用UI Automation实现自动化测试--4.7 (TogglePattern)_IT_03    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4使用UI Automation实现自动化测试--4.7 (TogglePattern)_IT_03    Title="Window1" Height="219" Width="353">
5使用UI Automation实现自动化测试--4.7 (TogglePattern)_IT_03    <Grid>
6使用UI Automation实现自动化测试--4.7 (TogglePattern)_IT_03        <CheckBox HorizontalAlignment="Right" Margin="0,75,10,89" Name="checkBox1" Width="120">CheckBox</CheckBox>
7使用UI Automation实现自动化测试--4.7 (TogglePattern)_IT_03    </Grid>
8使用UI Automation实现自动化测试--4.7 (TogglePattern)_IT_03</Window>
9使用UI Automation实现自动化测试--4.7 (TogglePattern)_IT_03

     本文主要简单介绍了TogglePattern以及使用TogglePattern操作CheckBox的方法。