capabilities.js
12 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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
'use strict';
/**
* @fileoverview Defines types related to describing the capabilities of a
* WebDriver session.
*/
const Symbols = require('./symbols');
/**
* Recognized browser names.
* @enum {string}
*/
const Browser = {
ANDROID: 'android',
CHROME: 'chrome',
EDGE: 'MicrosoftEdge',
FIREFOX: 'firefox',
IE: 'internet explorer',
INTERNET_EXPLORER: 'internet explorer',
IPAD: 'iPad',
IPHONE: 'iPhone',
OPERA: 'opera',
PHANTOM_JS: 'phantomjs',
SAFARI: 'safari',
HTMLUNIT: 'htmlunit'
};
/**
* Common Capability keys.
* @enum {string}
*/
const Capability = {
/**
* Indicates whether a driver should accept all SSL certs by default. This
* capability only applies when requesting a new session. To query whether
* a driver can handle insecure SSL certs, see {@link #SECURE_SSL}.
*/
ACCEPT_SSL_CERTS: 'acceptSslCerts',
/**
* The browser name. Common browser names are defined in the {@link Browser}
* enum.
*/
BROWSER_NAME: 'browserName',
/**
* Defines how elements should be scrolled into the viewport for interaction.
* This capability will be set to zero (0) if elements are aligned with the
* top of the viewport, or one (1) if aligned with the bottom. The default
* behavior is to align with the top of the viewport.
*/
ELEMENT_SCROLL_BEHAVIOR: 'elementScrollBehavior',
/**
* Whether the driver is capable of handling modal alerts (e.g. alert,
* confirm, prompt). To define how a driver <i>should</i> handle alerts,
* use {@link #UNEXPECTED_ALERT_BEHAVIOR}.
*/
HANDLES_ALERTS: 'handlesAlerts',
/**
* Key for the logging driver logging preferences.
*/
LOGGING_PREFS: 'loggingPrefs',
/**
* Whether this session generates native events when simulating user input.
*/
NATIVE_EVENTS: 'nativeEvents',
/**
* Describes the platform the browser is running on. Will be one of
* ANDROID, IOS, LINUX, MAC, UNIX, or WINDOWS. When <i>requesting</i> a
* session, ANY may be used to indicate no platform preference (this is
* semantically equivalent to omitting the platform capability).
*/
PLATFORM: 'platform',
/**
* Describes the proxy configuration to use for a new WebDriver session.
*/
PROXY: 'proxy',
/** Whether the driver supports changing the brower's orientation. */
ROTATABLE: 'rotatable',
/**
* Whether a driver is only capable of handling secure SSL certs. To request
* that a driver accept insecure SSL certs by default, use
* {@link #ACCEPT_SSL_CERTS}.
*/
SECURE_SSL: 'secureSsl',
/** Whether the driver supports manipulating the app cache. */
SUPPORTS_APPLICATION_CACHE: 'applicationCacheEnabled',
/** Whether the driver supports locating elements with CSS selectors. */
SUPPORTS_CSS_SELECTORS: 'cssSelectorsEnabled',
/** Whether the browser supports JavaScript. */
SUPPORTS_JAVASCRIPT: 'javascriptEnabled',
/** Whether the driver supports controlling the browser's location info. */
SUPPORTS_LOCATION_CONTEXT: 'locationContextEnabled',
/** Whether the driver supports taking screenshots. */
TAKES_SCREENSHOT: 'takesScreenshot',
/**
* Defines how the driver should handle unexpected alerts. The value should
* be one of "accept", "dismiss", or "ignore.
*/
UNEXPECTED_ALERT_BEHAVIOR: 'unexpectedAlertBehavior',
/** Defines the browser version. */
VERSION: 'version'
};
/**
* Describes how a proxy should be configured for a WebDriver session.
* @record
*/
function ProxyConfig() {}
/**
* The proxy type. Must be one of {"manual", "pac", "system"}.
* @type {string}
*/
ProxyConfig.prototype.proxyType;
/**
* URL for the PAC file to use. Only used if {@link #proxyType} is "pac".
* @type {(string|undefined)}
*/
ProxyConfig.prototype.proxyAutoconfigUrl;
/**
* The proxy host for FTP requests. Only used if {@link #proxyType} is "manual".
* @type {(string|undefined)}
*/
ProxyConfig.prototype.ftpProxy;
/**
* The proxy host for HTTP requests. Only used if {@link #proxyType} is
* "manual".
* @type {(string|undefined)}
*/
ProxyConfig.prototype.httpProxy;
/**
* The proxy host for HTTPS requests. Only used if {@link #proxyType} is
* "manual".
* @type {(string|undefined)}
*/
ProxyConfig.prototype.sslProxy;
/**
* A comma delimited list of hosts which should bypass all proxies. Only used if
* {@link #proxyType} is "manual".
* @type {(string|undefined)}
*/
ProxyConfig.prototype.noProxy;
/**
* Converts a generic hash object to a map.
* @param {!Object<string, ?>} hash The hash object.
* @return {!Map<string, ?>} The converted map.
*/
function toMap(hash) {
let m = new Map;
for (let key in hash) {
if (hash.hasOwnProperty(key)) {
m.set(key, hash[key]);
}
}
return m;
}
/**
* Describes a set of capabilities for a WebDriver session.
*/
class Capabilities extends Map {
/**
* @param {(Capabilities|Map<string, ?>|Object)=} opt_other Another set of
* capabilities to initialize this instance from.
*/
constructor(opt_other) {
if (opt_other && !(opt_other instanceof Map)) {
opt_other = toMap(opt_other);
}
super(opt_other);
}
/**
* @return {!Capabilities} A basic set of capabilities for Android.
*/
static android() {
return new Capabilities()
.set(Capability.BROWSER_NAME, Browser.ANDROID)
.set(Capability.PLATFORM, 'ANDROID');
}
/**
* @return {!Capabilities} A basic set of capabilities for Chrome.
*/
static chrome() {
return new Capabilities().set(Capability.BROWSER_NAME, Browser.CHROME);
}
/**
* @return {!Capabilities} A basic set of capabilities for Microsoft Edge.
*/
static edge() {
return new Capabilities()
.set(Capability.BROWSER_NAME, Browser.EDGE)
.set(Capability.PLATFORM, 'WINDOWS');
}
/**
* @return {!Capabilities} A basic set of capabilities for Firefox.
*/
static firefox() {
return new Capabilities().set(Capability.BROWSER_NAME, Browser.FIREFOX);
}
/**
* @return {!Capabilities} A basic set of capabilities for Internet Explorer.
*/
static ie() {
return new Capabilities().
set(Capability.BROWSER_NAME, Browser.INTERNET_EXPLORER).
set(Capability.PLATFORM, 'WINDOWS');
}
/**
* @return {!Capabilities} A basic set of capabilities for iPad.
*/
static ipad() {
return new Capabilities().
set(Capability.BROWSER_NAME, Browser.IPAD).
set(Capability.PLATFORM, 'MAC');
}
/**
* @return {!Capabilities} A basic set of capabilities for iPhone.
*/
static iphone() {
return new Capabilities().
set(Capability.BROWSER_NAME, Browser.IPHONE).
set(Capability.PLATFORM, 'MAC');
}
/**
* @return {!Capabilities} A basic set of capabilities for Opera.
*/
static opera() {
return new Capabilities().
set(Capability.BROWSER_NAME, Browser.OPERA);
}
/**
* @return {!Capabilities} A basic set of capabilities for PhantomJS.
*/
static phantomjs() {
return new Capabilities().
set(Capability.BROWSER_NAME, Browser.PHANTOM_JS);
}
/**
* @return {!Capabilities} A basic set of capabilities for Safari.
*/
static safari() {
return new Capabilities().
set(Capability.BROWSER_NAME, Browser.SAFARI).
set(Capability.PLATFORM, 'MAC');
}
/**
* @return {!Capabilities} A basic set of capabilities for HTMLUnit.
*/
static htmlunit() {
return new Capabilities().
set(Capability.BROWSER_NAME, Browser.HTMLUNIT);
}
/**
* @return {!Capabilities} A basic set of capabilities for HTMLUnit
* with enabled Javascript.
*/
static htmlunitwithjs() {
return new Capabilities().
set(Capability.BROWSER_NAME, Browser.HTMLUNIT).
set(Capability.SUPPORTS_JAVASCRIPT, true);
}
/**
* @return {!Object<string, ?>} The JSON representation of this instance.
* Note, the returned object may contain nested promised values.
* @suppress {checkTypes} Suppress [] access on a struct (state inherited from
* Map).
*/
[Symbols.serialize]() {
return serialize(this);
}
/**
* Merges another set of capabilities into this instance.
* @param {!(Capabilities|Map<String, ?>|Object<string, ?>)} other The other
* set of capabilities to merge.
* @return {!Capabilities} A self reference.
*/
merge(other) {
if (!other) {
throw new TypeError('no capabilities provided for merge');
}
if (!(other instanceof Map)) {
other = toMap(other);
}
for (let key of other.keys()) {
this.set(key, other.get(key));
}
return this;
}
/**
* @param {string} key The capability key.
* @param {*} value The capability value.
* @return {!Capabilities} A self reference.
* @throws {TypeError} If the `key` is not a string.
* @override
*/
set(key, value) {
if (typeof key !== 'string') {
throw new TypeError('Capability keys must be strings: ' + typeof key);
}
super.set(key, value);
return this;
}
/**
* Sets the logging preferences. Preferences may be specified as a
* {@link ./logging.Preferences} instance, or a as a map of log-type to
* log-level.
* @param {!(./logging.Preferences|Object<string>)} prefs The logging
* preferences.
* @return {!Capabilities} A self reference.
*/
setLoggingPrefs(prefs) {
return this.set(Capability.LOGGING_PREFS, prefs);
}
/**
* Sets the proxy configuration for this instance.
* @param {ProxyConfig} proxy The desired proxy configuration.
* @return {!Capabilities} A self reference.
*/
setProxy(proxy) {
return this.set(Capability.PROXY, proxy);
}
/**
* Sets whether native events should be used.
* @param {boolean} enabled Whether to enable native events.
* @return {!Capabilities} A self reference.
*/
setEnableNativeEvents(enabled) {
return this.set(Capability.NATIVE_EVENTS, enabled);
}
/**
* Sets how elements should be scrolled into view for interaction.
* @param {number} behavior The desired scroll behavior: either 0 to align
* with the top of the viewport or 1 to align with the bottom.
* @return {!Capabilities} A self reference.
*/
setScrollBehavior(behavior) {
return this.set(Capability.ELEMENT_SCROLL_BEHAVIOR, behavior);
}
/**
* Sets the default action to take with an unexpected alert before returning
* an error.
* @param {string} behavior The desired behavior; should be "accept",
* "dismiss", or "ignore". Defaults to "dismiss".
* @return {!Capabilities} A self reference.
*/
setAlertBehavior(behavior) {
return this.set(Capability.UNEXPECTED_ALERT_BEHAVIOR, behavior);
}
}
/**
* Serializes a capabilities object. This is defined as a standalone function
* so it may be type checked (where Capabilities[Symbols.serialize] has type
* checking disabled since it is defined with [] access on a struct).
*
* @param {!Capabilities} caps The capabilities to serialize.
* @return {!Object<string, ?>} The JSON representation of this instance.
* Note, the returned object may contain nested promised values.
*/
function serialize(caps) {
let ret = {};
for (let key of caps.keys()) {
let cap = caps.get(key);
if (cap !== undefined && cap !== null) {
ret[key] = cap;
}
}
return ret;
}
// PUBLIC API
module.exports = {
Browser: Browser,
Capabilities: Capabilities,
Capability: Capability,
ProxyConfig: ProxyConfig
};