使用UI Automation实现自动化测试(4.7)——TogglePattern

发表于:2009-11-13 13:46

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:开着拖拉机    来源:51Testing软件测试网采编

  TogglePattern

  支持TogglePattern的控件有CheckBox,TreeView中的button控件等。

  1.TogglePattern的方法

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

  2.TogglePattern属性

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

  1.On

  2.Off

  3.Indeterminate

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


 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         #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:

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的方法。

相关阅读:

使用UI Automation实现自动化测试(4.6.2)——SelectItemPattern Demo

使用UI Automation实现自动化测试(4.6.1)——SelectionItemPattern

使用UI Automation实现自动化测试(4.5)——WindowPattern

使用UI Automation实现自动化测试(4.4)—— ValuePattern

使用UI Automation实现自动化测试(4.3)——InvokePattern

使用UI Automation实现自动化测试(4.2)——ExpandCollapsePattern

使用UI Automation实现自动化测试(4.1)——DockPattern

《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号