-
Notifications
You must be signed in to change notification settings - Fork 0
/
opera.js
405 lines (357 loc) · 12.8 KB
/
opera.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
// 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.
/**
* @fileoverview Defines a {@linkplain Driver WebDriver} client for the
* Opera web browser (v26+). Before using this module, you must download the
* latest OperaDriver
* [release](https://github.com/operasoftware/operachromiumdriver/releases) and
* ensure it can be found on your system
* [PATH](http://en.wikipedia.org/wiki/PATH_%28variable%29).
*
* There are three primary classes exported by this module:
*
* 1. {@linkplain ServiceBuilder}: configures the
* {@link selenium-webdriver/remote.DriverService remote.DriverService}
* that manages the
* [OperaDriver](https://github.com/operasoftware/operachromiumdriver)
* child process.
*
* 2. {@linkplain Options}: defines configuration options for each new Opera
* session, such as which {@linkplain Options#setProxy proxy} to use,
* what {@linkplain Options#addExtensions extensions} to install, or
* what {@linkplain Options#addArguments command-line switches} to use when
* starting the browser.
*
* 3. {@linkplain Driver}: the WebDriver client; each new instance will control
* a unique browser session with a clean user profile (unless otherwise
* configured through the {@link Options} class).
*
* By default, every Opera session will use a single driver service, which is
* started the first time a {@link Driver} instance is created and terminated
* when this process exits. The default service will inherit its environment
* from the current process and direct all output to /dev/null. You may obtain
* a handle to this default service using
* {@link #getDefaultService getDefaultService()} and change its configuration
* with {@link #setDefaultService setDefaultService()}.
*
* You may also create a {@link Driver} with its own driver service. This is
* useful if you need to capture the server's log output for a specific session:
*
* var opera = require('selenium-webdriver/opera');
*
* var service = new opera.ServiceBuilder()
* .loggingTo('/my/log/file.txt')
* .enableVerboseLogging()
* .build();
*
* var options = new opera.Options();
* // configure browser options ...
*
* var driver = opera.Driver.createSession(options, service);
*
* Users should only instantiate the {@link Driver} class directly when they
* need a custom driver service configuration (as shown above). For normal
* operation, users should start Opera using the
* {@link selenium-webdriver.Builder}.
*/
'use strict';
const fs = require('fs');
const http = require('./http'),
io = require('./io'),
capabilities = require('./lib/capabilities'),
promise = require('./lib/promise'),
Symbols = require('./lib/symbols'),
webdriver = require('./lib/webdriver'),
portprober = require('./net/portprober'),
remote = require('./remote');
/**
* Name of the OperaDriver executable.
* @type {string}
* @const
*/
const OPERADRIVER_EXE =
process.platform === 'win32' ? 'operadriver.exe' : 'operadriver';
/**
* Creates {@link remote.DriverService} instances that manages an
* [OperaDriver](https://github.com/operasoftware/operachromiumdriver)
* server in a child process.
*/
class ServiceBuilder extends remote.DriverService.Builder {
/**
* @param {string=} opt_exe Path to the server executable to use. If omitted,
* the builder will attempt to locate the operadriver on the current
* PATH.
* @throws {Error} If provided executable does not exist, or the operadriver
* cannot be found on the PATH.
*/
constructor(opt_exe) {
let exe = opt_exe || io.findInPath(OPERADRIVER_EXE, true);
if (!exe) {
throw Error(
'The OperaDriver could not be found on the current PATH. Please ' +
'download the latest version of the OperaDriver from ' +
'https://github.com/operasoftware/operachromiumdriver/releases and ' +
'ensure it can be found on your PATH.');
}
super(exe);
this.setLoopback(true);
}
/**
* Sets the path of the log file the driver should log to. If a log file is
* not specified, the driver will log to stderr.
* @param {string} path Path of the log file to use.
* @return {!ServiceBuilder} A self reference.
*/
loggingTo(path) {
return this.addArguments('--log-path=' + path);
}
/**
* Enables verbose logging.
* @return {!ServiceBuilder} A self reference.
*/
enableVerboseLogging() {
return this.addArguments('--verbose');
}
/**
* Silence sthe drivers output.
* @return {!ServiceBuilder} A self reference.
*/
silent() {
return this.addArguments('--silent');
}
}
/** @type {remote.DriverService} */
var defaultService = null;
/**
* Sets the default service to use for new OperaDriver instances.
* @param {!remote.DriverService} service The service to use.
* @throws {Error} If the default service is currently running.
*/
function setDefaultService(service) {
if (defaultService && defaultService.isRunning()) {
throw Error(
'The previously configured OperaDriver service is still running. ' +
'You must shut it down before you may adjust its configuration.');
}
defaultService = service;
}
/**
* Returns the default OperaDriver service. If such a service has not been
* configured, one will be constructed using the default configuration for
* a OperaDriver executable found on the system PATH.
* @return {!remote.DriverService} The default OperaDriver service.
*/
function getDefaultService() {
if (!defaultService) {
defaultService = new ServiceBuilder().build();
}
return defaultService;
}
/**
* @type {string}
* @const
*/
var OPTIONS_CAPABILITY_KEY = 'chromeOptions';
/**
* Class for managing {@linkplain Driver OperaDriver} specific options.
*/
class Options {
constructor() {
/** @private {!Array.<string>} */
this.args_ = [];
/** @private {?string} */
this.binary_ = null;
/** @private {!Array.<(string|!Buffer)>} */
this.extensions_ = [];
/** @private {./lib/logging.Preferences} */
this.logPrefs_ = null;
/** @private {?capabilities.ProxyConfig} */
this.proxy_ = null;
}
/**
* Extracts the OperaDriver specific options from the given capabilities
* object.
* @param {!capabilities.Capabilities} caps The capabilities object.
* @return {!Options} The OperaDriver options.
*/
static fromCapabilities(caps) {
var options;
var o = caps.get(OPTIONS_CAPABILITY_KEY);
if (o instanceof Options) {
options = o;
} else if (o) {
options = new Options()
.addArguments(o.args || [])
.addExtensions(o.extensions || [])
.setOperaBinaryPath(o.binary);
} else {
options = new Options;
}
if (caps.has(capabilities.Capability.PROXY)) {
options.setProxy(caps.get(capabilities.Capability.PROXY));
}
if (caps.has(capabilities.Capability.LOGGING_PREFS)) {
options.setLoggingPrefs(
caps.get(capabilities.Capability.LOGGING_PREFS));
}
return options;
}
/**
* Add additional command line arguments to use when launching the Opera
* browser. Each argument may be specified with or without the "--" prefix
* (e.g. "--foo" and "foo"). Arguments with an associated value should be
* delimited by an "=": "foo=bar".
* @param {...(string|!Array.<string>)} var_args The arguments to add.
* @return {!Options} A self reference.
*/
addArguments(var_args) {
this.args_ = this.args_.concat.apply(this.args_, arguments);
return this;
}
/**
* Add additional extensions to install when launching Opera. Each extension
* should be specified as the path to the packed CRX file, or a Buffer for an
* extension.
* @param {...(string|!Buffer|!Array.<(string|!Buffer)>)} var_args The
* extensions to add.
* @return {!Options} A self reference.
*/
addExtensions(var_args) {
this.extensions_ = this.extensions_.concat.apply(
this.extensions_, arguments);
return this;
}
/**
* Sets the path to the Opera binary to use. On Mac OS X, this path should
* reference the actual Opera executable, not just the application binary. The
* binary path be absolute or relative to the operadriver server executable, but
* it must exist on the machine that will launch Opera.
*
* @param {string} path The path to the Opera binary to use.
* @return {!Options} A self reference.
*/
setOperaBinaryPath(path) {
this.binary_ = path;
return this;
}
/**
* Sets the logging preferences for the new session.
* @param {!./lib/logging.Preferences} prefs The logging preferences.
* @return {!Options} A self reference.
*/
setLoggingPrefs(prefs) {
this.logPrefs_ = prefs;
return this;
}
/**
* Sets the proxy settings for the new session.
* @param {capabilities.ProxyConfig} proxy The proxy configuration to use.
* @return {!Options} A self reference.
*/
setProxy(proxy) {
this.proxy_ = proxy;
return this;
}
/**
* Converts this options instance to a {@link capabilities.Capabilities}
* object.
* @param {capabilities.Capabilities=} opt_capabilities The capabilities to
* merge these options into, if any.
* @return {!capabilities.Capabilities} The capabilities.
*/
toCapabilities(opt_capabilities) {
var caps = opt_capabilities || capabilities.Capabilities.opera();
caps.
set(capabilities.Capability.PROXY, this.proxy_).
set(capabilities.Capability.LOGGING_PREFS, this.logPrefs_).
set(OPTIONS_CAPABILITY_KEY, this);
return caps;
}
/**
* Converts this instance to its JSON wire protocol representation. Note this
* function is an implementation not intended for general use.
* @return {!Object} The JSON wire protocol representation of this instance.
*/
[Symbols.serialize]() {
var json = {
args: this.args_,
extensions: this.extensions_.map(function(extension) {
if (Buffer.isBuffer(extension)) {
return extension.toString('base64');
}
return io.read(/** @type {string} */(extension))
.then(buffer => buffer.toString('base64'));
})
};
if (this.binary_) {
json.binary = this.binary_;
}
return json;
}
}
/**
* Creates a new WebDriver client for Opera.
*/
class Driver extends webdriver.WebDriver {
/**
* Creates a new session for Opera.
*
* @param {(capabilities.Capabilities|Options)=} opt_config The configuration
* options.
* @param {remote.DriverService=} opt_service The session to use; will use
* the {@link getDefaultService default service} by default.
* @param {promise.ControlFlow=} opt_flow The control flow to use,
* or {@code null} to use the currently active flow.
* @return {!Driver} A new driver instance.
*/
static createSession(opt_config, opt_service, opt_flow) {
var service = opt_service || getDefaultService();
var client = service.start().then(url => new http.HttpClient(url));
var executor = new http.Executor(client);
var caps =
opt_config instanceof Options ? opt_config.toCapabilities() :
(opt_config || capabilities.Capabilities.opera());
// On Linux, the OperaDriver does not look for Opera on the PATH, so we
// must explicitly find it. See: operachromiumdriver #9.
if (process.platform === 'linux') {
var options = Options.fromCapabilities(caps);
if (!options.binary_) {
let exe = io.findInPath('opera', true);
if (!exe) {
throw Error(
'The opera executable could not be found on the current PATH');
}
options.setOperaBinaryPath(exe);
}
caps = options.toCapabilities(caps);
}
return /** @type {!Driver} */(
webdriver.WebDriver.createSession(executor, caps, opt_flow, this));
}
/**
* This function is a no-op as file detectors are not supported by this
* implementation.
* @override
*/
setFileDetector() {}
}
// PUBLIC API
exports.Driver = Driver;
exports.Options = Options;
exports.ServiceBuilder = ServiceBuilder;
exports.getDefaultService = getDefaultService;
exports.setDefaultService = setDefaultService;