forked from SeleniumHQ/selenium
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuilder.js
More file actions
184 lines (155 loc) · 5.57 KB
/
Copy pathbuilder.js
File metadata and controls
184 lines (155 loc) · 5.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
goog.provide('webdriver.Builder');
goog.require('goog.Uri');
goog.require('goog.userAgent');
goog.require('webdriver.Capabilities');
goog.require('webdriver.FirefoxDomExecutor');
goog.require('webdriver.WebDriver');
goog.require('webdriver.http.CorsClient');
goog.require('webdriver.http.Executor');
goog.require('webdriver.http.XhrClient');
/**
* Creates new {@code webdriver.WebDriver} clients for use in a browser
* environment. Upon instantiation, each Builder will configure itself based
* on the following query parameters:
* <dl>
* <dt>wdurl
* <dd>Defines the WebDriver server to send commands to. If this is a
* relative URL, the builder will use the standard WebDriver wire
* protocol and a {@link webdriver.http.XhrClient}. Otherwise, it will
* use a {@link webdriver.http.CorsClient}; this only works when
* connecting to an instance of the Java Selenium server. The server URL
* may be changed using {@code #usingServer}.
*
* <dt>wdsid
* <dd>Defines the session to connect to. If omitted, will request a new
* session from the server.
* </dl>
*
* @param {Window=} opt_window The window to extract query parameters from.
* @constructor
* @final
* @struct
*/
webdriver.Builder = function(opt_window) {
var win = opt_window || window;
var data = new goog.Uri(win.location).getQueryData();
/** @private {string} */
this.serverUrl_ =
/** @type {string} */ (data.get(webdriver.Builder.SERVER_URL_PARAM,
webdriver.Builder.DEFAULT_SERVER_URL));
/** @private {string} */
this.sessionId_ =
/** @type {string} */ (data.get(webdriver.Builder.SESSION_ID_PARAM));
/** @private {!webdriver.Capabilities} */
this.capabilities_ = new webdriver.Capabilities();
};
/**
* Query parameter that defines which session to connect to.
* @type {string}
* @const
*/
webdriver.Builder.SESSION_ID_PARAM = 'wdsid';
/**
* Query parameter that defines the URL of the remote server to connect to.
* @type {string}
* @const
*/
webdriver.Builder.SERVER_URL_PARAM = 'wdurl';
/**
* The default server URL to use.
* @type {string}
* @const
*/
webdriver.Builder.DEFAULT_SERVER_URL = 'http://localhost:4444/wd/hub';
/**
* Configures which WebDriver server should be used for new sessions.
* @param {string} url URL of the server to use.
* @return {!webdriver.Builder} This Builder instance for chain calling.
*/
webdriver.Builder.prototype.usingServer = function(url) {
this.serverUrl_ = url;
return this;
};
/**
* @return {string} The URL of the WebDriver server this instance is configured
* to use.
*/
webdriver.Builder.prototype.getServerUrl = function() {
return this.serverUrl_;
};
/**
* Configures the builder to create a client that will use an existing WebDriver
* session.
* @param {string} id The existing session ID to use.
* @return {!webdriver.Builder} This Builder instance for chain calling.
*/
webdriver.Builder.prototype.usingSession = function(id) {
this.sessionId_ = id;
return this;
};
/**
* @return {string} The ID of the session, if any, this builder is configured
* to reuse.
*/
webdriver.Builder.prototype.getSession = function() {
return this.sessionId_;
};
/**
* Sets the desired capabilities when requesting a new session. This will
* overwrite any previously set desired capabilities.
* @param {!(Object|webdriver.Capabilities)} capabilities The desired
* capabilities for a new session.
* @return {!webdriver.Builder} This Builder instance for chain calling.
*/
webdriver.Builder.prototype.withCapabilities = function(capabilities) {
this.capabilities_ = new webdriver.Capabilities(capabilities);
return this;
};
/**
* Builds a new {@link webdriver.WebDriver} instance using this builder's
* current configuration.
* @return {!webdriver.WebDriver} A new WebDriver client.
*/
webdriver.Builder.prototype.build = function() {
if (goog.userAgent.GECKO && document.readyState != 'complete') {
throw Error('Cannot create driver instance before window.onload');
}
var executor;
if (webdriver.FirefoxDomExecutor.isAvailable()) {
executor = new webdriver.FirefoxDomExecutor();
return webdriver.WebDriver.createSession(executor, this.capabilities_);
} else {
var url = this.serverUrl_;
var client;
if (url[0] == '/') {
var origin = window.location.origin ||
(window.location.protocol + '//' + window.location.host);
client = new webdriver.http.XhrClient(origin + url);
} else {
client = new webdriver.http.CorsClient(url);
}
executor = new webdriver.http.Executor(client);
if (this.sessionId_) {
return webdriver.WebDriver.attachToSession(executor, this.sessionId_);
} else {
throw new Error('Unable to create a new client for this browser. The ' +
'WebDriver session ID has not been defined.');
}
}
};