Path: blob/trunk/javascript/selenium-webdriver/edge.js
2884 views
// Licensed to the Software Freedom Conservancy (SFC) under one1// or more contributor license agreements. See the NOTICE file2// distributed with this work for additional information3// regarding copyright ownership. The SFC licenses this file4// to you under the Apache License, Version 2.0 (the5// "License"); you may not use this file except in compliance6// with the License. You may obtain a copy of the License at7//8// http://www.apache.org/licenses/LICENSE-2.09//10// Unless required by applicable law or agreed to in writing,11// software distributed under the License is distributed on an12// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13// KIND, either express or implied. See the License for the14// specific language governing permissions and limitations15// under the License.1617/**18* @fileoverview Defines a {@linkplain Driver WebDriver} client for19* Microsoft's Edge web browser. Edge (Chromium) is supported and support20* for Edge Legacy (EdgeHTML) as part of https://github.com/SeleniumHQ/selenium/issues/9166.21* Before using this module, you must download and install the correct22* [WebDriver](https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/) server.23*24* Ensure that the msedgedriver (Chromium)25* is on your [PATH](http://en.wikipedia.org/wiki/PATH_%28variable%29).26*27* You may use {@link Options} to specify whether Edge Chromium options should be used:2829* const edge = require('selenium-webdriver/edge');30* const options = new edge.Options();3132* There are three primary classes exported by this module:33*34* 1. {@linkplain ServiceBuilder}: configures the35* {@link ./remote.DriverService remote.DriverService}36* that manages the [WebDriver] child process.37*38* 2. {@linkplain Options}: defines configuration options for each new39* WebDriver session, such as which40* {@linkplain Options#setProxy proxy} to use when starting the browser.41*42* 3. {@linkplain Driver}: the WebDriver client; each new instance will control43* a unique browser session.44*45* __Customizing the WebDriver Server__ <a id="custom-server"></a>46*47* By default, every MicrosoftEdge session will use a single driver service,48* which is started the first time a {@link Driver} instance is created and49* terminated when this process exits. The default service will inherit its50* environment from the current process.51* You may obtain a handle to this default service using52* {@link #getDefaultService getDefaultService()} and change its configuration53* with {@link #setDefaultService setDefaultService()}.54*55* You may also create a {@link Driver} with its own driver service. This is56* useful if you need to capture the server's log output for a specific session:57*58* const edge = require('selenium-webdriver/edge');59*60* const service = new edge.ServiceBuilder()61* .setPort(55555)62* .build();63*64* let options = new edge.Options();65* // configure browser options ...66*67* let driver = edge.Driver.createSession(options, service);68*69* Users should only instantiate the {@link Driver} class directly when they70* need a custom driver service configuration (as shown above). For normal71* operation, users should start msedgedriver using the72* {@link ./builder.Builder selenium-webdriver.Builder}.73*74* [WebDriver (Chromium)]: https://docs.microsoft.com/en-us/microsoft-edge/webdriver-chromium75*76* @module selenium-webdriver/edge77*/7879'use strict'8081const { Browser } = require('./lib/capabilities')82const chromium = require('./chromium')83const EDGE_CAPABILITY_KEY = 'ms:edgeOptions'8485/** @type {remote.DriverService} */8687/**88* Creates {@link selenium-webdriver/remote.DriverService} instances that manage89* a [MSEdgeDriver](https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/)90* server in a child process.91*/92class ServiceBuilder extends chromium.ServiceBuilder {93/**94* @param {string=} opt_exe Path to the server executable to use. If omitted,95* the builder will attempt to locate the msedgedriver on the current96* PATH.97* @throws {Error} If provided executable does not exist, or the msedgedriver98* cannot be found on the PATH.99*/100constructor(opt_exe) {101super(opt_exe)102this.setLoopback(true)103}104}105106/**107* Class for managing edge chromium specific options.108*/109class Options extends chromium.Options {110/**111* Sets the path to the edge binary to use112*113* The binary path be absolute or relative to the msedgedriver server114* executable, but it must exist on the machine that will launch edge chromium.115*116* @param {string} path The path to the msedgedriver binary to use.117* @return {!Options} A self reference.118*/119setEdgeChromiumBinaryPath(path) {120return this.setBinaryPath(path)121}122123/**124* Changes the browser name to 'webview2' to enable125* <a href="https://learn.microsoft.com/en-us/microsoft-edge/webview2/how-to/webdriver">126* test automation of WebView2 apps with Microsoft Edge WebDriver127* </a>128*129* @param {boolean} enable flag to enable or disable the 'webview2' usage130*/131useWebView(enable) {132const browserName = enable ? 'webview2' : Browser.EDGE133return this.setBrowserName(browserName)134}135}136137/**138* Creates a new WebDriver client for Microsoft's Edge.139*/140class Driver extends chromium.Driver {141/**142* Creates a new browser session for Microsoft's Edge browser.143*144* @param {(Capabilities|Options)=} opt_config The configuration options.145* @param {remote.DriverService=} opt_serviceExecutor The service to use; will create146* a new Legacy or Chromium service based on {@linkplain Options} by default.147* @return {!Driver} A new driver instance.148*/149static createSession(opt_config, opt_serviceExecutor) {150let caps = opt_config || new Options()151return /** @type {!Driver} */ (super.createSession(caps, opt_serviceExecutor, 'ms', EDGE_CAPABILITY_KEY))152}153154/**155* returns new instance of edge driver service156* @returns {remote.DriverService}157*/158static getDefaultService() {159return new ServiceBuilder().build()160}161162/**163* This function is a no-op as file detectors are not supported by this164* implementation.165* @override166*/167setFileDetector() {}168}169170Options.prototype.BROWSER_NAME_VALUE = Browser.EDGE171Options.prototype.CAPABILITY_KEY = EDGE_CAPABILITY_KEY172173// PUBLIC API174175module.exports = {176Driver,177Options,178ServiceBuilder,179}180181182