# lib/webdriver/builder.js

    1// Copyright 2011 Software Freedom Conservancy. All Rights Reserved.2//3// Licensed under the Apache License, Version 2.0 (the "License");4// you may not use this file except in compliance with the License.5// You may obtain a copy of the License at6//7//      http://www.apache.org/licenses/LICENSE-2.08//9// Unless required by applicable law or agreed to in writing, software10// distributed under the License is distributed on an "AS IS" BASIS,11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12// See the License for the specific language governing permissions and13// limitations under the License.1415goog.provide('webdriver.Builder');1617goog.require('goog.userAgent');18goog.require('webdriver.AbstractBuilder');19goog.require('webdriver.FirefoxDomExecutor');20goog.require('webdriver.WebDriver');21goog.require('webdriver.http.CorsClient');22goog.require('webdriver.http.Executor');23goog.require('webdriver.http.XhrClient');24goog.require('webdriver.process');25262728/**29 * @constructor30 * @extends {webdriver.AbstractBuilder}31 */32webdriver.Builder = function() {33  goog.base(this);3435  /**36   * ID of an existing WebDriver session that new clients should use.37   * Initialized from the value of the38   * {@link webdriver.AbstractBuilder.SESSION_ID_ENV} environment variable, but39   * may be overridden using40   * {@link webdriver.AbstractBuilder#usingSession}.41   * @private {string}42   */43  this.sessionId_ =44      webdriver.process.getEnv(webdriver.Builder.SESSION_ID_ENV);45};46goog.inherits(webdriver.Builder, webdriver.AbstractBuilder);474849/**50 * Environment variable that defines the session ID of an existing WebDriver51 * session to use when creating clients. If set, all new Builder instances will52 * default to creating clients that use this session. To create a new session,53 * use {@code #useExistingSession(boolean)}. The use of this environment54 * variable requires that {@link webdriver.AbstractBuilder.SERVER_URL_ENV} also55 * be set.56 * @type {string}57 * @const58 * @see webdriver.process.getEnv59 */60webdriver.Builder.SESSION_ID_ENV = 'wdsid';616263/**64 * Configures the builder to create a client that will use an existing WebDriver65 * session.66 * @param {string} id The existing session ID to use.67 * @return {!webdriver.AbstractBuilder} This Builder instance for chain calling.68 */69webdriver.Builder.prototype.usingSession = function(id) {70  this.sessionId_ = id;71  return this;72};737475/**76 * @return {string} The ID of the session, if any, this builder is configured77 *     to reuse.78 */79webdriver.Builder.prototype.getSession = function() {80  return this.sessionId_;81};828384/**85 * @override86 */87webdriver.Builder.prototype.build = function() {88  if (goog.userAgent.GECKO && document.readyState != 'complete') {89    throw Error('Cannot create driver instance before window.onload');90  }9192  var executor;9394  if (webdriver.FirefoxDomExecutor.isAvailable()) {95    executor = new webdriver.FirefoxDomExecutor();96    return webdriver.WebDriver.createSession(executor, this.getCapabilities());97  } else {98    var url = this.getServerUrl() ||99        webdriver.AbstractBuilder.DEFAULT_SERVER_URL;100    var client;101    if (url[0] == '/') {102      var origin = window.location.origin ||103          (window.location.protocol + '//' + window.location.host);104      client = new webdriver.http.XhrClient(origin + url);105    } else {106      client = new webdriver.http.CorsClient(url);107    }108    executor = new webdriver.http.Executor(client);109110    if (this.getSession()) {111      return webdriver.WebDriver.attachToSession(executor, this.getSession());112    } else {113      throw new Error('Unable to create a new client for this browser. The ' +114          'WebDriver session ID has not been defined.');115    }116  }117};