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

TogglePattern

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

1.       TogglePattern的方法

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

2.       TogglePattern属性

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

1.         On

2.         Off

3.         Indeterminate

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


ContractedBlock.gif ExpandedBlockStart.gif Code
 1using System;
 2using System.Text;
 3using System.Diagnostics;
 4using System.Threading;
 5using System.Windows.Automation;
 6
 7namespace UIATest
 8ExpandedBlockStart.gifContractedBlock.gif{
 9    class Program
10ExpandedSubBlockStart.gifContractedSubBlock.gif    {
11        static void Main(string[] args)
12ExpandedSubBlockStart.gifContractedSubBlock.gif        {
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
22ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <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)
28ExpandedSubBlockStart.gifContractedSubBlock.gif        {
29            AutomationElement targetWindow = null;
30            int count = 0;
31            try
32ExpandedSubBlockStart.gifContractedSubBlock.gif            {
33                Process p = Process.GetProcessById(processId);
34                targetWindow = AutomationElement.FromHandle(p.MainWindowHandle);
35                return targetWindow;
36            }

37            catch (Exception ex)
38ExpandedSubBlockStart.gifContractedSubBlock.gif            {
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)
43ExpandedSubBlockStart.gifContractedSubBlock.gif                {
44                    throw new InvalidProgramException(message, ex);
45                }

46                else
47ExpandedSubBlockStart.gifContractedSubBlock.gif                {
48                    return FindWindowByProcessId(processId);
49                }

50            }

51        }

52
53ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <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)
60ExpandedSubBlockStart.gifContractedSubBlock.gif        {
61            AutomationElement aeForm = FindWindowByProcessId(processId);
62            AutomationElement tarFindElement = aeForm.FindFirst(TreeScope.Descendants,
63            new PropertyCondition(AutomationElement.AutomationIdProperty, automationId));
64            return tarFindElement;
65        }

66
67ContractedSubBlock.gifExpandedSubBlockStart.gif        TogglePattern helper#region TogglePattern helper
68
69ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <param name="element">AutomationElement instance</param>
70        /// <returns>TogglePattern instance</returns>

71        public static TogglePattern GetTogglePattern(AutomationElement element)
72ExpandedSubBlockStart.gifContractedSubBlock.gif        {
73            object currentPattern;
74            if (!element.TryGetCurrentPattern(TogglePattern.Pattern, out currentPattern))
75ExpandedSubBlockStart.gifContractedSubBlock.gif            {
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
ContractedBlock.gif ExpandedBlockStart.gif 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

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

转载于:https://www.cnblogs.com/kangyi/archive/2009/09/20/UIAutomation.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值