-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebdriver.js
More file actions
152 lines (134 loc) · 4.84 KB
/
webdriver.js
File metadata and controls
152 lines (134 loc) · 4.84 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
const fs = require('fs');
const util = require('util');
const path = require('path');
const http = require('selenium-webdriver/http');
const io = require('selenium-webdriver/io');
const remote = require('selenium-webdriver/remote');
const webdriver = require('selenium-webdriver/lib/webdriver');
const { Browser, Capabilities } = require('selenium-webdriver/lib/capabilities');
/**
* _Synchronously_ attempts to locate the jsdom driver executable on the current
* system.
*
* @return {?string} the located executable, or `null`.
*/
function locateSynchronously() {
const filename = {
win32: 'jsdom-webdriver-win.exe',
darwin: 'jsdom-webdriver-macos',
linux: 'jsdom-webdriver-linux',
}[process.platform];
return path.resolve(__dirname, './bin/' + filename);
}
/**
* Class for managing JSDOMDriver specific options.
*/
class Options extends Capabilities {
/**
* @param {(Capabilities|Map<string, ?>|Object)=} other Another set of
* capabilities to initialize this instance from.
*/
constructor(other = undefined) {
super(other);
this.setBrowserName('JSDOM');
}
}
/**
* Creates {@link remote.DriverService} instances that manage a
* JSDOMDriver 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 use the locally installed binary.
*/
constructor(opt_exe) {
let exe = opt_exe || locateSynchronously();
super(exe);
// Binding to the loopback address will fail if not running with
// administrator privileges. Since we cannot test for that in script
// (or can we?), force the DriverService to use "localhost".
this.setHostname('localhost');
}
/**
* Enables verbose logging.
* @return {!ServiceBuilder} A self reference.
*/
enableVerboseLogging() {
return this.addArguments('--verbose');
}
}
/** @type {remote.DriverService} */
var defaultService = null;
/**
* Sets the default service to use for new JSDOMDriver 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 JSDOMDriver service is still running. ' +
'You must shut it down before you may adjust its configuration.',
);
}
defaultService = service;
}
/**
* Returns the default JSDOMDriver service. If such a service has
* not been configured, one will be constructed using the default configuration
* for a JSDOMDriver executable found on the system PATH.
* @return {!remote.DriverService} The default JSDOMDriver service.
*/
function getDefaultService() {
if (!defaultService) {
defaultService = new ServiceBuilder().build();
}
return defaultService;
}
/**
* Creates a new WebDriver client for JSDOM.
*/
class JSDOMDriver extends webdriver.WebDriver {
/**
* Creates a new browser session in JSDOM and starts the service, if not already started.
*
* @param {(Capabilities|Options)=} options The configuration options.
* @param {remote.DriverService=} service The session to use; will use
* the {@linkplain #getDefaultService default service} by default.
* @return {!JSDOMDriver} A new driver instance.
*/
static createSession(options, opt_service) {
let service = opt_service || getDefaultService();
let client = service.start().then(url => new http.HttpClient(url));
let executor = new http.Executor(client);
options = options || new Options();
return /** @type {!JSDOMDriver} */ (super.createSession(executor, options, () => service.kill()));
}
/**
* Creates a new browser session in JSDOM.
*
* @param {(Capabilities|Options)=} options The configuration options.
* @param {string} url the address of an existing JSDOM server instance e.g. http://localhost:3000
* @return {!JSDOMDriver} A new driver instance.
*/
static createSessionWithExistingService(options, url) {
let client = new http.HttpClient(url);
let executor = new http.Executor(client);
options = options || new Options();
return /** @type {!JSDOMDriver} */ (super.createSession(executor, options));
}
/**
* This function is a no-op as file detectors are not supported by this
* implementation.
* @override
*/
setFileDetector() {}
}
// PUBLIC API
exports.Driver = JSDOMDriver;
exports.Options = Options;
exports.ServiceBuilder = ServiceBuilder;
exports.getDefaultService = getDefaultService;
exports.setDefaultService = setDefaultService;
exports.locateSynchronously = locateSynchronously;