SelectionItemPattern
支援SelectionItemPattern的控件有ListView、ListBox、RadioButton、GridView等。
1. SelectionItemPattern的三個重要方法:
1. AddToSelection:将目前元素添加到所選項的集合。
2. RemoveFromSelection: 從標明項的集合中移除目前元素。
3. Select: 取消所有已選中的項,然後選擇目前元素。
2. SelectionItemPattern的Current屬性
可通過Current屬性的IsSelected屬性來判斷AutomationElement是否被selected.
如下代碼示範了使用SelectionItemPattern來操作RadioButton控件。
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) Code
1
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) using System;
2
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) using System.Text;
3
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) using System.Diagnostics;
4
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) using System.Threading;
5
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) using System.Windows.Automation;
6
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) 7
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) namespace UIATest
8
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) {
9
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) class Program
10
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern)
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern)
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) 11
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) static void Main(string[] args)
12
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern)
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern)
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) 13
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) Process process = Process.Start(@"F:\CSharpDotNet\AutomationTest\ATP\WpfApp\bin\Debug\WpfApp.exe");
14
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) int processId = process.Id;
15
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) 16
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) AutomationElement element = FindElementById(processId, "radioButton1");
17
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) SelectionItemPattern selectionItemPattern = GetSelectionItemPattern(element);
18
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) selectionItemPattern.Select();
19
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) }
20
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) 21
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern)
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) /**//// <summary>
22
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) /// Get the automation elemention of current form.
23
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) /// </summary>
24
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) /// <param name="processId">Process Id</param>
25
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) /// <returns>Target element</returns>
26
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) public static AutomationElement FindWindowByProcessId(int processId)
27
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern)
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern)
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) 28
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) AutomationElement targetWindow = null;
29
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) int count = 0;
30
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) try
31
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern)
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern)
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) 32
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) Process p = Process.GetProcessById(processId);
33
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) targetWindow = AutomationElement.FromHandle(p.MainWindowHandle);
34
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) return targetWindow;
35
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) }
36
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) catch (Exception ex)
37
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern)
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern)
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) 38
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) count++;
39
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) StringBuilder sb = new StringBuilder();
40
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) string message = sb.AppendLine(string.Format("Target window is not existing.try #{0}", count)).ToString();
41
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) if (count > 5)
42
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern)
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern)
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) 43
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) throw new InvalidProgramException(message, ex);
44
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) }
45
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) else
46
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern)
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern)
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) 47
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) return FindWindowByProcessId(processId);
48
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) 49
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) 50
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) 51
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) 52
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) 53
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern)
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) 54
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) /// Get the automation element by automation Id.
55
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) 56
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) /// <param name="windowName">Window name</param>
57
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) /// <param name="automationId">Control automation Id</param>
58
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) /// <returns>Automatin element searched by automation Id</returns>
59
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) public static AutomationElement FindElementById(int processId, string automationId)
60
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern)
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern)
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) 61
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) AutomationElement aeForm = FindWindowByProcessId(processId);
62
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) AutomationElement tarFindElement = aeForm.FindFirst(TreeScope.Descendants,
63
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) new PropertyCondition(AutomationElement.AutomationIdProperty, automationId));
64
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) return tarFindElement;
65
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) 66
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) 67
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern)
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) SelectItemPattern#region SelectItemPattern
68
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) 69
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern)
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) 70
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) /// Get SelectItemPattern
71
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) 72
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) /// <param name="element">AutomationElement instance</param>
73
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) /// <returns>SelectItemPattern instance</returns>
74
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) public static SelectionItemPattern GetSelectionItemPattern(AutomationElement element)
75
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern)
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern)
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) 76
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) object currentPattern;
77
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) if (!element.TryGetCurrentPattern(SelectionItemPattern.Pattern, out currentPattern))
78
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern)
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern)
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) 79
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) throw new Exception(string.Format("Element with AutomationId '{0}' and Name '{1}' does not support the SelectionItemPattern.",
80
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) element.Current.AutomationId, element.Current.Name));
81
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) 82
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) return currentPattern as SelectionItemPattern;
83
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) 84
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) 85
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) #endregion
86
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) }
87
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) }
88
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) 以下代碼為XAML:
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern)
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) 1
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) <Window x:Class="WpfApp.Window1"
2
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) Title="Window1" Height="219" Width="353">
5
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) <Grid>
6
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) <RadioButton Height="16" HorizontalAlignment="Right" Margin="0,46,10,0" Name="radioButton1" VerticalAlignment="Top" Width="120">RadioButton</RadioButton>
7
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) </Grid>
8
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern) </Window>
9
使用UI Automation實作自動化測試--4.6.1 (SelectionItemPattern)