| 1 | // Copyright 2012 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 | goog.provide('webdriver.AbstractBuilder'); |
| 16 | |
| 17 | goog.require('webdriver.Capabilities'); |
| 18 | goog.require('webdriver.process'); |
| 19 | |
| 20 | |
| 21 | |
| 22 | /** |
| 23 | * Creates new {@code webdriver.WebDriver} clients. Upon instantiation, each |
| 24 | * Builder will configure itself based on the following environment variables: |
| 25 | * <dl> |
| 26 | * <dt>{@code webdriver.AbstractBuilder.SERVER_URL_ENV}</dt> |
| 27 | * <dd>Defines the remote WebDriver server that should be used for command |
| 28 | * command execution; may be overridden using |
| 29 | * {@code webdriver.AbstractBuilder.prototype.usingServer}.</dd> |
| 30 | * </dl> |
| 31 | * @constructor |
| 32 | */ |
| 33 | webdriver.AbstractBuilder = function() { |
| 34 | |
| 35 | /** |
| 36 | * URL of the remote server to use for new clients; initialized from the |
| 37 | * value of the {@link webdriver.AbstractBuilder.SERVER_URL_ENV} environment |
| 38 | * variable, but may be overridden using |
| 39 | * {@link webdriver.AbstractBuilder#usingServer}. |
| 40 | * @private {string} |
| 41 | */ |
| 42 | this.serverUrl_ = webdriver.process.getEnv( |
| 43 | webdriver.AbstractBuilder.SERVER_URL_ENV); |
| 44 | |
| 45 | /** |
| 46 | * The desired capabilities to use when creating a new session. |
| 47 | * @private {!webdriver.Capabilities} |
| 48 | */ |
| 49 | this.capabilities_ = new webdriver.Capabilities(); |
| 50 | }; |
| 51 | |
| 52 | |
| 53 | /** |
| 54 | * Environment variable that defines the URL of the WebDriver server that |
| 55 | * should be used for all new WebDriver clients. This setting may be overridden |
| 56 | * using {@code #usingServer(url)}. |
| 57 | * @type {string} |
| 58 | * @const |
| 59 | * @see webdriver.process.getEnv |
| 60 | */ |
| 61 | webdriver.AbstractBuilder.SERVER_URL_ENV = 'wdurl'; |
| 62 | |
| 63 | |
| 64 | /** |
| 65 | * The default URL of the WebDriver server to use if |
| 66 | * {@link webdriver.AbstractBuilder.SERVER_URL_ENV} is not set. |
| 67 | * @type {string} |
| 68 | * @const |
| 69 | */ |
| 70 | webdriver.AbstractBuilder.DEFAULT_SERVER_URL = 'http://localhost:4444/wd/hub'; |
| 71 | |
| 72 | |
| 73 | /** |
| 74 | * Configures which WebDriver server should be used for new sessions. Overrides |
| 75 | * the value loaded from the {@link webdriver.AbstractBuilder.SERVER_URL_ENV} |
| 76 | * upon creation of this instance. |
| 77 | * @param {string} url URL of the server to use. |
| 78 | * @return {!webdriver.AbstractBuilder} This Builder instance for chain calling. |
| 79 | */ |
| 80 | webdriver.AbstractBuilder.prototype.usingServer = function(url) { |
| 81 | this.serverUrl_ = url; |
| 82 | return this; |
| 83 | }; |
| 84 | |
| 85 | |
| 86 | /** |
| 87 | * @return {string} The URL of the WebDriver server this instance is configured |
| 88 | * to use. |
| 89 | */ |
| 90 | webdriver.AbstractBuilder.prototype.getServerUrl = function() { |
| 91 | return this.serverUrl_; |
| 92 | }; |
| 93 | |
| 94 | |
| 95 | /** |
| 96 | * Sets the desired capabilities when requesting a new session. This will |
| 97 | * overwrite any previously set desired capabilities. |
| 98 | * @param {!(Object|webdriver.Capabilities)} capabilities The desired |
| 99 | * capabilities for a new session. |
| 100 | * @return {!webdriver.AbstractBuilder} This Builder instance for chain calling. |
| 101 | */ |
| 102 | webdriver.AbstractBuilder.prototype.withCapabilities = function(capabilities) { |
| 103 | this.capabilities_ = new webdriver.Capabilities(capabilities); |
| 104 | return this; |
| 105 | }; |
| 106 | |
| 107 | |
| 108 | /** |
| 109 | * @return {!webdriver.Capabilities} The current desired capabilities for this |
| 110 | * builder. |
| 111 | */ |
| 112 | webdriver.AbstractBuilder.prototype.getCapabilities = function() { |
| 113 | return this.capabilities_; |
| 114 | }; |
| 115 | |
| 116 | |
| 117 | /** |
| 118 | * Builds a new {@link webdriver.WebDriver} instance using this builder's |
| 119 | * current configuration. |
| 120 | * @return {!webdriver.WebDriver} A new WebDriver client. |
| 121 | */ |
| 122 | webdriver.AbstractBuilder.prototype.build = goog.abstractMethod; |