UNPKG

7.76 kBJavaScriptView Raw
1// Licensed to the Software Freedom Conservancy (SFC) under one
2// or more contributor license agreements. See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership. The SFC licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License. You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied. See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18/**
19 * @fileoverview Defines a {@linkplain Driver WebDriver} client for
20 * Microsoft's Edge web browser. Before using this module,
21 * you must download and install the latest
22 * [MicrosoftEdgeDriver](http://go.microsoft.com/fwlink/?LinkId=619687) server.
23 * Ensure that the MicrosoftEdgeDriver is on your
24 * [PATH](http://en.wikipedia.org/wiki/PATH_%28variable%29).
25 *
26 * There are three primary classes exported by this module:
27 *
28 * 1. {@linkplain ServiceBuilder}: configures the
29 * {@link ./remote.DriverService remote.DriverService}
30 * that manages the [MicrosoftEdgeDriver] child process.
31 *
32 * 2. {@linkplain Options}: defines configuration options for each new
33 * MicrosoftEdgeDriver session, such as which
34 * {@linkplain Options#setProxy proxy} to use when starting the browser.
35 *
36 * 3. {@linkplain Driver}: the WebDriver client; each new instance will control
37 * a unique browser session.
38 *
39 * __Customizing the MicrosoftEdgeDriver Server__ <a id="custom-server"></a>
40 *
41 * By default, every MicrosoftEdge session will use a single driver service,
42 * which is started the first time a {@link Driver} instance is created and
43 * terminated when this process exits. The default service will inherit its
44 * environment from the current process.
45 * You may obtain a handle to this default service using
46 * {@link #getDefaultService getDefaultService()} and change its configuration
47 * with {@link #setDefaultService setDefaultService()}.
48 *
49 * You may also create a {@link Driver} with its own driver service. This is
50 * useful if you need to capture the server's log output for a specific session:
51 *
52 * var edge = require('selenium-webdriver/edge');
53 *
54 * var service = new edge.ServiceBuilder()
55 * .setPort(55555)
56 * .build();
57 *
58 * var options = new edge.Options();
59 * // configure browser options ...
60 *
61 * var driver = edge.Driver.createSession(options, service);
62 *
63 * Users should only instantiate the {@link Driver} class directly when they
64 * need a custom driver service configuration (as shown above). For normal
65 * operation, users should start MicrosoftEdge using the
66 * {@link ./builder.Builder selenium-webdriver.Builder}.
67 *
68 * [MicrosoftEdgeDriver]: https://msdn.microsoft.com/en-us/library/mt188085(v=vs.85).aspx
69 */
70
71'use strict';
72
73const fs = require('fs');
74const util = require('util');
75
76const http = require('./http');
77const io = require('./io');
78const portprober = require('./net/portprober');
79const promise = require('./lib/promise');
80const remote = require('./remote');
81const Symbols = require('./lib/symbols');
82const webdriver = require('./lib/webdriver');
83const {Browser, Capabilities} = require('./lib/capabilities');
84
85const EDGEDRIVER_EXE = 'MicrosoftWebDriver.exe';
86
87
88/**
89 * _Synchronously_ attempts to locate the edge driver executable on the current
90 * system.
91 *
92 * @return {?string} the located executable, or `null`.
93 */
94function locateSynchronously() {
95 return process.platform === 'win32'
96 ? io.findInPath(EDGEDRIVER_EXE, true) : null;
97}
98
99
100/**
101 * Class for managing MicrosoftEdgeDriver specific options.
102 */
103class Options extends Capabilities {
104 /**
105 * @param {(Capabilities|Map<string, ?>|Object)=} other Another set of
106 * capabilities to initialize this instance from.
107 */
108 constructor(other = undefined) {
109 super(other);
110 this.setBrowserName(Browser.EDGE);
111 }
112}
113
114
115/**
116 * Creates {@link remote.DriverService} instances that manage a
117 * MicrosoftEdgeDriver server in a child process.
118 */
119class ServiceBuilder extends remote.DriverService.Builder {
120 /**
121 * @param {string=} opt_exe Path to the server executable to use. If omitted,
122 * the builder will attempt to locate the MicrosoftEdgeDriver on the current
123 * PATH.
124 * @throws {Error} If provided executable does not exist, or the
125 * MicrosoftEdgeDriver cannot be found on the PATH.
126 */
127 constructor(opt_exe) {
128 let exe = opt_exe || locateSynchronously();
129 if (!exe) {
130 throw Error(
131 'The ' + EDGEDRIVER_EXE + ' could not be found on the current PATH. ' +
132 'Please download the latest version of the MicrosoftEdgeDriver from ' +
133 'https://www.microsoft.com/en-us/download/details.aspx?id=48212 and ' +
134 'ensure it can be found on your PATH.');
135 }
136
137 super(exe);
138
139 // Binding to the loopback address will fail if not running with
140 // administrator privileges. Since we cannot test for that in script
141 // (or can we?), force the DriverService to use "localhost".
142 this.setHostname('localhost');
143 }
144
145 /**
146 * Enables verbose logging.
147 * @return {!ServiceBuilder} A self reference.
148 */
149 enableVerboseLogging() {
150 return this.addArguments('--verbose');
151 }
152}
153
154
155/** @type {remote.DriverService} */
156var defaultService = null;
157
158
159/**
160 * Sets the default service to use for new MicrosoftEdgeDriver instances.
161 * @param {!remote.DriverService} service The service to use.
162 * @throws {Error} If the default service is currently running.
163 */
164function setDefaultService(service) {
165 if (defaultService && defaultService.isRunning()) {
166 throw Error(
167 'The previously configured EdgeDriver service is still running. ' +
168 'You must shut it down before you may adjust its configuration.');
169 }
170 defaultService = service;
171}
172
173
174/**
175 * Returns the default MicrosoftEdgeDriver service. If such a service has
176 * not been configured, one will be constructed using the default configuration
177 * for an MicrosoftEdgeDriver executable found on the system PATH.
178 * @return {!remote.DriverService} The default MicrosoftEdgeDriver service.
179 */
180function getDefaultService() {
181 if (!defaultService) {
182 defaultService = new ServiceBuilder().build();
183 }
184 return defaultService;
185}
186
187
188/**
189 * Creates a new WebDriver client for Microsoft's Edge.
190 */
191class Driver extends webdriver.WebDriver {
192 /**
193 * Creates a new browser session for Microsoft's Edge browser.
194 *
195 * @param {(Capabilities|Options)=} options The configuration options.
196 * @param {remote.DriverService=} service The session to use; will use
197 * the {@linkplain #getDefaultService default service} by default.
198 * @return {!Driver} A new driver instance.
199 */
200 static createSession(options, opt_service) {
201 let service = opt_service || getDefaultService();
202 let client = service.start().then(url => new http.HttpClient(url));
203 let executor = new http.Executor(client);
204
205 options = options || new Options();
206 return /** @type {!Driver} */(super.createSession(
207 executor, options, () => service.kill()));
208 }
209
210 /**
211 * This function is a no-op as file detectors are not supported by this
212 * implementation.
213 * @override
214 */
215 setFileDetector() {}
216}
217
218
219// PUBLIC API
220
221
222exports.Driver = Driver;
223exports.Options = Options;
224exports.ServiceBuilder = ServiceBuilder;
225exports.getDefaultService = getDefaultService;
226exports.setDefaultService = setDefaultService;
227exports.locateSynchronously = locateSynchronously;