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

本文介绍UIAutomation中ValuePattern的应用,包括其在Winform和WPF TextBox控件中的作用及其实现准则。通过示例代码展示了如何使用ValuePattern来设置和获取TextBox的值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ValuePattern

ValuePatternUI Automation中最常见的Pattern之一,WinformWPFTextBox控件都支持ValuePattern

ValuePattern的一个重要的方法是SetValue,在允许调用 SetValue 之前,控件应将其 IsEnabledProperty 设置为 true 并将其 IsReadOnlyProperty 设置为 false

通过ValuePatternCurrent属性可以获得控件的valueIsReadOnly属性。

实现 Value 控件模式时,请注意以下准则和约定:

如果任何项的值是可编辑的,则诸如 ListItem TreeItem 等控件必须支持 ValuePattern,而不管控件的当前编辑模式如何。如果子项是可编辑的,则父控件还必须支持 ValuePattern

    下面的例子是通过ValuePattern来给TextBox设置和获取值:

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            AutomationElement element = FindElementById(processId, "textBox1");
16            ValuePattern currentPattern = GetValuePattern(element);
17            Console.WriteLine("Is read only:'{0}', TextBox text is:'{1}'", currentPattern.Current.IsReadOnly, currentPattern.Current.Value);
18            currentPattern.SetValue("KadenKang");
19            Console.WriteLine("After using the SetValue, the TextBox value is '{0}'", currentPattern.Current.Value);
20            
21        }

22
23ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
24        /// Get the automation elemention of current form.
25        /// </summary>
26        /// <param name="processId">Process Id</param>
27        /// <returns>Target element</returns>

28        public static AutomationElement FindWindowByProcessId(int processId)
29ExpandedSubBlockStart.gifContractedSubBlock.gif        {
30            AutomationElement targetWindow = null;
31            int count = 0;
32            try
33ExpandedSubBlockStart.gifContractedSubBlock.gif            {
34                Process p = Process.GetProcessById(processId);
35                targetWindow = AutomationElement.FromHandle(p.MainWindowHandle);
36                return targetWindow;
37            }

38            catch (Exception ex)
39ExpandedSubBlockStart.gifContractedSubBlock.gif            {
40                count++;
41                StringBuilder sb = new StringBuilder();
42                string message = sb.AppendLine(string.Format("Target window is not existing.try #{0}", count)).ToString();
43                if (count > 5)
44ExpandedSubBlockStart.gifContractedSubBlock.gif                {
45                    throw new InvalidProgramException(message, ex);
46                }

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

51            }

52        }

53
54ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
55        /// Get the automation element by automation Id.
56        /// </summary>
57        /// <param name="windowName">Window name</param>
58        /// <param name="automationId">Control automation Id</param>
59        /// <returns>Automatin element searched by automation Id</returns>

60        public static AutomationElement FindElementById(int processId, string automationId)
61ExpandedSubBlockStart.gifContractedSubBlock.gif        {
62            AutomationElement aeForm = FindWindowByProcessId(processId);
63            AutomationElement tarFindElement = aeForm.FindFirst(TreeScope.Descendants,
64            new PropertyCondition(AutomationElement.AutomationIdProperty, automationId));
65            return tarFindElement;
66        }

67
68ContractedSubBlock.gifExpandedSubBlockStart.gif        ValuePattern helper#region ValuePattern helper
69
70ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
71        /// To get the ValuePattern
72        /// </summary>
73        /// <param name="element">Target element</param>
74        /// <returns>ValuePattern instance</returns>

75        public static ValuePattern GetValuePattern(AutomationElement element)
76ExpandedSubBlockStart.gifContractedSubBlock.gif        {
77            object currentPattern;
78            if (!element.TryGetCurrentPattern(ValuePattern.Pattern, out currentPattern))
79ExpandedSubBlockStart.gifContractedSubBlock.gif            {
80                throw new Exception(string.Format("Element with AutomationId '{0}' and Name '{1}' does not support the ValuePattern.",
81                    element.Current.AutomationId, element.Current.Name));
82            }

83            return currentPattern as ValuePattern;
84        }

85
86        #endregion

87    }

88}

89

下面的代码是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        <TextBox Height="23" Margin="50,20,160,0" Name="textBox1" VerticalAlignment="Top" MaxLength="5">textBox text</TextBox>
7    </Grid>
8</Window>
9

     本文通过简单的实例介绍了UI Automation中的ValuePattern及其使用方法。

转载于:https://www.cnblogs.com/kangyi/archive/2009/09/15/1566945.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值