forked from SeleniumHQ/selenium
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathedge.js
More file actions
265 lines (240 loc) · 9.24 KB
/
Copy pathedge.js
File metadata and controls
265 lines (240 loc) · 9.24 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
// 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
* Microsoft's Edge web browser. Both Edge (Chromium), and Edge Legacy (EdgeHTML) are
* supported. Before using this module, you must download and install the correct
* [WebDriver](https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/)
* server.
*
* Ensure that the either MicrosoftWebDriver (EdgeHTML) or msedgedriver (Chromium)
* is on your [PATH](http://en.wikipedia.org/wiki/PATH_%28variable%29). MicrosoftWebDriver
* and Edge Legacy (EdgeHTML) will be used by default.
*
* You may use {@link Options} to specify whether Edge Chromium should be used:
* var options = new edge.Options();
* options.useEdgeChromium(true);
* // configure browser options ...
* Note that Chromium-specific {@link Options} will be ignored when using Edge Legacy.
*
* There are three primary classes exported by this module:
*
* 1. {@linkplain ServiceBuilder}: configures the
* {@link ./remote.DriverService remote.DriverService}
* that manages the [WebDriver] child process.
*
* 2. {@linkplain Options}: defines configuration options for each new
* WebDriver session, such as which
* {@linkplain Options#setProxy proxy} to use when starting the browser.
*
* 3. {@linkplain Driver}: the WebDriver client; each new instance will control
* a unique browser session.
*
* __Customizing the WebDriver Server__ <a id="custom-server"></a>
*
* By default, every MicrosoftEdge 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.
* 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 edge = require('selenium-webdriver/edge');
*
* var service = new edge.ServiceBuilder()
* .setPort(55555)
* .build();
*
* var options = new edge.Options();
* // configure browser options ...
*
* var driver = edge.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 MicrosoftEdge using the
* {@link ./builder.Builder selenium-webdriver.Builder}.
*
* [WebDriver (EdgeHTML)]: https://docs.microsoft.com/en-us/microsoft-edge/webdriver
* [WebDriver (Chromium)]: https://docs.microsoft.com/en-us/microsoft-edge/webdriver-chromium
*/
'use strict'
const http = require('./http')
const io = require('./io')
const webdriver = require('./lib/webdriver')
const { Browser, Capabilities } = require('./lib/capabilities')
const chromium = require('./chromium')
const EDGE_CHROMIUM_BROWSER_NAME = 'msedge'
const EDGEDRIVER_LEGACY_EXE = 'MicrosoftWebDriver.exe'
const EDGEDRIVER_CHROMIUM_EXE =
process.platform === 'win32' ? 'msedgedriver.exe' : 'msedgedriver'
/**
* _Synchronously_ attempts to locate the Edge driver executable
* on the current system. Searches for the legacy MicrosoftWebDriver by default.
*
* @param {string=} browserName Name of the Edge driver executable to locate.
* May be either 'msedge' to locate the Edge Chromium driver, or 'MicrosoftEdge' to
* locate the Edge Legacy driver. If omitted, will attempt to locate Edge Legacy.
* @return {?string} the located executable, or `null`.
*/
function locateSynchronously(browserName) {
browserName = browserName || Browser.EDGE
if (browserName === EDGE_CHROMIUM_BROWSER_NAME) {
return io.findInPath(EDGEDRIVER_CHROMIUM_EXE, true)
}
return process.platform === 'win32'
? io.findInPath(EDGEDRIVER_LEGACY_EXE, true)
: null
}
/**
* Class for managing Edge specific options.
*/
class Options extends chromium.Options {
/**
* Instruct the EdgeDriver to use Edge Chromium if true.
* Otherwise, use Edge Legacy (EdgeHTML). Defaults to using Edge Legacy.
*
* @param {boolean} useEdgeChromium
* @return {!Options} A self reference.
*/
setEdgeChromium(useEdgeChromium) {
this.set(Options.USE_EDGE_CHROMIUM, !!useEdgeChromium)
return this
}
}
Options.USE_EDGE_CHROMIUM = 'ms:edgeChromium'
Options.prototype.BROWSER_NAME_VALUE = Browser.EDGE
Options.prototype.CAPABILITY_KEY = 'ms:edgeOptions'
Options.prototype.VENDOR_CAPABILITY_PREFIX = 'ms'
/**
* @param {(Capabilities|Object<string, *>)=} o The options object
* @return {boolean}
*/
function useEdgeChromium(o) {
if (o instanceof Capabilities) {
return !!o.get(Options.USE_EDGE_CHROMIUM)
}
if (o && typeof o === 'object') {
return !!o[Options.USE_EDGE_CHROMIUM]
}
return false
}
/**
* Creates {@link remote.DriverService} instances that manage a
* WebDriver server in a child process. Used for driving both
* Microsoft Edge Legacy and Chromium. A ServiceBuilder constructed
* with default parameters will launch a MicrosoftWebDriver child
* process for driving Edge Legacy. You may pass in a path to
* msedgedriver.exe to use Edge Chromium instead.
*/
class ServiceBuilder extends chromium.ServiceBuilder {
/**
* @param {string=} opt_exe Path to the server executable to use. If omitted,
* the builder will attempt to locate MicrosoftWebDriver on the current
* PATH.
* @throws {Error} If provided executable does not exist, or the
* MicrosoftWebDriver cannot be found on the PATH.
*/
constructor(opt_exe) {
let exe = opt_exe || locateSynchronously()
if (!exe) {
throw Error(
'The WebDriver for Edge could not be found on the current PATH. Please ' +
'download the latest version of ' +
EDGEDRIVER_LEGACY_EXE +
' from ' +
'https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/ and ' +
'ensure it can be found on your PATH.'
)
}
super(exe)
}
}
/** @type {remote.DriverService} */
var defaultService = null
/**
* Sets the default service to use for new Edge 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 EdgeDriver service is still running. ' +
'You must shut it down before you may adjust its configuration.'
)
}
defaultService = service
}
/**
* Returns the default Microsoft Edge driver service. If such a service has
* not been configured, one will be constructed using the default configuration
* for a MicrosoftWebDriver executable found on the system PATH.
* @return {!remote.DriverService} The default Microsoft Edge driver service.
*/
function getDefaultService() {
if (!defaultService) {
defaultService = new ServiceBuilder().build()
}
return defaultService
}
function createServiceFromCapabilities(options) {
let exe
if (useEdgeChromium(options)) {
exe = locateSynchronously(EDGE_CHROMIUM_BROWSER_NAME)
}
return new ServiceBuilder(exe).build()
}
/**
* Creates a new WebDriver client for Microsoft's Edge.
*/
class Driver extends webdriver.WebDriver {
/**
* Creates a new browser session for Microsoft's Edge browser.
*
* @param {(Capabilities|Options)=} options The configuration options.
* @param {remote.DriverService=} opt_service The service to use; will create
* a new Legacy or Chromium service based on {@linkplain Options} by default.
* @return {!Driver} A new driver instance.
*/
static createSession(options, opt_service) {
options = options || new Options()
let service = opt_service || createServiceFromCapabilities(options)
let client = service.start().then((url) => new http.HttpClient(url))
let executor = new http.Executor(client)
return /** @type {!Driver} */ (super.createSession(executor, options, () =>
service.kill()
))
}
/**
* 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
exports.locateSynchronously = locateSynchronously