edge.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. * @fileoverview Defines a {@linkplain Driver WebDriver} client for
  19. * Microsoft's Edge web browser. Before using this module,
  20. * you must download and install the latest
  21. * [MicrosoftEdgeDriver](http://go.microsoft.com/fwlink/?LinkId=619687) server.
  22. * Ensure that the MicrosoftEdgeDriver is on your
  23. * [PATH](http://en.wikipedia.org/wiki/PATH_%28variable%29).
  24. *
  25. * There are three primary classes exported by this module:
  26. *
  27. * 1. {@linkplain ServiceBuilder}: configures the
  28. * {@link ./remote.DriverService remote.DriverService}
  29. * that manages the [MicrosoftEdgeDriver] child process.
  30. *
  31. * 2. {@linkplain Options}: defines configuration options for each new
  32. * MicrosoftEdgeDriver session, such as which
  33. * {@linkplain Options#setProxy proxy} to use when starting the browser.
  34. *
  35. * 3. {@linkplain Driver}: the WebDriver client; each new instance will control
  36. * a unique browser session.
  37. *
  38. * __Customizing the MicrosoftEdgeDriver Server__ <a id="custom-server"></a>
  39. *
  40. * By default, every MicrosoftEdge session will use a single driver service,
  41. * which is started the first time a {@link Driver} instance is created and
  42. * terminated when this process exits. The default service will inherit its
  43. * environment from the current process.
  44. * You may obtain a handle to this default service using
  45. * {@link #getDefaultService getDefaultService()} and change its configuration
  46. * with {@link #setDefaultService setDefaultService()}.
  47. *
  48. * You may also create a {@link Driver} with its own driver service. This is
  49. * useful if you need to capture the server's log output for a specific session:
  50. *
  51. * var edge = require('selenium-webdriver/edge');
  52. *
  53. * var service = new edge.ServiceBuilder()
  54. * .setPort(55555)
  55. * .build();
  56. *
  57. * var options = new edge.Options();
  58. * // configure browser options ...
  59. *
  60. * var driver = edge.Driver.createSession(options, service);
  61. *
  62. * Users should only instantiate the {@link Driver} class directly when they
  63. * need a custom driver service configuration (as shown above). For normal
  64. * operation, users should start MicrosoftEdge using the
  65. * {@link ./builder.Builder selenium-webdriver.Builder}.
  66. *
  67. * [MicrosoftEdgeDriver]: https://msdn.microsoft.com/en-us/library/mt188085(v=vs.85).aspx
  68. */
  69. 'use strict';
  70. const fs = require('fs'),
  71. util = require('util');
  72. const http = require('./http'),
  73. io = require('./io'),
  74. capabilities = require('./lib/capabilities'),
  75. promise = require('./lib/promise'),
  76. Symbols = require('./lib/symbols'),
  77. webdriver = require('./lib/webdriver'),
  78. portprober = require('./net/portprober'),
  79. remote = require('./remote');
  80. const EDGEDRIVER_EXE = 'MicrosoftWebDriver.exe';
  81. /**
  82. * Option keys.
  83. * @enum {string}
  84. */
  85. const CAPABILITY_KEY = {
  86. PAGE_LOAD_STRATEGY: 'pageLoadStrategy'
  87. };
  88. /**
  89. * Class for managing MicrosoftEdgeDriver specific options.
  90. */
  91. class Options {
  92. constructor() {
  93. /** @private {!Object} */
  94. this.options_ = {};
  95. /** @private {?capabilities.ProxyConfig} */
  96. this.proxy_ = null;
  97. }
  98. /**
  99. * Extracts the MicrosoftEdgeDriver specific options from the given
  100. * capabilities object.
  101. * @param {!capabilities.Capabilities} caps The capabilities object.
  102. * @return {!Options} The MicrosoftEdgeDriver options.
  103. */
  104. static fromCapabilities(caps) {
  105. var options = new Options();
  106. var map = options.options_;
  107. Object.keys(CAPABILITY_KEY).forEach(function(key) {
  108. key = CAPABILITY_KEY[key];
  109. if (caps.has(key)) {
  110. map[key] = caps.get(key);
  111. }
  112. });
  113. if (caps.has(capabilities.Capability.PROXY)) {
  114. options.setProxy(caps.get(capabilities.Capability.PROXY));
  115. }
  116. return options;
  117. }
  118. /**
  119. * Sets the proxy settings for the new session.
  120. * @param {capabilities.ProxyConfig} proxy The proxy configuration to use.
  121. * @return {!Options} A self reference.
  122. */
  123. setProxy(proxy) {
  124. this.proxy_ = proxy;
  125. return this;
  126. }
  127. /**
  128. * Sets the page load strategy for Edge.
  129. * Supported values are "normal", "eager", and "none";
  130. *
  131. * @param {string} pageLoadStrategy The page load strategy to use.
  132. * @return {!Options} A self reference.
  133. */
  134. setPageLoadStrategy(pageLoadStrategy) {
  135. this.options_[CAPABILITY_KEY.PAGE_LOAD_STRATEGY] =
  136. pageLoadStrategy.toLowerCase();
  137. return this;
  138. }
  139. /**
  140. * Converts this options instance to a {@link capabilities.Capabilities}
  141. * object.
  142. * @param {capabilities.Capabilities=} opt_capabilities The capabilities to
  143. * merge these options into, if any.
  144. * @return {!capabilities.Capabilities} The capabilities.
  145. */
  146. toCapabilities(opt_capabilities) {
  147. var caps = opt_capabilities || capabilities.Capabilities.edge();
  148. if (this.proxy_) {
  149. caps.set(capabilities.Capability.PROXY, this.proxy_);
  150. }
  151. Object.keys(this.options_).forEach(function(key) {
  152. caps.set(key, this.options_[key]);
  153. }, this);
  154. return caps;
  155. }
  156. /**
  157. * Converts this instance to its JSON wire protocol representation. Note this
  158. * function is an implementation not intended for general use.
  159. * @return {{pageLoadStrategy: (string|undefined)}}
  160. * The JSON wire protocol representation of this instance.
  161. */
  162. [Symbols.serialize]() {
  163. var json = {};
  164. for (var key in this.options_) {
  165. if (this.options_[key] != null) {
  166. json[key] = this.options_[key];
  167. }
  168. }
  169. return json;
  170. }
  171. }
  172. /**
  173. * Creates {@link remote.DriverService} instances that manage a
  174. * MicrosoftEdgeDriver server in a child process.
  175. */
  176. class ServiceBuilder extends remote.DriverService.Builder {
  177. /**
  178. * @param {string=} opt_exe Path to the server executable to use. If omitted,
  179. * the builder will attempt to locate the MicrosoftEdgeDriver on the current
  180. * PATH.
  181. * @throws {Error} If provided executable does not exist, or the
  182. * MicrosoftEdgeDriver cannot be found on the PATH.
  183. */
  184. constructor(opt_exe) {
  185. let exe = opt_exe || io.findInPath(EDGEDRIVER_EXE, true);
  186. if (!exe) {
  187. throw Error(
  188. 'The ' + EDGEDRIVER_EXE + ' could not be found on the current PATH. ' +
  189. 'Please download the latest version of the MicrosoftEdgeDriver from ' +
  190. 'https://www.microsoft.com/en-us/download/details.aspx?id=48212 and ' +
  191. 'ensure it can be found on your PATH.');
  192. }
  193. super(exe);
  194. // Binding to the loopback address will fail if not running with
  195. // administrator privileges. Since we cannot test for that in script
  196. // (or can we?), force the DriverService to use "localhost".
  197. this.setHostname('localhost');
  198. }
  199. }
  200. /** @type {remote.DriverService} */
  201. var defaultService = null;
  202. /**
  203. * Sets the default service to use for new MicrosoftEdgeDriver instances.
  204. * @param {!remote.DriverService} service The service to use.
  205. * @throws {Error} If the default service is currently running.
  206. */
  207. function setDefaultService(service) {
  208. if (defaultService && defaultService.isRunning()) {
  209. throw Error(
  210. 'The previously configured EdgeDriver service is still running. ' +
  211. 'You must shut it down before you may adjust its configuration.');
  212. }
  213. defaultService = service;
  214. }
  215. /**
  216. * Returns the default MicrosoftEdgeDriver service. If such a service has
  217. * not been configured, one will be constructed using the default configuration
  218. * for an MicrosoftEdgeDriver executable found on the system PATH.
  219. * @return {!remote.DriverService} The default MicrosoftEdgeDriver service.
  220. */
  221. function getDefaultService() {
  222. if (!defaultService) {
  223. defaultService = new ServiceBuilder().build();
  224. }
  225. return defaultService;
  226. }
  227. /**
  228. * Creates a new WebDriver client for Microsoft's Edge.
  229. */
  230. class Driver extends webdriver.WebDriver {
  231. /**
  232. * Creates a new browser session for Microsoft's Edge browser.
  233. *
  234. * @param {(capabilities.Capabilities|Options)=} opt_config The configuration
  235. * options.
  236. * @param {remote.DriverService=} opt_service The session to use; will use
  237. * the {@linkplain #getDefaultService default service} by default.
  238. * @param {promise.ControlFlow=} opt_flow The control flow to use, or
  239. * {@code null} to use the currently active flow.
  240. * @return {!Driver} A new driver instance.
  241. */
  242. static createSession(opt_config, opt_service, opt_flow) {
  243. var service = opt_service || getDefaultService();
  244. var client = service.start().then(url => new http.HttpClient(url));
  245. var executor = new http.Executor(client);
  246. var caps =
  247. opt_config instanceof Options ? opt_config.toCapabilities() :
  248. (opt_config || capabilities.Capabilities.edge());
  249. return /** @type {!Driver} */(super.createSession(
  250. executor, caps, opt_flow, () => service.kill()));
  251. }
  252. /**
  253. * This function is a no-op as file detectors are not supported by this
  254. * implementation.
  255. * @override
  256. */
  257. setFileDetector() {}
  258. }
  259. // PUBLIC API
  260. exports.Driver = Driver;
  261. exports.Options = Options;
  262. exports.ServiceBuilder = ServiceBuilder;
  263. exports.getDefaultService = getDefaultService;
  264. exports.setDefaultService = setDefaultService;