使用UI Automation实现自动化测试--4.6.1
来源:互联网 发布:职场社交软件 编辑:程序博客网 时间:2024/06/06 04:32
SelectionItemPattern
支持SelectionItemPattern的控件有ListView、ListBox、RadioButton、GridView等。
1. SelectionItemPattern的三个重要方法:
1. AddToSelection:将当前元素添加到所选项的集合。
2. RemoveFromSelection: 从选定项的集合中移除当前元素。
3. Select: 取消所有已选中的项,然后选择当前元素。
2. SelectionItemPattern的Current属性
可通过Current属性的IsSelected属性来判断AutomationElement是否被selected.
如下代码演示了使用SelectionItemPattern来操作RadioButton控件。复制代码以下代码为XAML:复制代码本文简单介绍了SelectionItemPattern以及使用SelectionItemPattern来操作RadioButton。(文/开着拖拉机)
支持SelectionItemPattern的控件有ListView、ListBox、RadioButton、GridView等。
1. SelectionItemPattern的三个重要方法:
1. AddToSelection:将当前元素添加到所选项的集合。
2. RemoveFromSelection: 从选定项的集合中移除当前元素。
3. Select: 取消所有已选中的项,然后选择当前元素。
2. SelectionItemPattern的Current属性
可通过Current属性的IsSelected属性来判断AutomationElement是否被selected.
如下代码演示了使用SelectionItemPattern来操作RadioButton控件。
- 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 AutomationElement element = FindElementById(processId, "radioButton1");
- 17 SelectionItemPattern selectionItemPattern = GetSelectionItemPattern(element);
- 18 selectionItemPattern.Select();
- 19 }
- 20
- 21 /**//// <summary>
- 22 /// Get the automation elemention of current form.
- 23 /// </summary>
- 24 /// <param name="processId">Process Id</param>
- 25 /// <returns>Target element</returns>
- 26 public static AutomationElement FindWindowByProcessId(int processId)
- 27 {
- 28 AutomationElement targetWindow = null;
- 29 int count = 0;
- 30 try
- 31 {
- 32 Process p = Process.GetProcessById(processId);
- 33 targetWindow = AutomationElement.FromHandle(p.MainWindowHandle);
- 34 return targetWindow;
- 35 }
- 36 catch (Exception ex)
- 37 {
- 38 count++;
- 39 StringBuilder sb = new StringBuilder();
- 40 string message = sb.AppendLine(string.Format("Target window is not existing.try #{0}", count)).ToString();
- 41 if (count > 5)
- 42 {
- 43 throw new InvalidProgramException(message, ex);
- 44 }
- 45 else
- 46 {
- 47 return FindWindowByProcessId(processId);
- 48 }
- 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 SelectItemPattern#region SelectItemPattern
- 68
- 69 /**//// <summary>
- 70 /// Get SelectItemPattern
- 71 /// </summary>
- 72 /// <param name="element">AutomationElement instance</param>
- 73 /// <returns>SelectItemPattern instance</returns>
- 74 public static SelectionItemPattern GetSelectionItemPattern(AutomationElement element)
- 75 {
- 76 object currentPattern;
- 77 if (!element.TryGetCurrentPattern(SelectionItemPattern.Pattern, out currentPattern))
- 78 {
- 79 throw new Exception(string.Format("Element with AutomationId '{0}' and Name '{1}' does not support the SelectionItemPattern.",
- 80 element.Current.AutomationId, element.Current.Name));
- 81 }
- 82 return currentPattern as SelectionItemPattern;
- 83 }
- 84
- 85 #endregion
- 86 }
- 87}
- 88
- 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 <RadioButton Height="16" HorizontalAlignment="Right" Margin="0,46,10,0" Name="radioButton1" VerticalAlignment="Top" Width="120">RadioButton</RadioButton>
- 7 </Grid>
- 8</Window>
- 9
- 使用UI Automation实现自动化测试 -1
- 使用 UI Automation 实现自动化测试--1
- 使用UI Automation实现自动化测试--4.6.1
- 使用UI Automation实现自动化测试 -2
- 使用UI Automation实现自动化测试 -3
- 使用UI Automation实现自动化测试 -4.1
- 使用 UI Automation 实现自动化测试--2
- 使用 UI Automation 实现自动化测试--3
- 使用 UI Automation 实现自动化测试--4.1
- 使用 UI Automation 实现自动化测试--4.2
- 使用 UI Automation 实现自动化测试--4.3
- 使用 UI Automation 实现自动化测试--4.4
- 使用 UI Automation 实现自动化测试--4.5
- 使用 UI Automation 实现自动化测试--4.5
- 使用 UI Automation 实现自动化测试--4.4
- 使用 UI Automation 实现自动化测试--4.3
- 使用 UI Automation 实现自动化测试--4.2
- 使用 UI Automation 实现自动化测试--4.1
- 使用 UI Automation 实现自动化测试--4.4
- ActionBar-Three Dot
- Flash网站怎样做SEO优化,Flash怎样优化?
- 使用 UI Automation 实现自动化测试--4.5
- 关于视图控制器的初始化 viewController
- 使用UI Automation实现自动化测试--4.6.1
- 《partner4java 讲述jBPM4》之第一步:jBPM4概念 & helloworld
- MySQL修改默认字符集
- js回调函数、父画面与子画面之间的js回调函数(含参数传递)
- 使用UI Automation实现自动化测试--4.6.2
- yuv420_to_nv21
- 查询数据库当前连接数(session),进程数等操作
- 卸载jdk并重装后NetBeans启动时提示您使用默认Java_home设置的解决办法
- windows server 2008 R1/R2添加无线网络