-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebDriverExtensions.cs
More file actions
341 lines (309 loc) · 12.8 KB
/
WebDriverExtensions.cs
File metadata and controls
341 lines (309 loc) · 12.8 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Threading;
using OpenQA.Selenium;
using OpenQA.Selenium.Interactions;
using OpenQA.Selenium.Internal;
namespace ProtoTest.Specter
{
public static class WebDriverExtensions
{
public static string originalElementBorder;
public static void ShowOverlay(this IWebDriver driver)
{
string overlayCode =
"var overlay = document.createElement(\"div\");overlay.setAttribute(\"id\",\"overlay\");overlay.setAttribute(\"class\", \"overlay\");overlay.setAttribute(\"style\",\"background-color: #000;opacity:.7;filter: alpha(opacity=70);position: absolute; top: 0; left: 0;width: 100%; height: 100%;z-index: 10;\");document.body.appendChild(overlay);";
driver.ExecuteJavascript(overlayCode);
}
public static void DisableOverlay(this IWebDriver driver)
{
string overlayCode = "document.body.removeChild(document.getElementById(\"overlay\"));";
driver.ExecuteJavascript(overlayCode);
}
public static void MouseOver(this IWebElement element)
{
IWebDriver driver = ((IWrapsDriver) element).WrappedDriver;
var action = new Actions(driver);
action.MoveToElement(element).Build().Perform();
}
public static void ExecuteCommandByString(this IWebDriver driver, string command, string value)
{
switch (command)
{
case "Navigate":
driver.Navigate().GoToUrl(value);
break;
case "Count":
Program.Log("Found " + driver.FindElements(By.XPath(value)).Count + " elements using xpath : " +
value);
break;
default:
break;
}
}
public static void ExecuteCommandByString(this IWebElement element, string command, string text)
{
switch (command)
{
case "Click":
element.Flash();
element.Click();
break;
case "Find":
element.Flash();
break;
case "MouseOver":
element.Flash();
element.MouseOver();
break;
case "Type":
element.Flash();
element.SendKeys(text);
break;
case "Count":
IWebDriver driver = ((IWrapsDriver) element).WrappedDriver;
ReadOnlyCollection<IWebElement> elements =
driver.FindElements(By.XPath(Program.specter.currentXpath));
Program.Log(string.Format("Found {0} elements with xpath {1}", elements.Count,
Program.specter.currentXpath));
break;
default:
element.Flash();
break;
}
}
public static void RegisterClickEvent(this IWebDriver driver)
{
driver.ExecuteJavascript("var elementTarget;");
string listener =
"document.addEventListener('click', function(e) { e = e || window.event; window.elementTarget=e.target;}, false); return;";
driver.ExecuteJavascript(listener);
}
public static void RegisterHighlightOnMouseOver(this IWebDriver driver)
{
string var = "var lastBorder;";
string mouseOverListener =
"document.addEventListener('mouseover', function(e) { e = e || window.event; window.lastBorder=e.target.style.border; e.target.style.border='3px solid blue'}, false); return;";
string mouseOutListener =
"document.addEventListener('mouseout', function(e) { e = e || window.event; e.target.style.border=''}, false); return;";
//string touchMoveListener = "document.addEventListener('touchmove', function(e) { e = e || window.event; e.target.style.border=''}, false); return;";
driver.ExecuteJavascript(var);
driver.ExecuteJavascript(mouseOverListener);
driver.ExecuteJavascript(mouseOutListener);
}
public static void DisableMouseOver(this IWebDriver driver)
{
string all =
"document.mouseover = function() { return false; };document.mouseout = function() { return false; };return;";
driver.ExecuteJavascript(all);
}
public static void DisableClicks(this IWebDriver driver)
{
driver.ExecuteJavascript("var elementTarget;");
//string links = "var anchors = document.getElementsByTagName(\"a\"); for (var i = 0; i < anchors.length; i++) { anchors[i].href = '';}; return;";
string all = "document.onclick = function() { return false; };";
driver.ExecuteJavascript(all);
}
public static void DisableDTVEPanel(this IWebDriver driver)
{
driver.ExecuteJavascript(
"document.getElementById(\"topnav-mobile-account-menu\").style.visibility='hidden';return;");
}
public static object ExecuteJavascript(this IWebDriver driver, string js)
{
try
{
var jsDriver = ((IJavaScriptExecutor) driver);
return jsDriver.ExecuteScript(js);
}
catch (Exception err)
{
Program.Error("Error executing javascript : " + js + err.Message);
return null;
}
}
public static object ExecuteJavascript(this IWebDriver driver, string js, params object[] args)
{
try
{
var jsDriver = ((IJavaScriptExecutor) driver);
return jsDriver.ExecuteScript(js, args);
}
catch (Exception err)
{
Program.Error("Error executing javascript : " + js + err.Message);
return null;
}
}
public static void RegisterRightClickEvent(this IWebDriver driver)
{
driver.ExecuteJavascript("var elementTarget;");
string longPress =
"function onLongPress(node) {node.ontouchstart = nullEvent;node.ontouchend = nullEvent;node.ontouchcancel = nullEvent;node.ontouchmove = nullEvent;} function nullEvent(event) { var e = event || window.event; window.elementTarget=e.target; e.preventDefault && e.preventDefault(); e.stopPropagation && e.stopPropagation(); e.cancelBubble = true; e.returnValue = false; return false;}onLongPress(document); return;";
string listener =
"if (document.addEventListener) {document.addEventListener('contextmenu', function(e) {e = e || window.event; window.elementTarget=e.target;e.preventDefault();}, false);} else {document.attachEvent('oncontextmenu', function() {e = e || window.event; window.elementTarget=e.target;window.event.returnValue = false;});} return;";
driver.ExecuteJavascript(listener);
driver.ExecuteJavascript(longPress);
}
public static void SetClickedElement(this IWebDriver driver, IWebElement element)
{
driver.ExecuteJavascript("window.elementTarget=arguments[0]", element);
}
public static IWebElement GetElementClicked(this IWebDriver driver)
{
try
{
var jsDriver = ((IJavaScriptExecutor) driver);
return (IWebElement) jsDriver.ExecuteScript("return window.elementTarget");
}
catch (Exception err)
{
Program.Error("Could not get element : " + err.Message);
return driver.FindElement(By.XPath("//html"));
}
}
public static void Highlight(this IWebElement element)
{
try
{
IWebDriver driver = ((IWrapsDriver) element).WrappedDriver;
var jsDriver = ((IJavaScriptExecutor) driver);
originalElementBorder = (string) jsDriver.ExecuteScript("return arguments[0].style.border", element);
jsDriver.ExecuteScript("arguments[0].style.border='5px solid red'", element);
}
catch (Exception)
{
Program.Error("Could not highlight element");
}
}
public static void UnHighlight(this IWebElement element)
{
try
{
IWebDriver driver = ((IWrapsDriver) element).WrappedDriver;
var jsDriver = ((IJavaScriptExecutor) driver);
jsDriver.ExecuteScript("arguments[0].style.border='" + originalElementBorder + "'", element);
}
catch (Exception)
{
Program.Error("Could not unhighlight element");
}
}
public static void Flash(this IWebElement element)
{
element.Highlight();
Thread.Sleep(1000);
element.UnHighlight();
}
public static string GetHtml(this IWebElement element)
{
try
{
IWebDriver driver = ((IWrapsDriver) element).WrappedDriver;
var html =
(string)
((IJavaScriptExecutor) driver).ExecuteScript(
"var f = document.createElement('div').appendChild(arguments[0].cloneNode(true)); return f.parentNode.innerHTML",
element);
return html;
}
catch (Exception)
{
Program.Error("COuld not get html");
return "";
}
}
public static IWebElement GetParent(this IWebElement element)
{
try
{
IWebDriver driver = ((IWrapsDriver) element).WrappedDriver;
return
(IWebElement)
((IJavaScriptExecutor) driver).ExecuteScript(@"return arguments[0].parentNode;", element);
}
catch (Exception)
{
Program.Error("Could not get parent");
return element;
}
}
public static IWebElement FindSibling(this IWebElement element, By by)
{
return element.GetParent().FindElement(by);
}
public static void BG_Get(this IWebDriver driver, string urlString)
{
try
{
if (!urlString.Contains("http"))
urlString = "http://" + urlString;
driver.Navigate().GoToUrl(urlString);
}
catch (Exception err)
{
Program.Log("Could not navigate to url : " + urlString + err.Message);
}
}
public static IWebElement GetAncestor(this IWebElement element, IWebElement relative)
{
IWebElement ancestor = relative;
while (element.Text == "")
{
element = element.GetParent();
}
while (ancestor.TagName != "body" && ancestor.TagName != "html")
{
if (ancestor.Text.Contains(element.Text))
return ancestor;
ancestor = ancestor.GetParent();
}
return null;
}
public static List<IWebElement> GetSiblingElements(this IWebElement element)
{
var webelements = new List<IWebElement>();
string html = element.GetHtml();
IWebElement parent = element.GetParent();
ReadOnlyCollection<IWebElement> elements = parent.FindElements(By.XPath("child::*"));
foreach (IWebElement ele in elements)
{
if (ele.GetHtml() != html)
webelements.Add(ele);
}
return webelements;
}
public static IWebElement GetSibling(this IWebElement element)
{
return element.FindElement(By.XPath("following-sibling::*"));
}
public static IWebElement GetChild(this IWebElement element)
{
return element.FindElement(By.XPath("child::*"));
}
public static bool IsWebDriverConnected(this IWebDriver driver)
{
try
{
string source = driver.PageSource;
return true;
}
catch (Exception)
{
return false;
}
}
public static bool IsStale(this IWebElement element)
{
try
{
return !element.Enabled;
}
catch (Exception)
{
return true;
}
}
}
}