Skip to content

Commit ec627ef

Browse files
dbojleyba
authored andcommitted
[js] Use browser CORS, if available.
Fixes #485 Signed-off-by: Jason Leyba <[email protected]>
1 parent 6f34f2a commit ec627ef

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

Diff for: javascript/webdriver/builder.js

+25-1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ webdriver.Builder = function(opt_window) {
6464
this.sessionId_ =
6565
/** @type {string} */ (data.get(webdriver.Builder.SESSION_ID_PARAM));
6666

67+
/** @private {boolean} */
68+
this.useBrowserCors_ =
69+
/** @type {boolean} */ (data.containsKey(webdriver.Builder.USE_BROWSER_CORS));
70+
6771
/** @private {!webdriver.Capabilities} */
6872
this.capabilities_ = new webdriver.Capabilities();
6973
};
@@ -93,6 +97,24 @@ webdriver.Builder.SERVER_URL_PARAM = 'wdurl';
9397
webdriver.Builder.DEFAULT_SERVER_URL = 'http://localhost:4444/wd/hub';
9498

9599

100+
/**
101+
* Query parameter that defines whether browser CORS support should be used,
102+
* if available.
103+
* @type {string}
104+
* @const
105+
*/
106+
webdriver.Builder.USE_BROWSER_CORS = 'wdcors';
107+
108+
109+
/**
110+
* Configures the WebDriver to use browser's CORS, if available.
111+
* @return {!webdriver.Builder} This Builder instance for chain calling.
112+
*/
113+
webdriver.Builder.prototype.useBrowserCors = function() {
114+
this.useBrowserCors_ = true;
115+
return this;
116+
};
117+
96118
/**
97119
* Configures which WebDriver server should be used for new sessions.
98120
* @param {string} url URL of the server to use.
@@ -165,7 +187,9 @@ webdriver.Builder.prototype.build = function() {
165187
} else {
166188
var url = this.serverUrl_;
167189
var client;
168-
if (url[0] == '/') {
190+
if (this.useBrowserCors_ && webdriver.http.CorsClient.isAvailable()) {
191+
client = new webdriver.http.XhrClient(url);
192+
} else if (url[0] == '/') {
169193
var origin = window.location.origin ||
170194
(window.location.protocol + '//' + window.location.host);
171195
client = new webdriver.http.XhrClient(origin + url);

0 commit comments

Comments
 (0)