Permalink
Browse files

Added very basic ajax mocking and merged Facade object into UITest

  • Loading branch information...
1 parent 8b5f65e commit 9a107ee3ead2d9b3da16404da9a33370da05634c @mennovanslooten committed May 12, 2010
Showing with 271 additions and 145 deletions.
  1. +5 −0 demo/main.js
  2. +225 −145 js/uitest.js
  3. +41 −0 uitest_tests.js
View
5 demo/main.js
@@ -9,9 +9,14 @@
url:'ajax.html',
async:true,
cache:false,
+ /*
success: function(html) {
_result.html(html);
}
+ */
+ complete: function(response, status) {
+ _result.html(response.responseText);
+ }
});
}
View
370 js/uitest.js
@@ -1,5 +1,6 @@
var UITest = new function() {
var _testFrame = parent.frames.main;
+ var _ajax_mocks = [];
var _testScripts = [];
var _activeTest = null;
var _passed = 0;
@@ -12,6 +13,7 @@ var UITest = new function() {
var _startIndex = parseInt(parent.location.search.substr(1), 10);
+ // Debug log and listing
function setStatus(status) {
if (_activeTest) {
document.getElementById('test-status-' + _activeTest.index).className = status;
@@ -30,6 +32,31 @@ var UITest = new function() {
}
+ function writeList() {
+ var list = document.getElementById('listtests');
+ var html = '<table>'
+ html += '<thead><tr><th colspan="3"><a href="index.html" target="_parent">List of tests</a></th></tr></thead>';
+ html += '<tbody>';
+ for (var i = 0; i < _testScripts.length; i++) {
+ html += '<tr id="test-status-' + i + '">'
+ html += '<th>' + _testScripts[i].name + '</th>';
+ html += '<td><a href="index.html?' + i + '" target="_parent">run</a></td>';
+ html += '<td><a href="index.html?' + i + '&slow" target="_parent">slow</a></td>';
+ html += '<td><a href="index.html?' + i + '&notests" target="_parent">no tests</a></td>';
+ html += '</tr>'
+ }
+ html += '<tr>'
+ html += '<th>All tests</th>';
+ html += '<td><a href="index.html?all" target="_parent">run</a></td>';
+ html += '<td><a href="index.html?all&slow" target="_parent">slow</a></td>';
+ html += '<td></td>';
+ html += '</tr>'
+ html += '</tbody></table>';
+ list.innerHTML = html;
+ }
+
+
+ // Test execution
function setupAndExecute(testScript) {
schedule(testScript);
_testFrame.location.href = testScript.url;
@@ -57,7 +84,7 @@ var UITest = new function() {
log('Starting test script ["' + testScript.name + '"] on ' + testScript.url + '', "name");
_activeTest = testScript;
setStatus('setup');
- Facade.init(function() {
+ initFacade(function() {
if (testScript.setup) {
testScript.setup(_testFrame);
}
@@ -107,144 +134,8 @@ var UITest = new function() {
}
- this.fail = function(assertion, s) {
- if (_activeTest) {
- setStatus('failed');
-
- var message = 'Failed ' + assertion + ' in ' + _activeTest.name +': ' + s;
- teardown(message);
- throw new Error(message);
- }
- }
-
-
- this.start = function() {
- var list = document.getElementById('listtests');
- var html = '<table>'
- html += '<thead><tr><th colspan="3"><a href="index.html" target="_parent">List of tests</a></th></tr></thead>';
- html += '<tbody>';
- for (var i = 0; i < _testScripts.length; i++) {
- html += '<tr id="test-status-' + i + '">'
- html += '<th>' + _testScripts[i].name + '</th>';
- html += '<td><a href="index.html?' + i + '" target="_parent">run</a></td>';
- html += '<td><a href="index.html?' + i + '&slow" target="_parent">slow</a></td>';
- html += '<td><a href="index.html?' + i + '&notests" target="_parent">no tests</a></td>';
- html += '</tr>'
- }
- html += '<tr>'
- html += '<th>All tests</th>';
- html += '<td><a href="index.html?all" target="_parent">run</a></td>';
- html += '<td><a href="index.html?all&slow" target="_parent">slow</a></td>';
- html += '<td></td>';
- html += '</tr>'
- html += '</tbody></table>';
- list.innerHTML = html;
-
- if (!isNaN(_startIndex)) {
- _testScripts = _testScripts.splice(_startIndex, 1);
- _total = _testScripts[0].tests.length;
- } else if (!_runAll) {
- return;
- }
-
- nextTest();
- }
-
-
- this.addTestScript = function(testScript) {
- // For short notation
- if (testScript.waitFor && testScript.thenRun) {
- testScript.tests = [{
- waitFor: testScript.waitFor,
- thenRun: testScript.thenRun
- }];
- }
-
- var tests = testScript.tests;
- var wrappers = [];
- for (var i = tests.length - 1; i >= 0; i--) {
- _total++;
- wrappers[i] = function(index, selector, success) {
- return function() {
- if (index < tests.length - 1) {
- var nestedsuccess = function(element) {
- try {
- success(element);
- _passed++;
- updateProgress();
- wrappers[index + 1]();
- } catch(e) {
- console.error(e);
- }
- }
- } else {
- var nestedsuccess = function(element) {
- try {
- success(element);
- _passed++;
- updateProgress();
- pass();
- } catch(e) {
- console.error(e);
- }
- }
- }
-
- switch (typeof selector) {
- case 'number':
- setTimeout(nestedsuccess, selector);
- break;
-
- case 'function':
- Facade.waitForExpressionTrue(selector, nestedsuccess, function() {
- UITest.fail('waitForExpressionTrue', selector + ' failed');
- });
- break;
-
- case 'string':
- if (selector.indexOf('!') === 0) {
- Facade.waitForElementHidden(selector.substr(1), nestedsuccess, function() {
- UITest.fail('waitForElementHidden', selector + ' failed');
- });
- } else if (selector === 'pageToLoad') {
- Facade.waitForPageToLoad(nestedsuccess, function() {
- UITest.fail('waitForPageToLoad', selector + ' failed');
- });
- } else {
- Facade.waitForElementVisible(selector, nestedsuccess, function() {
- UITest.fail('waitForElementVisible', selector + ' failed');
- });
- }
- break;
- }
- }
- }(i, tests[i].waitFor, tests[i].thenRun);
- }
- testScript.execute = function() {
- updateProgress();
- wrappers[0]();
- }
- testScript.index = _testScripts.length;
-
- _testScripts.push(testScript);
- }
-
-
- this.log = function(message) {
- if (message) {
- if (_slowMode) {
- alert(message);
- }
- log(message);
- }
- }
-};
-
-
-
-var Facade = new function() {
- var init = this.init = function(callback) {
- var _testFrame = parent.frames.main;
+ // Facade (interface with page being tested)
+ function initFacade(callback) {
_testFrame.confirm = function() { return true; };
_testFrame.onbeforeunload = null;
$ = _testFrame.$;
@@ -263,11 +154,71 @@ var Facade = new function() {
UITest.log(s);
}
};
-
+
+ initMockAjax();
callback();
}
+ function initMockAjax() {
+ var originalAjax = $.ajax;
+ _ajax_mocks = [];
+
+ $.ajax = function(options) {
+ for (var i = 0; i < _ajax_mocks.length; i++) {
+ var ajax_mock = _ajax_mocks[i];
+ if (ajax_mock.path == options.url) {
+ mockAjax(options, ajax_mock.response);
+ return;
+ }
+ }
+ originalAjax(options);
+ };
+ }
+
+
+ function mockAjax(options, response) {
+ console.dir(options);
+ var std_response = {
+ headers : {},
+ delay : 250,
+ addHeader: function(name, value) {
+ this.headers[name] = value;
+ },
+ status : 200,
+ getResponseHeader : function(name) {
+ return this.headers[name] || null;
+ },
+ getAllResponseHeaders : function() {
+ var header_string = '';
+ for (var name in this.headers) {
+ header_string += name + ':' + this.headers[name] + '\n';
+ }
+ return header_string;
+ },
+ responseText: ''
+ };
+
+ for (var p in response) {
+ std_response[p] = response[p]
+ }
+
+ function callback() {
+ if (options.success) {
+ options.success(std_response.responseText);
+ } else if (options.complete) {
+ options.complete(std_response, 'success');
+ }
+ }
+
+ if (options.async) {
+ setTimeout(callback, std_response.delay);
+ } else {
+ callback();
+ }
+ }
+
+
function waitForCondition(condition, callback, timeoutcallback, maxtimes) {
if (typeof maxtimes === 'undefined') {
maxtimes = 20;
@@ -289,7 +240,7 @@ var Facade = new function() {
}
- this.waitForPageToLoad = function(callback, timeoutcallback, maxtimes) {
+ function waitForPageToLoad(callback, timeoutcallback, maxtimes) {
function condition() {
var testFrame = parent.frames.main;
return (testFrame && testFrame.jQuery && testFrame.jQuery.isReady);
@@ -298,7 +249,7 @@ var Facade = new function() {
}
- this.waitForElementVisible = function(selector, callback, timeoutcallback, maxtimes) {
+ function waitForElementVisible(selector, callback, timeoutcallback, maxtimes) {
function condition() {
var element = $(selector);
if (element && element.is(':visible')) {
@@ -310,7 +261,7 @@ var Facade = new function() {
}
- this.waitForElementHidden = function(selector, callback, timeoutcallback, maxtimes) {
+ function waitForElementHidden(selector, callback, timeoutcallback, maxtimes) {
function condition() {
var element = $(selector);
if (!element.length || element.is(':hidden') || element.css('opacity') == 0) {
@@ -322,11 +273,140 @@ var Facade = new function() {
}
- this.waitForExpressionTrue = function(condition, callback, timeoutcallback, maxtimes) {
+ function waitForExpressionTrue(condition, callback, timeoutcallback, maxtimes) {
waitForCondition(condition, callback, timeoutcallback);
}
-};
+ // Public methods
+ return {
+ start : function() {
+ writeList();
+ if (!isNaN(_startIndex)) {
+ _testScripts = _testScripts.splice(_startIndex, 1);
+ _total = _testScripts[0].tests.length;
+ } else if (!_runAll) {
+ return;
+ }
+
+ nextTest();
+ },
+
+ addTestScript : function(testScript) {
+ // For short notation
+ if (testScript.waitFor && testScript.thenRun) {
+ testScript.tests = [{
+ waitFor: testScript.waitFor,
+ thenRun: testScript.thenRun
+ }];
+ }
+ var tests = testScript.tests;
+ var wrappers = [];
+ for (var i = tests.length - 1; i >= 0; i--) {
+ _total++;
+ wrappers[i] = function(index, selector, success) {
+ return function() {
+ if (index < tests.length - 1) {
+ var nestedsuccess = function(element) {
+ try {
+ success(element);
+ _passed++;
+ updateProgress();
+ wrappers[index + 1]();
+ } catch(e) {
+ console.error(e);
+ }
+ }
+ } else {
+ var nestedsuccess = function(element) {
+ try {
+ success(element);
+ _passed++;
+ updateProgress();
+ pass();
+ } catch(e) {
+ console.error(e);
+ }
+ }
+ }
+
+ switch (typeof selector) {
+ case 'number':
+ setTimeout(nestedsuccess, selector);
+ break;
+
+ case 'function':
+ waitForExpressionTrue(selector, nestedsuccess, function() {
+ UITest.fail('waitForExpressionTrue', selector + ' failed');
+ });
+ break;
+
+ case 'string':
+ if (selector.indexOf('!') === 0) {
+ waitForElementHidden(selector.substr(1), nestedsuccess, function() {
+ UITest.fail('waitForElementHidden', selector + ' failed');
+ });
+ } else if (selector === 'pageToLoad') {
+ waitForPageToLoad(nestedsuccess, function() {
+ UITest.fail('waitForPageToLoad', selector + ' failed');
+ });
+ } else {
+ waitForElementVisible(selector, nestedsuccess, function() {
+ UITest.fail('waitForElementVisible', selector + ' failed');
+ });
+ }
+ break;
+ }
+ }
+ }(i, tests[i].waitFor, tests[i].thenRun);
+ }
+ testScript.execute = function() {
+ updateProgress();
+ wrappers[0]();
+ }
+ testScript.index = _testScripts.length;
+
+ _testScripts.push(testScript);
+ },
+
+
+ log : function(message) {
+ if (message) {
+ if (_slowMode) {
+ alert(message);
+ }
+ log(message);
+ }
+ },
+
+
+ fail : function(assertion, s) {
+ if (_activeTest) {
+ setStatus('failed');
+
+ var message = 'Failed ' + assertion + ' in ' + _activeTest.name +': ' + s;
+ teardown(message);
+ throw new Error(message);
+ }
+ },
+
+
+ addMockAjax : function (path, response) {
+ _ajax_mocks.push({
+ path:path,
+ response:response
+ });
+ },
+
+
+ removeMockAjax : function (path) {
+ for (var i = _ajax_mocks.length - 1; i >= 0; i--) {
+ if (_ajax_mocks[i].path === path) {
+ _ajax_mocks.splice(i, 1);
+ }
+ }
+ }
+ };
+};
View
41 uitest_tests.js
@@ -81,6 +81,47 @@
UITest.addTestScript({
+ name : 'Demo Test: Ajax Mockup',
+ url : 'demo/demo.html',
+ setup : function(testframe) {
+ UITest.addMockAjax('ajax.html', {
+ headers : {
+ headerName1: 'headerValue1',
+ headerName2: 'headerValue2'
+ },
+ delay:1500,
+ responseText: '<p>I am a mocked up response.</p>'
+ });
+ },
+ tests : [
+ {
+ waitFor: 'pageToLoad',
+ thenRun: function() {
+ UITest.log('Load async ajax');
+ // This ajax request will be mocked up
+ $('#button-load-ajax').click();
+ }
+ },
+ {
+ waitFor: '#result p:contains("mocked up")',
+ thenRun: function() {
+ UITest.log('Ajax data available');
+ UITest.removeMockAjax('ajax.html');
+ // This ajax request will be normal again
+ $('#button-load-ajax').click();
+ }
+ },
+ {
+ waitFor: '#result p:contains("Lorem ipsum")',
+ thenRun: function() {
+ UITest.log('Ajax data available');
+ }
+ }
+ ]
+ });
+
+
+ UITest.addTestScript({
name : 'Demo Test: Fade Out/Fade In',
url : 'demo/demo.html',
tests : [

0 comments on commit 9a107ee

Please sign in to comment.