# firefox/index.js

    1// Copyright 2014 Selenium committers2// Copyright 2014 Software Freedom Conservancy3//4// Licensed under the Apache License, Version 2.0 (the "License");5// you may not use this file except in compliance with the License.6//     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, software11// distributed under the License is distributed on an "AS IS" BASIS,12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13// See the License for the specific language governing permissions and14// limitations under the License.1516'use strict';1718var url = require('url'),19    util = require('util');2021var Binary = require('./binary').Binary,22    Profile = require('./profile').Profile,23    decodeProfile = require('./profile').decode,24    webdriver = require('..'),25    executors = require('../executors'),26    httpUtil = require('../http/util'),27    net = require('../net'),28    portprober = require('../net/portprober');293031/**32 * Configuration options for the FirefoxDriver.33 * @constructor34 */35var Options = function() {36  /** @private {Profile} */37  this.profile_ = null;3839  /** @private {Binary} */40  this.binary_ = null;4142  /** @private {webdriver.logging.Preferences} */43  this.logPrefs_ = null;4445  /** @private {webdriver.ProxyConfig} */46  this.proxy_ = null;47};484950/**51 * Sets the profile to use. The profile may be specified as a52 * {@link Profile} object or as the path to an existing Firefox profile to use53 * as a template.54 *55 * @param {(string|!Profile)} profile The profile to use.56 * @return {!Options} A self reference.57 */58Options.prototype.setProfile = function(profile) {59  if (typeof profile === 'string') {60    profile = new Profile(profile);61  }62  this.profile_ = profile;63  return this;64};656667/**68 * Sets the binary to use. The binary may be specified as the path to a Firefox69 * executable, or as a {@link Binary} object.70 *71 * @param {(string|!Binary)} binary The binary to use.72 * @return {!Options} A self reference.73 */74Options.prototype.setBinary = function(binary) {75  if (typeof binary === 'string') {76    binary = new Binary(binary);77  }78  this.binary_ = binary;79  return this;80};818283/**84 * Sets the logging preferences for the new session.85 * @param {webdriver.logging.Preferences} prefs The logging preferences.86 * @return {!Options} A self reference.87 */88Options.prototype.setLoggingPreferences = function(prefs) {89  this.logPrefs_ = prefs;90  return this;91};929394/**95 * Sets the proxy to use.96 *97 * @param {webdriver.ProxyConfig} proxy The proxy configuration to use.98 * @return {!Options} A self reference.99 */100Options.prototype.setProxy = function(proxy) {101  this.proxy_ = proxy;102  return this;103};104105106/**107 * Converts these options to a {@link webdriver.Capabilities} instance.108 *109 * @return {!webdriver.Capabilities} A new capabilities object.110 */111Options.prototype.toCapabilities = function(opt_remote) {112  var caps = webdriver.Capabilities.firefox();113  if (this.logPrefs_) {114    caps.set(webdriver.Capability.LOGGING_PREFS, this.logPrefs_);115  }116  if (this.proxy_) {117    caps.set(webdriver.Capability.PROXY, this.proxy_);118  }119  if (this.binary_) {120    caps.set('firefox_binary', this.binary_);121  }122  if (this.profile_) {123    caps.set('firefox_profile', this.profile_);124  }125  return caps;126};127128129/**130 * A WebDriver client for Firefox.131 *132 * @param {(Options|webdriver.Capabilities|Object)=} opt_config The133 *    configuration options for this driver, specified as either an134 *    {@link Options} or {@link webdriver.Capabilities}, or as a raw hash135 *    object.136 * @param {webdriver.promise.ControlFlow=} opt_flow The flow to137 *     schedule commands through. Defaults to the active flow object.138 * @constructor139 * @extends {webdriver.WebDriver}140 */141var Driver = function(opt_config, opt_flow) {142  var caps;143  if (opt_config instanceof Options) {144    caps = opt_config.toCapabilities();145  } else {146    caps = new webdriver.Capabilities(opt_config);147  }148149  var binary = caps.get('firefox_binary') || new Binary();150  if (typeof binary === 'string') {151    binary = new Binary(binary);152  }153154  var profile = caps.get('firefox_profile') || new Profile();155156  caps.set('firefox_binary', null);157  caps.set('firefox_profile', null);158159  var serverUrl = portprober.findFreePort().then(function(port) {160    var prepareProfile;161    if (typeof profile === 'string') {162      prepareProfile = decodeProfile(profile).then(function(dir) {163        var profile = new Profile(dir);164        profile.setPreference('webdriver_firefox_port', port);165        return profile.writeToDisk();166      });167    } else {168      profile.setPreference('webdriver_firefox_port', port);169      prepareProfile = profile.writeToDisk();170    }171172    return prepareProfile.then(function(dir) {173      return binary.launch(dir);174    }).then(function() {175      var serverUrl = url.format({176        protocol: 'http',177        hostname: net.getLoopbackAddress(),178        port: port,179        pathname: '/hub'180      });181182      return httpUtil.waitForServer(serverUrl, 45 * 1000).then(function() {183        return serverUrl;184      });185    });186  });187188  var executor = executors.createExecutor(serverUrl);189  var driver = webdriver.WebDriver.createSession(executor, caps, opt_flow);190191  webdriver.WebDriver.call(this, driver.getSession(), executor, opt_flow);192};193util.inherits(Driver, webdriver.WebDriver);194195196// PUBLIC API197198199exports.Binary = Binary;200exports.Driver = Driver;201exports.Options = Options;202exports.Profile = Profile;