-
Notifications
You must be signed in to change notification settings - Fork 4
/
selenium-extend.js
222 lines (152 loc) · 4.81 KB
/
selenium-extend.js
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
var webdriver = require('selenium-webdriver');
var by = require('selenium-webdriver').By;
var until = require('selenium-webdriver').until;
var chrome = require('selenium-webdriver/chrome');
module.exports = {
createChrome: createChrome,
createChromeWithExtension: createChromeWithExtension,
addExtend: applyWrapper,
setWaitTime: setWaitTime
};
var WAITTIME = 10000; // default wait time
function setWaitTime(time) {
WAITTIME = time;
}
function createChrome(driverPath) {
var o = new chrome.Options();
var s = new chrome.ServiceBuilder(driverPath).build();
var driver = chrome.createDriver(o, s);
applyWrapper(driver);
return driver;
}
function createChromeWithExtension(extensionPath, driverPath) {
var o = new chrome.Options();
o.addArguments("load-extension="+extensionPath);
o.addArguments("--extensions-on-chrome-urls");
var s = new chrome.ServiceBuilder(driverPath).build();
var driver = chrome.createDriver(o, s);
driver.manage().window().setSize(1268, 648);
applyWrapper(driver);
return driver;
}
function applyWrapper(driver) {
// Helper
function waitForElement(css) {
driver.wait(until.elementLocated(by.css(css)), WAITTIME);
}
driver.extend = {};
var extend = driver.extend;
// Methods
extend.enableIncognito = function enableIncognito(numberOfExtensions) {
// first extension is always selenium
// assume desire for 2 extensions if numberOfExtensions is undefined
// (Selenium and a user defined extension)
if (numberOfExtensions === undefined) {
numberOfExtensions = 2;
}
var extensionsURL = "chrome://extensions-frame"; //used to avoid iframe
driver.get(extensionsURL);
extend.isClickable("div#extension-settings-list div:nth-child(1) label.incognito-control input");
for (var i = 0; i < numberOfExtensions; i++) {
var num = i + 1;
extend.click("div#extension-settings-list div.extension-list-item-wrapper:nth-of-type(" + num + ") label.incognito-control input");
}
};
extend.isClickable = function waitForClickableElement(css) {
driver.wait(until.elementLocated(by.css(css)), WAITTIME);
var element = driver.findElement(by.css(css));
driver.wait(function () {
return element.isDisplayed().then(function (displayed) {
if (!displayed)
return false;
return element.isEnabled();
});
}, WAITTIME);
};
extend.click = function waitAndClick(css) {
extend.isClickable(css);
driver
.findElement(by.css(css))
.click();
};
extend.doubleClick = function waitAndDoubleClick(css) {
extend.isClickable(css);
var element = driver.findElement(by.css(css))
driver
.actions()
.doubleClick(element)
.perform();
};
extend.exists = function waitAndExists(css) {
waitForElement(css);
// returns promise
return driver
.findElement(by.css(css))
.isEnabled();
};
extend.getText = function waitAndGetText(css) {
waitForElement(css);
return driver
.findElement(by.css(css))
.getText();
};
extend.getValue = function waitAndGetValue(css) {
waitForElement(css);
return driver
.findElement(by.css(css))
.getAttribute("value");
};
extend.selectText = function selectText(css, offset1, offset2){
waitForElement(css);
var element = driver.findElement(by.css(css));
//start drag event
driver
.actions()
.mouseMove(element, 0, 0)
.mouseDown()
.mouseMove({x:5, y:0})
.perform();
// inject selection
var script = [
"var range = document.createRange();",
"var textNode = document.querySelector('" + css + "').firstChild;",
"var selection = window.getSelection();",
"range.setStart(textNode, " + offset1 + ");",
"range.setEnd(textNode, " + offset2 + ");",
"selection.removeAllRanges();",
"selection.addRange(range);"
].join('\n');
driver.executeScript(script);
driver
.actions()
.mouseUp()
.perform();
};
extend.typeText = function waitAndType(css, text){
extend.isClickable(css);
return driver
.findElement(by.css(css))
.sendKeys(text);
};
extend.toNextWindow = function switchToNextWindow() {
driver.getWindowHandle().then(function(currenthandle){
driver.getAllWindowHandles().then(function(handles){
for (id in handles) {
var handle = handles[id];
if (handle != currenthandle) {
driver.switchTo().window(handle);
break;
}
}
});
});
};
extend.moveToElement = function moveToElement(css) {
waitForElement(css);
var script = [
'var element = document.querySelector("' + css + '");',
"element.scrollIntoView();"
].join('\n');
driver.executeScript(script);
};
}