comparison js/black-box.js @ 110:4567dc69cb3e

Added failure messages and more color feedback to black-box tests
author Atul Varma <avarma@mozilla.com>
date Fri, 30 Apr 2010 05:26:01 -0700
parents 8335b1abc295
children 00d6b3149165
comparison
equal deleted inserted replaced
109:8335b1abc295 110:4567dc69cb3e
1 function Automator(window, jQuery, onDone) { 1 function Automator(window, jQuery, onDone, onFail) {
2 this.jQuery = jQuery; 2 this.jQuery = jQuery;
3 this.onDone = onDone; 3 this.onDone = onDone;
4 this.onFail = onFail;
4 this.queue = []; 5 this.queue = [];
5 this.window = window; 6 this.window = window;
6 } 7 }
7 8
8 Automator.prototype = { 9 Automator.prototype = {
22 }, 23 },
23 _$: function _$(sel) { 24 _$: function _$(sel) {
24 var query = this.jQuery(sel); 25 var query = this.jQuery(sel);
25 26
26 if (query.length == 0) 27 if (query.length == 0)
27 throw new Error("selector yields no results: " + sel); 28 this.onFail("selector yields no results: " + sel);
28 if (query.length > 1) 29 if (query.length > 1)
29 throw new Error("selector yields " + query.length + 30 this.onFail("selector yields " + query.length +
30 " results instead of 1: " + sel); 31 " results instead of 1: " + sel);
31 32
32 return query; 33 return query;
33 }, 34 },
34 verifyVisible: function verifyVisible(field) { 35 verifyVisible: function verifyVisible(field) {
35 this.queue.push( 36 this.queue.push(
36 function() { 37 function() {
37 var query = this.jQuery(field + ":visible"); 38 var query = this.jQuery(field + ":visible");
38 39
39 if (query.length == 0) 40 if (query.length == 0)
40 throw new Error("selector not visible: " + field); 41 this.onFail("selector not visible: " + field);
41 }); 42 });
42 }, 43 },
43 type: function type(field, value) { 44 type: function type(field, value) {
44 this.queue.push(function() { this._$(field).val(value); }); 45 this.queue.push(function() { this._$(field).val(value); });
45 }, 46 },
46 submit: function submit(form) { 47 submit: function submit(form) {
47 this.queue.push(function() { this._$(form).submit(); }); 48 this.queue.push(function() { this._$(form).submit(); });
48 } 49 }
49 }; 50 };
50 51
52 function testLoginWithNoPassword(auto) {
53 auto.type("#login .username", "john@doe.com");
54 auto.type("#login .password", "");
55 auto.submit("#login form");
56 auto.verifyVisible("#header .requires-login");
57 }
58
51 function testLoginWithCorrectPassword(auto) { 59 function testLoginWithCorrectPassword(auto) {
52 auto.type("#login .username", "john@doe.com"); 60 auto.type("#login .username", "john@doe.com");
53 auto.type("#login .password", "test"); 61 auto.type("#login .password", "test");
54 auto.submit("#login form"); 62 auto.submit("#login form");
55 auto.verifyVisible("#header .requires-login"); 63 auto.verifyVisible("#header .requires-login");
56 auto.verifyVisible("#header .requires-auth-login"); 64 auto.verifyVisible("#header .requires-auth-login");
57 }
58
59 function testLoginWithNoPassword(auto) {
60 auto.type("#login .username", "john@doe.com");
61 auto.type("#login .password", "");
62 auto.submit("#login form");
63 auto.verifyVisible("#header .requires-login");
64 } 65 }
65 66
66 function testLoginWithIncorrectPassword(auto) { 67 function testLoginWithIncorrectPassword(auto) {
67 auto.type("#login .username", "john@doe.com"); 68 auto.type("#login .username", "john@doe.com");
68 auto.type("#login .password", "u"); 69 auto.type("#login .password", "u");
116 var testButton = this; 117 var testButton = this;
117 var testFunc = window[testButton.id]; 118 var testFunc = window[testButton.id];
118 var auto; 119 var auto;
119 120
120 $(testButton).addClass("running"); 121 $(testButton).addClass("running");
122 function onFail(reason) {
123 $(testButton).addClass("fail");
124 var msg = ("Failure in " + $(testButton).text() + ": " +
125 reason);
126 var msgElem = $('<p class="fail"></p>').text(msg);
127 msgElem.hide();
128 $("#messages").append(msgElem);
129 msgElem.slideDown();
130 }
121 function onDone() { 131 function onDone() {
122 $(testButton).removeClass("running"); 132 $(testButton).removeClass("running");
133 if (!$(testButton).hasClass("fail"))
134 $(testButton).addClass("success");
123 } 135 }
124 136
125 resetDashboard( 137 resetDashboard(
126 function(method, args) { 138 function(method, args) {
127 switch (method) { 139 switch (method) {
128 case "blackBox.onDashboardLoaded": 140 case "blackBox.onDashboardLoaded":
129 var dashboard = args[0]; 141 var dashboard = args[0];
130 var options = args[1]; 142 var options = args[1];
131 auto = new Automator(window, options.jQuery, onDone); 143 auto = new Automator(window, options.jQuery, onDone,
144 onFail);
132 testFunc(auto); 145 testFunc(auto);
133 break; 146 break;
134 case "blackBox.afterInit": 147 case "blackBox.afterInit":
135 auto.queueNextCommand(); 148 auto.queueNextCommand();
136 break; 149 break;