-
Notifications
You must be signed in to change notification settings - Fork 476
Expand file tree
/
Copy pathSearchConditionFactory.cs
More file actions
142 lines (126 loc) · 7.19 KB
/
SearchConditionFactory.cs
File metadata and controls
142 lines (126 loc) · 7.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
using System;
using System.Linq;
using System.Windows.Automation;
using System.Xml;
using System.Xml.XPath;
using TestStack.White.Mappings;
using TestStack.White.UIItems.Custom;
using TestStack.White.UIItems.WindowItems;
namespace TestStack.White.UIItems.Finders
{
public static class SearchConditionFactory
{
public static SimpleSearchCondition CreateForControlType(ControlType controlType)
{
return new SimpleSearchCondition(automationElement => automationElement.Current.ControlType, new ControlTypeProperty(controlType, "ControlType"));
}
public static SearchCondition CreateForControlType(Type testControlType, WindowsFramework framework)
{
if (testControlType.IsCustomType())
return CreateForControlType(CustomControlTypeMapping.ControlType(testControlType, framework));
var controlTypes = ControlDictionary.Instance.GetControlType(testControlType, framework.FrameworkId());
if (controlTypes.Length == 1)
return CreateForControlType(controlTypes[0]);
return new OrSearchCondition(controlTypes.Select(CreateForControlType).ToArray());
}
/// <summary>
///
/// </summary>
/// <param name="frameworkId">List available from WindowsFramework class or Constants class</param>
/// <returns></returns>
public static SearchCondition CreateForFrameworkId(string frameworkId)
{
return new SimpleSearchCondition(automationElement => automationElement.Current.FrameworkId,
new AutomationElementProperty(frameworkId, "FrameworkId", AutomationElement.FrameworkIdProperty));
}
public static SearchCondition CreateForAutomationId(string id)
{
return new SimpleSearchCondition(automationElement => automationElement.Current.AutomationId,
new AutomationElementProperty(id, "AutomationId", AutomationElement.AutomationIdProperty));
}
/// <summary>
/// Getting the objects (AutomationElement) hierarchy of the given window
/// Create SimpleSearchCondition with AutomationId for given xpath
/// </summary>
/// <param name="xpath"></param>
/// <param name="window"></param>
/// <returns></returns>
public static SearchCondition CreateForXPath(string xpath, Window window)
{
XmlDocument xmlDoc = new XmlDocument();
string hierarchy = window.GetHierarchy(window.AutomationElement);
xmlDoc.LoadXml(hierarchy);
XmlNodeList nodeList = xmlDoc.SelectNodes(xpath);
if (nodeList.Count == 0)
{
throw new Exception("No element found for given XPath: " + xpath);
}
else if (nodeList.Count > 1)
{
throw new Exception("Multiple elements found for given XPath: " + xpath);
}
else
{
foreach (XmlNode node in nodeList)
{
var automationId = node.Attributes["AutomationId"].Value;
var className = node.Attributes["ClassName"].Value;
//var controlType = node.Attributes["ControlType"].Value;
var frameworkId = node.Attributes["FrameworkId"].Value;
var name = node.Attributes["Name"].Value;
var helpText = node.Attributes["HelpText"].Value;
if (!String.IsNullOrEmpty(name))
{
return new SimpleSearchCondition(automationElement => automationElement.Current.Name,
new AutomationElementProperty(name, "Name", AutomationElement.NameProperty));
}
else if (!String.IsNullOrEmpty(automationId))
{
return new SimpleSearchCondition(automationElement => automationElement.Current.AutomationId,
new AutomationElementProperty(automationId, "AutomationId", AutomationElement.AutomationIdProperty));
}
else if (!String.IsNullOrEmpty(helpText))
{
return new SimpleSearchCondition(automationElement => automationElement.Current.HelpText,
new AutomationElementProperty(helpText, "HelpText", AutomationElement.HelpTextProperty));
}
else if (!String.IsNullOrEmpty(className))
{
return new SimpleSearchCondition(automationElement => automationElement.Current.ClassName,
new AutomationElementProperty(className, "ClassName", AutomationElement.ClassNameProperty));
}
else if (!String.IsNullOrEmpty(frameworkId))
{
return new SimpleSearchCondition(automationElement => automationElement.Current.FrameworkId,
new AutomationElementProperty(frameworkId, "FrameworkId", AutomationElement.FrameworkIdProperty));
}
}
}
throw new Exception("Invalid Xpath");
}
public static SearchCondition CreateForName(string name)
{
return new SimpleSearchCondition(automationElement => automationElement.Current.Name, new AutomationElementProperty(name, "Name", AutomationElement.NameProperty));
}
public static SearchCondition CreateForClassName(string className)
{
return new SimpleSearchCondition(automationElement => automationElement.Current.ClassName,
new AutomationElementProperty(className, "ClassName", AutomationElement.ClassNameProperty));
}
public static SearchCondition CreateForNativeProperty(AutomationProperty automationProperty, string value)
{
var automationElementProperty = new AutomationElementProperty(value, automationProperty.ProgrammaticName, automationProperty);
return new SimpleSearchCondition(automationElement => automationElement.GetCurrentPropertyValue(automationProperty), automationElementProperty);
}
public static SearchCondition CreateForNativeProperty(AutomationProperty automationProperty, bool value)
{
var automationElementProperty = new AutomationElementProperty(value, automationProperty.ProgrammaticName, automationProperty);
return new SimpleSearchCondition(automationElement => automationElement.GetCurrentPropertyValue(automationProperty), automationElementProperty);
}
public static SearchCondition CreateForNativeProperty(AutomationProperty automationProperty, object value)
{
var automationElementProperty = new AutomationElementProperty(value, automationProperty.ProgrammaticName, automationProperty);
return new SimpleSearchCondition(automationElement => automationElement.GetCurrentPropertyValue(automationProperty), automationElementProperty);
}
}
}