Skip to content

Commit 50039c6

Browse files
committed
Making deferred.js available globally and adding a new http library also available globally
1 parent 72bfb94 commit 50039c6

File tree

5 files changed

+141
-60
lines changed

5 files changed

+141
-60
lines changed

ide/main/src/content/debugger.js

-13
Original file line numberDiff line numberDiff line change
@@ -66,18 +66,6 @@ function Debugger(editor) {
6666
}
6767
}
6868

69-
// var pluginProvided = SeleniumIDE.Preferences.getString("pluginProvidedUserExtensions");
70-
// if (typeof pluginProvided != 'undefined') {
71-
// try {
72-
// var split_pluginProvided = pluginProvided.split(",");
73-
// for (var sp = 0; sp < split_pluginProvided.length; sp++) {
74-
// var js_pluginProvided = split_pluginProvided[sp].split(";");
75-
// ExtensionsLoader.loadSubScript(subScriptLoader, js_pluginProvided[0], this.runner);
76-
// }
77-
// } catch (error) {
78-
// this.log.error("error loading plugin provided user extension: " + error);
79-
// }
80-
// }
8169
var pluginManager = editor.pluginManager;
8270
pluginManager.getEnabledUserExtensions().forEach(function (plugin) {
8371
for (var i = 0; i < plugin.code.length; i++) {
@@ -95,7 +83,6 @@ function Debugger(editor) {
9583
});
9684

9785
if (executeUsingWebDriver) {
98-
subScriptLoader.loadSubScript('chrome://selenium-ide/content/deferred.js', this.runner);
9986
subScriptLoader.loadSubScript('chrome://selenium-ide/content/webdriver-backed-selenium.js', this.runner);
10087
}
10188
pluginManager.getEnabledUserExtensions().forEach(function (plugin) {

ide/main/src/content/selenium-ide-common.xul

+2
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ limitations under the License.
5252
<script type="application/x-javascript" src="chrome://selenium-ide/content/locatorBuilders.js"/>
5353
<script type="application/x-javascript" src="chrome://selenium-ide/content/application.js"/>
5454
<script type="application/x-javascript" src="chrome://selenium-ide/content/storedHistory.js"/>
55+
<script type="application/x-javascript" src="chrome://selenium-ide/content/utils/deferred.js"/>
56+
<script type="application/x-javascript" src="chrome://selenium-ide/content/utils/http.js"/>
5557
<script type="application/x-javascript" src="chrome://selenium-ide/content/browser/mozilla/prompt-service.js"/>
5658
<script type="application/x-javascript" src="chrome://selenium-ide/content/editor.js"/>
5759
<script type="application/x-javascript" src="chrome://selenium-ide/content/sidebar-editor.js"/>

ide/main/src/content/deferred.js ide/main/src/content/utils/deferred.js

+20-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,23 @@
1-
//Loosely modeled on jQuery's Deferred and
1+
/*
2+
* Copyright 2014 Samit Badle
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/**
18+
* Partial implementation of promises
19+
* Loosely modeled on jQuery's Deferred
20+
*/
221
function Deferred(ctor) {
322
var that = this, state = Deferred.PENDING, done = [], fail = [], argsValue = null;
423

ide/main/src/content/utils/http.js

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
* Copyright 2014 Samit Badle
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/**
18+
* HTTP service for Firefox addons
19+
* Do not instantiate
20+
*/
21+
function HTTP() {
22+
}
23+
24+
/**
25+
* Make a generic http request
26+
*
27+
* @param method GET, POST, DELETE, PUT or any other http request
28+
* @param url
29+
* @param {string|object} [data] string or object, object is converted to json and sets the Content-Type header
30+
* @param {Object.<string, string>} [headers] hash with keys containing header names and values containing its value
31+
* @param {function(string, string, string)} [callback] a callback function that takes response, success, status. If callback is not given,
32+
* a deferred object is created. If deferred.js is not loaded, an exception will occur.
33+
* @returns {Deferred} if a deferred has been created
34+
*/
35+
HTTP.request = function(method, url, data, headers, callback) {
36+
var deferred;
37+
if (!callback) {
38+
deferred = new Deferred();
39+
callback = function(response, success, status) {
40+
if (success) {
41+
deferred.resolve(response, success, status);
42+
} else {
43+
deferred.reject(response, success, status);
44+
}
45+
};
46+
47+
}
48+
var httpRequest = new XMLHttpRequest();
49+
//LOG.debug('Executing: ' + method + " " + url);
50+
httpRequest.open(method, url);
51+
httpRequest.onreadystatechange = function() {
52+
try {
53+
if (httpRequest.readyState === 4) {
54+
if (httpRequest.status === 200 || (httpRequest.status > 200 && httpRequest.status < 300)) {
55+
callback(httpRequest.responseText, true, httpRequest.status);
56+
} else if (httpRequest.status === 500 ) {
57+
callback(httpRequest.responseText, false, httpRequest.status);
58+
} else {
59+
//TODO eliminate alert and signal the failure
60+
// alert('There was a problem with the request.\nUrl: ' + url + '\nHttp Status: ' + httpRequest.status + "\nResponse: " + httpRequest.responseText);
61+
LOG.debug('Error: There was a problem with the request.\nUrl: ' + url + '\nHttp Status: ' + httpRequest.status + "\nResponse: " + httpRequest.responseText);
62+
callback(null, false, httpRequest.status);
63+
}
64+
}
65+
} catch(e) {
66+
//TODO eliminate alert and signal the failure, typically when callback is not given and Deferred is not loaded
67+
alert('Caught Exception in HTTP.request: ' + e);
68+
throw e;
69+
}
70+
};
71+
//httpRequest.channel.loadFlags |= Components.interfaces.nsIRequest.LOAD_BYPASS_CACHE;
72+
if (data && typeof data !== 'string') {
73+
data = JSON.stringify(data);
74+
//do this before you set custom headers, so that user supplied headers will overwrite this
75+
httpRequest.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
76+
}
77+
if (headers) {
78+
for (var header in headers) {
79+
httpRequest.setRequestHeader(header, headers[header] + '');
80+
}
81+
}
82+
if (data) {
83+
httpRequest.send(data);
84+
} else {
85+
httpRequest.send();
86+
}
87+
return deferred;
88+
};
89+
90+
/**
91+
* Shortcut method to create HTTP POST requests. See HTTP.request() for more details.
92+
*
93+
* @param url
94+
* @param {string|object} [data] string or object, object is converted to json and sets the Content-Type header
95+
* @param {Object.<string, string>} [headers] hash with keys containing header names and values containing its value
96+
* @param {function(string, string, string)} [callback] a callback function that takes response, success, status. If callback is not given,
97+
* a deferred object is created. If deferred.js is not loaded, an exception occurs.
98+
* @returns {Deferred} if a deferred has been created
99+
*/
100+
HTTP.post = function(url, data, headers, callback) {
101+
return this.request('POST', url, data, headers, callback);
102+
};
103+
104+
/**
105+
* Shortcut method to create HTTP GET requests. See HTTP.request() for more details.
106+
*
107+
* @param url
108+
* @param {Object.<string, string>} [headers] hash with keys containing header names and values containing its value
109+
* @param {function(string, string, string)} [callback] a callback function that takes response, success, status. If callback is not given,
110+
* a deferred object is created. If deferred.js is not loaded, an exception occurs.
111+
* @returns {Deferred} if a deferred has been created
112+
*/
113+
HTTP.get = function (url, headers, callback) {
114+
return this.request('GET', url, null, headers, callback);
115+
};
116+

ide/main/src/content/webdriver-backed-selenium.js

+3-46
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ WebdriverBackedSelenium.prototype.startNewWebdriverSession = function(browserNam
229229
var self = this;
230230
return new Deferred(function(deferred) {
231231
LOG.debug('Connecting to Selenium Server');
232-
httpPost('http://localhost:4444/wd/hub/session',
232+
HTTP.post('http://localhost:4444/wd/hub/session',
233233
JSON.stringify({
234234
'desiredCapabilities': {'browserName': browserName}
235235
}), {'Accept': 'application/json; charset=utf-8'}, function(response, success) {
@@ -263,7 +263,7 @@ WebdriverBackedSelenium.prototype.remoteControlCommand = function(verb, args) {
263263
var requestData = httpRequestFor(verb, args, this.sessionId);
264264
// alert("Sending server request: " + requestData);
265265
return new Deferred(function(deferred) {
266-
httpPost('http://localhost:4444/selenium-server/driver/', requestData, {'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8'}, function(response, success) {
266+
HTTP.post('http://localhost:4444/selenium-server/driver/', requestData, {'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8'}, function(response, success) {
267267
if (success) {
268268
if (response.substr(0, 2) === 'OK') {
269269
deferred.resolve(response.substr(3)); //strip "OK," from response
@@ -303,7 +303,7 @@ WebdriverBackedSelenium.prototype.webDriverCommand = function(url, opts, args) {
303303
}
304304
// alert("Sending server request: " + requestData);
305305
return new Deferred(function(deferred) {
306-
httpCommon(requestMethod, 'http://localhost:4444/wd/hub/' + url, requestData, contentType, function(response, success, status) {
306+
HTTP.request(requestMethod, 'http://localhost:4444/wd/hub/' + url, requestData, contentType, function(response, success, status) {
307307
var result;
308308
if (response) {
309309
// alert("Response: " + response);
@@ -338,46 +338,3 @@ function httpRequestFor(verb, args, sessionId) {
338338
}
339339
return data;
340340
}
341-
342-
function httpCommon(method, url, data, headers, callback) {
343-
var httpRequest = new XMLHttpRequest();
344-
//LOG.debug('Executing: ' + method + " " + url);
345-
httpRequest.open(method, url);
346-
httpRequest.onreadystatechange = function() {
347-
try {
348-
if (httpRequest.readyState === 4) {
349-
if (httpRequest.status === 200 || (httpRequest.status > 200 && httpRequest.status < 300)) {
350-
callback(httpRequest.responseText, true, httpRequest.status);
351-
} else if (httpRequest.status === 500 ) {
352-
callback(httpRequest.responseText, false, httpRequest.status);
353-
} else {
354-
//TODO eliminate alert and signal the failure
355-
// alert('There was a problem with the request.\nUrl: ' + url + '\nHttp Status: ' + httpRequest.status + "\nResponse: " + httpRequest.responseText);
356-
LOG.debug('Error: There was a problem with the request.\nUrl: ' + url + '\nHttp Status: ' + httpRequest.status + "\nResponse: " + httpRequest.responseText);
357-
callback(null, false, httpRequest.status);
358-
}
359-
}
360-
} catch(e) {
361-
//TODO eliminate alert and signal the failure
362-
alert('Caught Exception in httpPost: ' + e);
363-
throw e;
364-
}
365-
};
366-
//httpRequest.channel.loadFlags |= Components.interfaces.nsIRequest.LOAD_BYPASS_CACHE;
367-
for (var header in headers) {
368-
httpRequest.setRequestHeader(header, headers[header] + '');
369-
}
370-
if (data) {
371-
httpRequest.send(data);
372-
} else {
373-
httpRequest.send();
374-
}
375-
}
376-
377-
function httpPost(url, data, headers, callback) {
378-
httpCommon('POST',url, data, headers, callback);
379-
}
380-
381-
function httpGet(url, headers, callback) {
382-
httpCommon('GET',url, null, headers, callback);
383-
}

0 commit comments

Comments
 (0)