Path: blob/trunk/javascript/selenium-webdriver/safari.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 WebDriver client for Safari.19*20* @module selenium-webdriver/safari21*/2223'use strict'2425const http = require('./http')26const remote = require('./remote')27const webdriver = require('./lib/webdriver')28const { Browser, Capabilities } = require('./lib/capabilities')29const { getBinaryPaths } = require('./common/driverFinder')3031/**32* Creates {@link remote.DriverService} instances that manage33* a [safaridriver] server in a child process.34*35* [safaridriver]: https://developer.apple.com/library/prerelease/content/releasenotes/General/WhatsNewInSafari/Articles/Safari_10_0.html#//apple_ref/doc/uid/TP40014305-CH11-DontLinkElementID_2836*/37class ServiceBuilder extends remote.DriverService.Builder {38/**39* @param {string=} opt_exe Path to the server executable to use. If omitted,40* the builder will attempt to locate the safaridriver on the system PATH.41*/42constructor(opt_exe) {43super(opt_exe)44this.setLoopback(true) // Required.45}46}4748const OPTIONS_CAPABILITY_KEY = 'safari:options'49const TECHNOLOGY_PREVIEW_OPTIONS_KEY = 'technologyPreview'5051/**52* Configuration options specific to the {@link Driver SafariDriver}.53*/54class Options extends Capabilities {55/**56* @param {(Capabilities|Map<string, ?>|Object)=} other Another set of57* capabilities to initialize this instance from.58*/59constructor(other = undefined) {60super(other)6162/** @private {!Object} */63this.options_ = this.get(OPTIONS_CAPABILITY_KEY) || {}6465this.set(OPTIONS_CAPABILITY_KEY, this.options_)66this.setBrowserName(Browser.SAFARI)67}6869/**70* Instruct the SafariDriver to use the Safari Technology Preview if true.71* Otherwise, use the release version of Safari. Defaults to using the release version of Safari.72*73* @param {boolean} useTechnologyPreview74* @return {!Options} A self reference.75*/76setTechnologyPreview(useTechnologyPreview) {77this.options_[TECHNOLOGY_PREVIEW_OPTIONS_KEY] = !!useTechnologyPreview78return this79}8081/**82* Enables diagnostic logging for Safari.83*84* This method sets the `safari:diagnose` option to `true` in the current configuration.85* It is used to enable additional logging or diagnostic features specific to Safari.86*87* @returns {Options} Returns the current instance88*/89enableLogging() {90this.set('safari:diagnose', true)91return this92}93}9495/**96* @param {(Capabilities|Object<string, *>)=} o The options object97* @return {boolean}98*/99function useTechnologyPreview(o) {100if (o instanceof Capabilities) {101let options = o.get(OPTIONS_CAPABILITY_KEY)102return !!(options && options[TECHNOLOGY_PREVIEW_OPTIONS_KEY])103}104105if (o && typeof o === 'object') {106return !!o[TECHNOLOGY_PREVIEW_OPTIONS_KEY]107}108109return false110}111112const SAFARIDRIVER_TECHNOLOGY_PREVIEW_EXE = '/Applications/Safari Technology Preview.app/Contents/MacOS/safaridriver'113114/**115* A WebDriver client for Safari. This class should never be instantiated116* directly; instead, use the {@linkplain ./builder.Builder Builder}:117*118* var driver = new Builder()119* .forBrowser('safari')120* .build();121*122*/123class Driver extends webdriver.WebDriver {124/**125* Creates a new Safari session.126*127* @param {(Options|Capabilities)=} options The configuration options.128* @return {!Driver} A new driver instance.129*/130static createSession(options) {131let caps = options || new Options()132133let exe134if (useTechnologyPreview(caps.get(OPTIONS_CAPABILITY_KEY))) {135exe = SAFARIDRIVER_TECHNOLOGY_PREVIEW_EXE136}137138let service = new ServiceBuilder(exe).build()139if (!service.getExecutable()) {140service.setExecutable(getBinaryPaths(caps).driverPath)141}142let executor = new http.Executor(service.start().then((url) => new http.HttpClient(url)))143144return /** @type {!Driver} */ (super.createSession(executor, caps, () => service.kill()))145}146}147148// Public API149150exports.Driver = Driver151exports.Options = Options152exports.ServiceBuilder = ServiceBuilder153154155