| 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 at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // 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 and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | var base = require('./_base'), |
| 16 | executors = require('./executors'); |
| 17 | |
| 18 | var 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'); |
| 24 | |
| 25 | |
| 26 | /** |
| 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 | */ |
| 31 | function 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 -> index |
| 36 | var chrome = require('./chrome'); |
| 37 | return chrome.createDriver(capabilities); |
| 38 | |
| 39 | case Browser.PHANTOM_JS: |
| 40 | // Requiring 'phantomjs' would create a cycle: |
| 41 | // index -> builder -> phantomjs -> index |
| 42 | var phantomjs = require('./phantomjs'); |
| 43 | return phantomjs.createDriver(capabilities); |
| 44 | |
| 45 | default: |
| 46 | return null; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | |
| 51 | |
| 52 | /** |
| 53 | * Creates new {@link webdriver.WebDriver WebDriver} instances. |
| 54 | * @constructor |
| 55 | * @extends {webdriver.AbstractBuilder} |
| 56 | */ |
| 57 | var Builder = function() { |
| 58 | goog.base(this); |
| 59 | }; |
| 60 | goog.inherits(Builder, AbstractBuilder); |
| 61 | |
| 62 | |
| 63 | /** |
| 64 | * Sets the proxy configuration to use for WebDriver clients created by this |
| 65 | * builder. Any calls to {@link #withCapabilities} after this function will |
| 66 | * overwrite these settings. |
| 67 | * @param {!proxy.ProxyConfig} config The configuration to use. |
| 68 | * @return {!Builder} A self reference. |
| 69 | */ |
| 70 | Builder.prototype.setProxy = function(config) { |
| 71 | this.getCapabilities().set(Capability.PROXY, config); |
| 72 | return this; |
| 73 | }; |
| 74 | |
| 75 | |
| 76 | /** |
| 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 | */ |
| 81 | Builder.prototype.setChromeOptions = function(options) { |
| 82 | var newCapabilities = options.toCapabilities(this.getCapabilities()); |
| 83 | return /** @type {!Builder} */(this.withCapabilities(newCapabilities)); |
| 84 | }; |
| 85 | |
| 86 | |
| 87 | /** |
| 88 | * @override |
| 89 | */ |
| 90 | Builder.prototype.build = function() { |
| 91 | var url = this.getServerUrl(); |
| 92 | |
| 93 | // If a remote server wasn't specified, check for browsers we support |
| 94 | // 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 | } |
| 100 | |
| 101 | // Nope, fall-back to using the default java server. |
| 102 | url = AbstractBuilder.DEFAULT_SERVER_URL; |
| 103 | } |
| 104 | |
| 105 | var executor = executors.createExecutor(url); |
| 106 | return WebDriver.createSession(executor, this.getCapabilities()); |
| 107 | }; |
| 108 | |
| 109 | |
| 110 | // PUBLIC API |
| 111 | |
| 112 | |
| 113 | exports.Builder = Builder; |