-
-
Notifications
You must be signed in to change notification settings - Fork 8.4k
/
Copy pathcapabilities.js
555 lines (495 loc) · 14.6 KB
/
capabilities.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
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
// 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 = {
CHROME: 'chrome',
EDGE: 'MicrosoftEdge',
FIREFOX: 'firefox',
INTERNET_EXPLORER: 'internet explorer',
SAFARI: 'safari',
}
/**
* Strategies for waiting for [document readiness] after a navigation
* event.
*
* [document readiness]: https://html.spec.whatwg.org/#current-document-readiness
*
* @enum {string}
*/
const PageLoadStrategy = {
/**
* Indicates WebDriver should not wait on the document readiness state after a
* navigation event.
*/
NONE: 'none',
/**
* Indicates WebDriver should wait for the document readiness state to
* become "interactive" after navigation.
*/
EAGER: 'eager',
/**
* Indicates WebDriver should wait for the document readiness state to
* be "complete" after navigation. This is the default page loading strategy.
*/
NORMAL: 'normal',
}
/**
* Common platform names. These platforms are not explicitly defined by the
* WebDriver spec, however, their use is encouraged for interoperability.
*
* @enum {string}
* @see <https://w3c.github.io/webdriver/webdriver-spec.html>
*/
const Platform = {
LINUX: 'linux',
MAC: 'mac',
WINDOWS: 'windows',
}
/**
* Record object defining the timeouts that apply to certain WebDriver actions.
*
* @record
*/
function Timeouts() {}
/**
* Defines when, in milliseconds, to interrupt a script that is being
* {@linkplain ./webdriver.IWebDriver#executeScript evaluated}.
* @type {number}
*/
Timeouts.prototype.script
/**
* The timeout, in milliseconds, to apply to navigation events along with the
* {@link PageLoadStrategy}.
* @type {number}
*/
Timeouts.prototype.pageLoad
/**
* The maximum amount of time, in milliseconds, to spend attempting to
* {@linkplain ./webdriver.IWebDriver#findElement locate} an element on the
* current page.
* @type {number}
*/
Timeouts.prototype.implicit
/**
* The possible default actions a WebDriver session can take to respond to
* unhandled user prompts (`window.alert()`, `window.confirm()`, and
* `window.prompt()`).
*
* @enum {string}
*/
const UserPromptHandler = {
/** All prompts should be silently accepted. */
ACCEPT: 'accept',
/** All prompts should be silently dismissed. */
DISMISS: 'dismiss',
/**
* All prompts should be automatically accepted, but an error should be
* returned to the next (or currently executing) WebDriver command.
*/
ACCEPT_AND_NOTIFY: 'accept and notify',
/**
* All prompts should be automatically dismissed, but an error should be
* returned to the next (or currently executing) WebDriver command.
*/
DISMISS_AND_NOTIFY: 'dismiss and notify',
/** All prompts should be left unhandled. */
IGNORE: 'ignore',
}
/**
* The standard WebDriver capability keys.
*
* @enum {string}
* @see <https://w3c.github.io/webdriver/webdriver-spec.html#capabilities>
*/
const Capability = {
/**
* Indicates whether a WebDriver session implicitly trusts otherwise untrusted
* and self-signed TLS certificates during navigation.
*/
ACCEPT_INSECURE_TLS_CERTS: 'acceptInsecureCerts',
/**
* The browser name. Common browser names are defined in the
* {@link ./capabilities.Browser Browser} enum.
*/
BROWSER_NAME: 'browserName',
/** Identifies the browser version. */
BROWSER_VERSION: 'browserVersion',
/**
* Key for the logging driver logging preferences.
* The browser name. Common browser names are defined in the
* {@link ./capabilities.Browser Browser} enum.
*/
LOGGING_PREFS: 'goog:loggingPrefs',
/**
* Defines the session's
* {@linkplain ./capabilities.PageLoadStrategy page loading strategy}.
*/
PAGE_LOAD_STRATEGY: 'pageLoadStrategy',
/**
* Identifies the operating system of the endpoint node. Common values
* recognized by the most WebDriver server implementations are predefined in
* the {@link ./capabilities.Platform Platform} enum.
*/
PLATFORM_NAME: 'platformName',
/**
* Describes the proxy configuration to use for a new WebDriver session.
*/
PROXY: 'proxy',
/**
* Indicates whether the remote end supports all of the window resizing and
* positioning commands:
*
* - {@linkplain ./webdriver.Window#getRect Window.getRect()}
* - {@linkplain ./webdriver.Window#setRect Window.setRect()}
* - {@linkplain ./webdriver.Window#maximize Window.maximize()}
* - {@linkplain ./webdriver.Window#minimize Window.minimize()}
* - {@linkplain ./webdriver.Window#fullscreen Window.fullscreen()}
*/
SET_WINDOW_RECT: 'setWindowRect',
/**
* Describes the {@linkplain ./capabilities.Timeouts timeouts} imposed on
* certain session operations.
*/
TIMEOUTS: 'timeouts',
/**
* Defines how a WebDriver session should
* {@linkplain ./capabilities.UserPromptHandler respond} to unhandled user
* prompts.
*/
UNHANDLED_PROMPT_BEHAVIOR: 'unhandledPromptBehavior',
/**
* Defines the current session’s strict file interactability.
* Used to upload a file when strict file interactability is on
*/
STRICT_FILE_INTERACTABILITY: 'strictFileInteractability',
ENABLE_DOWNLOADS: 'se:downloadsEnabled',
}
/**
* 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 (Object.prototype.hasOwnProperty.call(hash, key)) {
m.set(key, hash[key])
}
}
return m
}
/**
* Describes a set of capabilities for a WebDriver session.
*/
class Capabilities {
/**
* @param {(Capabilities|Map<string, ?>|Object)=} other Another set of
* capabilities to initialize this instance from.
*/
constructor(other = undefined) {
if (other instanceof Capabilities) {
other = other.map_
} else if (other && !(other instanceof Map)) {
other = toMap(other)
}
/** @private @const {!Map<string, ?>} */
this.map_ = new Map(other)
}
/** @return {number} The number of capabilities set. */
get size() {
return this.map_.size
}
/**
* @return {!Capabilities} A basic set of capabilities for Chrome.
*/
static chrome() {
return new Capabilities().setBrowserName(Browser.CHROME)
}
/**
* @return {!Capabilities} A basic set of capabilities for Microsoft Edge.
*/
static edge() {
return new Capabilities().setBrowserName(Browser.EDGE)
}
/**
* @return {!Capabilities} A basic set of capabilities for Firefox.
*/
static firefox() {
return new Capabilities().setBrowserName(Browser.FIREFOX).set('moz:debuggerAddress', true)
}
/**
* @return {!Capabilities} A basic set of capabilities for Internet Explorer.
*/
static ie() {
return new Capabilities().setBrowserName(Browser.INTERNET_EXPLORER)
}
/**
* @return {!Capabilities} A basic set of capabilities for Safari.
*/
static safari() {
return new Capabilities().setBrowserName(Browser.SAFARI)
}
/**
* @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)
}
/**
* @param {string} key the parameter key to get.
* @return {T} the stored parameter value.
* @template T
*/
get(key) {
return this.map_.get(key)
}
/**
* @param {string} key the key to test.
* @return {boolean} whether this capability set has the specified key.
*/
has(key) {
return this.map_.has(key)
}
/**
* @return {!Iterator<string>} an iterator of the keys set.
*/
keys() {
return this.map_.keys()
}
/**
* 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) {
let otherMap
if (other instanceof Capabilities) {
otherMap = other.map_
} else if (other instanceof Map) {
otherMap = other
} else {
otherMap = toMap(other)
}
otherMap.forEach((value, key) => {
this.set(key, value)
})
return this
} else {
throw new TypeError('no capabilities provided for merge')
}
}
/**
* Deletes an entry from this set of capabilities.
*
* @param {string} key the capability key to delete.
*/
delete(key) {
this.map_.delete(key)
}
/**
* @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.
*/
set(key, value) {
if (typeof key !== 'string') {
throw new TypeError('Capability keys must be strings: ' + typeof key)
}
this.map_.set(key, value)
return this
}
/**
* Sets whether a WebDriver session should implicitly accept self-signed, or
* other untrusted TLS certificates on navigation.
*
* @param {boolean} accept whether to accept insecure certs.
* @return {!Capabilities} a self reference.
*/
setAcceptInsecureCerts(accept) {
return this.set(Capability.ACCEPT_INSECURE_TLS_CERTS, accept)
}
/**
* @return {boolean} whether the session is configured to accept insecure
* TLS certificates.
*/
getAcceptInsecureCerts() {
return this.get(Capability.ACCEPT_INSECURE_TLS_CERTS)
}
/**
* Sets the name of the target browser.
*
* @param {(Browser|string)} name the browser name.
* @return {!Capabilities} a self reference.
*/
setBrowserName(name) {
return this.set(Capability.BROWSER_NAME, name)
}
/**
* @return {(string|undefined)} the configured browser name, or undefined if
* not set.
*/
getBrowserName() {
return this.get(Capability.BROWSER_NAME)
}
/**
* Sets the desired version of the target browser.
*
* @param {string} version the desired version.
* @return {!Capabilities} a self reference.
*/
setBrowserVersion(version) {
return this.set(Capability.BROWSER_VERSION, version)
}
/**
* @return {(string|undefined)} the configured browser version, or undefined
* if not set.
*/
getBrowserVersion() {
return this.get(Capability.BROWSER_VERSION)
}
/**
* Sets the desired page loading strategy for a new WebDriver session.
*
* @param {PageLoadStrategy} strategy the desired strategy.
* @return {!Capabilities} a self reference.
*/
setPageLoadStrategy(strategy) {
return this.set(Capability.PAGE_LOAD_STRATEGY, strategy)
}
/**
* Returns the configured page load strategy.
*
* @return {(string|undefined)} the page load strategy.
*/
getPageLoadStrategy() {
return this.get(Capability.PAGE_LOAD_STRATEGY)
}
/**
* Sets the target platform.
*
* @param {(Platform|string)} platform the target platform.
* @return {!Capabilities} a self reference.
*/
setPlatform(platform) {
return this.set(Capability.PLATFORM_NAME, platform)
}
/**
* @return {(string|undefined)} the configured platform or undefined if not
* set.
*/
getPlatform() {
return this.get(Capability.PLATFORM_NAME)
}
/**
* Sets the logging preferences. Preferences may be specified as a
* {@link ./logging.Preferences} instance, or 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 {proxy.Config} proxy The desired proxy configuration.
* @return {!Capabilities} A self reference.
*/
setProxy(proxy) {
return this.set(Capability.PROXY, proxy)
}
/**
* @return {(proxy.Config|undefined)} the configured proxy settings, or
* undefined if not set.
*/
getProxy() {
return this.get(Capability.PROXY)
}
/**
* Sets the default action to take with an unexpected alert before returning
* an error. If unspecified, WebDriver will default to
* {@link UserPromptHandler.DISMISS_AND_NOTIFY}.
*
* @param {?UserPromptHandler} behavior The way WebDriver should respond to
* unhandled user prompts.
* @return {!Capabilities} A self reference.
*/
setAlertBehavior(behavior) {
return this.set(Capability.UNHANDLED_PROMPT_BEHAVIOR, behavior)
}
/**
* @return {(UserPromptHandler|undefined)} the behavior pattern for responding
* to unhandled user prompts, or undefined if not set.
*/
getAlertBehavior() {
return this.get(Capability.UNHANDLED_PROMPT_BEHAVIOR)
}
/**
* Sets the boolean flag configuration for this instance.
*/
setStrictFileInteractability(strictFileInteractability) {
return this.set(Capability.STRICT_FILE_INTERACTABILITY, strictFileInteractability)
}
enableDownloads() {
return this.set(Capability.ENABLE_DOWNLOADS, true)
}
}
/**
* 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,
Capabilities,
Capability,
PageLoadStrategy,
Platform,
Timeouts,
UserPromptHandler,
}