/
WiniumElement.cs
166 lines (133 loc) · 4.85 KB
/
WiniumElement.cs
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
namespace Winium.StoreApps.CodedUITestProject
{
#region
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Automation;
using System.Xml.Linq;
using Microsoft.VisualStudio.TestTools.UITest.Input;
using Microsoft.VisualStudio.TestTools.UITesting;
using Winium.StoreApps.Common;
#endregion
public class WiniumElement
{
#region Fields
private readonly AutomationElement element;
#endregion
#region Constructors and Destructors
public WiniumElement(AutomationElement element)
{
this.element = element;
}
#endregion
#region Public Properties
public static WiniumElement RootElement
{
get
{
return new WiniumElement(AutomationElement.RootElement);
}
}
public AutomationElement AutomationElement
{
get
{
return this.element;
}
}
public string RuntimeId
{
get
{
return string.Join(string.Empty, this.AutomationElement.GetRuntimeId());
}
}
#endregion
#region Public Methods and Operators
public XStreamingElement AsXml()
{
return new XStreamingElement(
"W" + this.RuntimeId,
new XAttribute("class", this.AutomationElement.Current.ClassName),
new XAttribute("id", this.AutomationElement.Current.AutomationId),
new XAttribute("name", this.AutomationElement.Current.Name),
new XAttribute("rect", this.AutomationElement.Current.BoundingRectangle),
from x in this.IterFind(TreeScope.Children, null) select x.AsXml());
}
public void Click()
{
Gesture.Tap(this.GetClickablePoint());
}
public string GetAttribute(string attributeName)
{
// TODO Add actual support for different attributes
return this.element.Current.AutomationId;
}
public Point GetClickablePoint()
{
try
{
return this.element.GetClickablePoint();
}
catch (NoClickablePointException)
{
// TODO this is temporary solution for WebView elements, that do not return clickable point even if visible
// FIXME middle point of BoundingRectangle might be under anither element or off screen, need some checks
var rect = this.AutomationElement.Current.BoundingRectangle;
return new Point(rect.Left + (rect.Width / 2), rect.Top + (rect.Height / 2));
}
}
public string GetText()
{
object patternObj;
if (this.element.TryGetCurrentPattern(ValuePattern.Pattern, out patternObj))
{
var valuePattern = (ValuePattern)patternObj;
return valuePattern.Current.Value;
}
if (this.element.TryGetCurrentPattern(TextPattern.Pattern, out patternObj))
{
var textPattern = (TextPattern)patternObj;
return textPattern.DocumentRange.GetText(-1);
}
return this.element.Current.Name;
}
public IEnumerable<WiniumElement> IterFind(TreeScope scope, Predicate<WiniumElement> predicate)
{
if (scope != TreeScope.Descendants && scope != TreeScope.Children)
{
throw new ArgumentException("scope should be one of TreeScope.Descendants or TreeScope.Children");
}
var walker = new TreeWalker(Condition.TrueCondition);
var elementNode = walker.GetFirstChild(this.AutomationElement);
while (elementNode != null)
{
var winiumElement = new WiniumElement(elementNode);
if (predicate == null || predicate(winiumElement))
{
yield return winiumElement;
}
if (scope == TreeScope.Descendants)
{
foreach (var descendant in winiumElement.IterFind(scope, predicate))
{
yield return descendant;
}
}
elementNode = walker.GetNextSibling(elementNode);
}
}
public void SendKeys(string value)
{
object patternObj;
if (!this.element.TryGetCurrentPattern(ValuePattern.Pattern, out patternObj))
{
throw new AutomationException("Element does not support sending keys.");
}
var valuePattern = (ValuePattern)patternObj;
valuePattern.SetValue(value);
}
#endregion
}
}