Skip to content

Commit

Permalink
Making deferred.js available globally and adding a new http library a…
Browse files Browse the repository at this point in the history
…lso available globally
  • Loading branch information
samitbadle committed Sep 14, 2014
1 parent 72bfb94 commit 50039c6
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 60 deletions.
13 changes: 0 additions & 13 deletions ide/main/src/content/debugger.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,6 @@ function Debugger(editor) {
}
}

// var pluginProvided = SeleniumIDE.Preferences.getString("pluginProvidedUserExtensions");
// if (typeof pluginProvided != 'undefined') {
// try {
// var split_pluginProvided = pluginProvided.split(",");
// for (var sp = 0; sp < split_pluginProvided.length; sp++) {
// var js_pluginProvided = split_pluginProvided[sp].split(";");
// ExtensionsLoader.loadSubScript(subScriptLoader, js_pluginProvided[0], this.runner);
// }
// } catch (error) {
// this.log.error("error loading plugin provided user extension: " + error);
// }
// }
var pluginManager = editor.pluginManager;
pluginManager.getEnabledUserExtensions().forEach(function (plugin) {
for (var i = 0; i < plugin.code.length; i++) {
Expand All @@ -95,7 +83,6 @@ function Debugger(editor) {
});

if (executeUsingWebDriver) {
subScriptLoader.loadSubScript('chrome://selenium-ide/content/deferred.js', this.runner);
subScriptLoader.loadSubScript('chrome://selenium-ide/content/webdriver-backed-selenium.js', this.runner);
}
pluginManager.getEnabledUserExtensions().forEach(function (plugin) {
Expand Down
2 changes: 2 additions & 0 deletions ide/main/src/content/selenium-ide-common.xul
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ limitations under the License.
<script type="application/x-javascript" src="chrome://selenium-ide/content/locatorBuilders.js"/>
<script type="application/x-javascript" src="chrome://selenium-ide/content/application.js"/>
<script type="application/x-javascript" src="chrome://selenium-ide/content/storedHistory.js"/>
<script type="application/x-javascript" src="chrome://selenium-ide/content/utils/deferred.js"/>
<script type="application/x-javascript" src="chrome://selenium-ide/content/utils/http.js"/>
<script type="application/x-javascript" src="chrome://selenium-ide/content/browser/mozilla/prompt-service.js"/>
<script type="application/x-javascript" src="chrome://selenium-ide/content/editor.js"/>
<script type="application/x-javascript" src="chrome://selenium-ide/content/sidebar-editor.js"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,23 @@
//Loosely modeled on jQuery's Deferred and
/*
* Copyright 2014 Samit Badle
*
* Licensed 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.
*/

/**
* Partial implementation of promises
* Loosely modeled on jQuery's Deferred
*/
function Deferred(ctor) {
var that = this, state = Deferred.PENDING, done = [], fail = [], argsValue = null;

Expand Down
116 changes: 116 additions & 0 deletions ide/main/src/content/utils/http.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
* Copyright 2014 Samit Badle
*
* Licensed 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.
*/

/**
* HTTP service for Firefox addons
* Do not instantiate
*/
function HTTP() {
}

/**
* Make a generic http request
*
* @param method GET, POST, DELETE, PUT or any other http request
* @param url
* @param {string|object} [data] string or object, object is converted to json and sets the Content-Type header
* @param {Object.<string, string>} [headers] hash with keys containing header names and values containing its value
* @param {function(string, string, string)} [callback] a callback function that takes response, success, status. If callback is not given,
* a deferred object is created. If deferred.js is not loaded, an exception will occur.
* @returns {Deferred} if a deferred has been created
*/
HTTP.request = function(method, url, data, headers, callback) {
var deferred;
if (!callback) {
deferred = new Deferred();
callback = function(response, success, status) {
if (success) {
deferred.resolve(response, success, status);
} else {
deferred.reject(response, success, status);
}
};

}
var httpRequest = new XMLHttpRequest();
//LOG.debug('Executing: ' + method + " " + url);
httpRequest.open(method, url);
httpRequest.onreadystatechange = function() {
try {
if (httpRequest.readyState === 4) {
if (httpRequest.status === 200 || (httpRequest.status > 200 && httpRequest.status < 300)) {
callback(httpRequest.responseText, true, httpRequest.status);
} else if (httpRequest.status === 500 ) {
callback(httpRequest.responseText, false, httpRequest.status);
} else {
//TODO eliminate alert and signal the failure
// alert('There was a problem with the request.\nUrl: ' + url + '\nHttp Status: ' + httpRequest.status + "\nResponse: " + httpRequest.responseText);
LOG.debug('Error: There was a problem with the request.\nUrl: ' + url + '\nHttp Status: ' + httpRequest.status + "\nResponse: " + httpRequest.responseText);
callback(null, false, httpRequest.status);
}
}
} catch(e) {
//TODO eliminate alert and signal the failure, typically when callback is not given and Deferred is not loaded
alert('Caught Exception in HTTP.request: ' + e);
throw e;
}
};
//httpRequest.channel.loadFlags |= Components.interfaces.nsIRequest.LOAD_BYPASS_CACHE;
if (data && typeof data !== 'string') {
data = JSON.stringify(data);
//do this before you set custom headers, so that user supplied headers will overwrite this
httpRequest.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
}
if (headers) {
for (var header in headers) {
httpRequest.setRequestHeader(header, headers[header] + '');
}
}
if (data) {
httpRequest.send(data);
} else {
httpRequest.send();
}
return deferred;
};

/**
* Shortcut method to create HTTP POST requests. See HTTP.request() for more details.
*
* @param url
* @param {string|object} [data] string or object, object is converted to json and sets the Content-Type header
* @param {Object.<string, string>} [headers] hash with keys containing header names and values containing its value
* @param {function(string, string, string)} [callback] a callback function that takes response, success, status. If callback is not given,
* a deferred object is created. If deferred.js is not loaded, an exception occurs.
* @returns {Deferred} if a deferred has been created
*/
HTTP.post = function(url, data, headers, callback) {
return this.request('POST', url, data, headers, callback);
};

/**
* Shortcut method to create HTTP GET requests. See HTTP.request() for more details.
*
* @param url
* @param {Object.<string, string>} [headers] hash with keys containing header names and values containing its value
* @param {function(string, string, string)} [callback] a callback function that takes response, success, status. If callback is not given,
* a deferred object is created. If deferred.js is not loaded, an exception occurs.
* @returns {Deferred} if a deferred has been created
*/
HTTP.get = function (url, headers, callback) {
return this.request('GET', url, null, headers, callback);
};

49 changes: 3 additions & 46 deletions ide/main/src/content/webdriver-backed-selenium.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ WebdriverBackedSelenium.prototype.startNewWebdriverSession = function(browserNam
var self = this;
return new Deferred(function(deferred) {
LOG.debug('Connecting to Selenium Server');
httpPost('http://localhost:4444/wd/hub/session',
HTTP.post('http://localhost:4444/wd/hub/session',
JSON.stringify({
'desiredCapabilities': {'browserName': browserName}
}), {'Accept': 'application/json; charset=utf-8'}, function(response, success) {
Expand Down Expand Up @@ -263,7 +263,7 @@ WebdriverBackedSelenium.prototype.remoteControlCommand = function(verb, args) {
var requestData = httpRequestFor(verb, args, this.sessionId);
// alert("Sending server request: " + requestData);
return new Deferred(function(deferred) {
httpPost('http://localhost:4444/selenium-server/driver/', requestData, {'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8'}, function(response, success) {
HTTP.post('http://localhost:4444/selenium-server/driver/', requestData, {'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8'}, function(response, success) {
if (success) {
if (response.substr(0, 2) === 'OK') {
deferred.resolve(response.substr(3)); //strip "OK," from response
Expand Down Expand Up @@ -303,7 +303,7 @@ WebdriverBackedSelenium.prototype.webDriverCommand = function(url, opts, args) {
}
// alert("Sending server request: " + requestData);
return new Deferred(function(deferred) {
httpCommon(requestMethod, 'http://localhost:4444/wd/hub/' + url, requestData, contentType, function(response, success, status) {
HTTP.request(requestMethod, 'http://localhost:4444/wd/hub/' + url, requestData, contentType, function(response, success, status) {
var result;
if (response) {
// alert("Response: " + response);
Expand Down Expand Up @@ -338,46 +338,3 @@ function httpRequestFor(verb, args, sessionId) {
}
return data;
}

function httpCommon(method, url, data, headers, callback) {
var httpRequest = new XMLHttpRequest();
//LOG.debug('Executing: ' + method + " " + url);
httpRequest.open(method, url);
httpRequest.onreadystatechange = function() {
try {
if (httpRequest.readyState === 4) {
if (httpRequest.status === 200 || (httpRequest.status > 200 && httpRequest.status < 300)) {
callback(httpRequest.responseText, true, httpRequest.status);
} else if (httpRequest.status === 500 ) {
callback(httpRequest.responseText, false, httpRequest.status);
} else {
//TODO eliminate alert and signal the failure
// alert('There was a problem with the request.\nUrl: ' + url + '\nHttp Status: ' + httpRequest.status + "\nResponse: " + httpRequest.responseText);
LOG.debug('Error: There was a problem with the request.\nUrl: ' + url + '\nHttp Status: ' + httpRequest.status + "\nResponse: " + httpRequest.responseText);
callback(null, false, httpRequest.status);
}
}
} catch(e) {
//TODO eliminate alert and signal the failure
alert('Caught Exception in httpPost: ' + e);
throw e;
}
};
//httpRequest.channel.loadFlags |= Components.interfaces.nsIRequest.LOAD_BYPASS_CACHE;
for (var header in headers) {
httpRequest.setRequestHeader(header, headers[header] + '');
}
if (data) {
httpRequest.send(data);
} else {
httpRequest.send();
}
}

function httpPost(url, data, headers, callback) {
httpCommon('POST',url, data, headers, callback);
}

function httpGet(url, headers, callback) {
httpCommon('GET',url, null, headers, callback);
}

0 comments on commit 50039c6

Please sign in to comment.