| 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 | goog.provide('webdriver.Builder'); |
| 16 | |
| 17 | goog.require('goog.userAgent'); |
| 18 | goog.require('webdriver.AbstractBuilder'); |
| 19 | goog.require('webdriver.FirefoxDomExecutor'); |
| 20 | goog.require('webdriver.WebDriver'); |
| 21 | goog.require('webdriver.http.CorsClient'); |
| 22 | goog.require('webdriver.http.Executor'); |
| 23 | goog.require('webdriver.http.XhrClient'); |
| 24 | goog.require('webdriver.process'); |
| 25 | |
| 26 | |
| 27 | |
| 28 | /** |
| 29 | * @constructor |
| 30 | * @extends {webdriver.AbstractBuilder} |
| 31 | */ |
| 32 | webdriver.Builder = function() { |
| 33 | goog.base(this); |
| 34 | |
| 35 | /** |
| 36 | * ID of an existing WebDriver session that new clients should use. |
| 37 | * Initialized from the value of the |
| 38 | * {@link webdriver.AbstractBuilder.SESSION_ID_ENV} environment variable, but |
| 39 | * may be overridden using |
| 40 | * {@link webdriver.AbstractBuilder#usingSession}. |
| 41 | * @private {string} |
| 42 | */ |
| 43 | this.sessionId_ = |
| 44 | webdriver.process.getEnv(webdriver.Builder.SESSION_ID_ENV); |
| 45 | }; |
| 46 | goog.inherits(webdriver.Builder, webdriver.AbstractBuilder); |
| 47 | |
| 48 | |
| 49 | /** |
| 50 | * Environment variable that defines the session ID of an existing WebDriver |
| 51 | * session to use when creating clients. If set, all new Builder instances will |
| 52 | * default to creating clients that use this session. To create a new session, |
| 53 | * use {@code #useExistingSession(boolean)}. The use of this environment |
| 54 | * variable requires that {@link webdriver.AbstractBuilder.SERVER_URL_ENV} also |
| 55 | * be set. |
| 56 | * @type {string} |
| 57 | * @const |
| 58 | * @see webdriver.process.getEnv |
| 59 | */ |
| 60 | webdriver.Builder.SESSION_ID_ENV = 'wdsid'; |
| 61 | |
| 62 | |
| 63 | /** |
| 64 | * Configures the builder to create a client that will use an existing WebDriver |
| 65 | * session. |
| 66 | * @param {string} id The existing session ID to use. |
| 67 | * @return {!webdriver.AbstractBuilder} This Builder instance for chain calling. |
| 68 | */ |
| 69 | webdriver.Builder.prototype.usingSession = function(id) { |
| 70 | this.sessionId_ = id; |
| 71 | return this; |
| 72 | }; |
| 73 | |
| 74 | |
| 75 | /** |
| 76 | * @return {string} The ID of the session, if any, this builder is configured |
| 77 | * to reuse. |
| 78 | */ |
| 79 | webdriver.Builder.prototype.getSession = function() { |
| 80 | return this.sessionId_; |
| 81 | }; |
| 82 | |
| 83 | |
| 84 | /** |
| 85 | * @override |
| 86 | */ |
| 87 | webdriver.Builder.prototype.build = function() { |
| 88 | if (goog.userAgent.GECKO && document.readyState != 'complete') { |
| 89 | throw Error('Cannot create driver instance before window.onload'); |
| 90 | } |
| 91 | |
| 92 | var executor; |
| 93 | |
| 94 | 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); |
| 109 | |
| 110 | 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 | }; |