# 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.1415var base = require('./_base'),16    executors = require('./executors');1718var goog = base.require('goog'),19    AbstractBuilder = base.require('webdriver.AbstractBuilder'),20    Browser = base.require('webdriver.Browser'),21    Capability = base.require('webdriver.Capability'),22    WebDriver = base.require('webdriver.WebDriver'),23    promise = base.require('webdriver.promise');242526/**27 * @param {!webdriver.Capabilities} capabilities The desired capabilities.28 * @return {webdriver.WebDriver} A new WebDriver instance or {@code null}29 *     if the requested browser is not natively supported in Node.30 */31function createNativeDriver(capabilities) {32  switch (capabilities.get(Capability.BROWSER_NAME)) {33    case Browser.CHROME:34      // Requiring 'chrome' above would create a cycle:35      // index -> builder -> chrome -> index36      var chrome = require('./chrome');37      return chrome.createDriver(capabilities);3839    case Browser.PHANTOM_JS:40      // Requiring 'phantomjs' would create a cycle:41      // index -> builder -> phantomjs -> index42      var phantomjs = require('./phantomjs');43      return phantomjs.createDriver(capabilities);4445    default:46      return null;47  }48}49505152/**53 * Creates new {@link webdriver.WebDriver WebDriver} instances.54 * @constructor55 * @extends {webdriver.AbstractBuilder}56 */57var Builder = function() {58  goog.base(this);59};60goog.inherits(Builder, AbstractBuilder);616263/**64 * Sets the proxy configuration to use for WebDriver clients created by this65 * builder. Any calls to {@link #withCapabilities} after this function will66 * overwrite these settings.67 * @param {!proxy.ProxyConfig} config The configuration to use.68 * @return {!Builder} A self reference.69 */70Builder.prototype.setProxy = function(config) {71  this.getCapabilities().set(Capability.PROXY, config);72  return this;73};747576/**77 * Sets Chrome-specific options for drivers created by this builder.78 * @param {!chrome.Options} options The ChromeDriver options to use.79 * @return {!Builder} A self reference.80 */81Builder.prototype.setChromeOptions = function(options) {82  var newCapabilities = options.toCapabilities(this.getCapabilities());83  return /** @type {!Builder} */(this.withCapabilities(newCapabilities));84};858687/**88 * @override89 */90Builder.prototype.build = function() {91  var url = this.getServerUrl();9293  // If a remote server wasn't specified, check for browsers we support94  // natively in node before falling back to using the java Selenium server.95  if (!url) {96    var driver = createNativeDriver(this.getCapabilities());97    if (driver) {98      return driver;99    }100101    // Nope, fall-back to using the default java server.102    url = AbstractBuilder.DEFAULT_SERVER_URL;103  }104105  var executor = executors.createExecutor(url);106  return WebDriver.createSession(executor, this.getCapabilities());107};108109110// PUBLIC API111112113exports.Builder = Builder;