Commit 244f1a9c 244f1a9c689226cedc2cc16f652b3514d90ec932 by Nicolas Perriault

added Casper.back() and .forward() for navigating history

1 parent 4cb79a2c
...@@ -89,6 +89,19 @@ ...@@ -89,6 +89,19 @@
89 */ 89 */
90 phantom.Casper.prototype = { 90 phantom.Casper.prototype = {
91 /** 91 /**
92 * Go a step back in browser's history
93 *
94 * @return Casper
95 */
96 back: function() {
97 return this.then(function(self) {
98 self.evaluate(function() {
99 history.back();
100 });
101 });
102 },
103
104 /**
92 * Encodes a resource using the base64 algorithm synchroneously using 105 * Encodes a resource using the base64 algorithm synchroneously using
93 * client-side XMLHttpRequest. 106 * client-side XMLHttpRequest.
94 * 107 *
...@@ -465,6 +478,19 @@ ...@@ -465,6 +478,19 @@
465 }, 478 },
466 479
467 /** 480 /**
481 * Go a step forward in browser's history
482 *
483 * @return Casper
484 */
485 forward: function(then) {
486 return this.then(function(self) {
487 self.evaluate(function() {
488 history.forward();
489 });
490 });
491 },
492
493 /**
468 * Retrieve current document url. 494 * Retrieve current document url.
469 * 495 *
470 * @return String 496 * @return String
...@@ -1250,8 +1276,8 @@ ...@@ -1250,8 +1276,8 @@
1250 exporter.addSuccess("unknown", message); 1276 exporter.addSuccess("unknown", message);
1251 } else { 1277 } else {
1252 casper.echo(this.colorize(FAIL, 'RED_BAR') + ' ' + this.formatMessage(message, 'WARNING')); 1278 casper.echo(this.colorize(FAIL, 'RED_BAR') + ' ' + this.formatMessage(message, 'WARNING'));
1253 this.comment(' got: ' + testValue); 1279 this.comment(' got: ' + testValue);
1254 this.comment(' expected: ' + expected); 1280 this.comment(' expected: ' + expected);
1255 this.testResults.failed++; 1281 this.testResults.failed++;
1256 exporter.addFailure("unknown", message, "test failed; expected: " + expected + "; got: " + testValue, "assertEquals"); 1282 exporter.addFailure("unknown", message, "test failed; expected: " + expected + "; got: " + testValue, "assertEquals");
1257 } 1283 }
...@@ -1298,7 +1324,17 @@ ...@@ -1298,7 +1324,17 @@
1298 * @param String message Test description 1324 * @param String message Test description
1299 */ 1325 */
1300 this.assertMatch = function(subject, pattern, message) { 1326 this.assertMatch = function(subject, pattern, message) {
1301 return this.assert(pattern.test(subject), message); 1327 if (pattern.test(subject)) {
1328 casper.echo(this.colorize(PASS, 'INFO') + ' ' + this.formatMessage(message));
1329 this.testResults.passed++;
1330 exporter.addSuccess("unknown", message);
1331 } else {
1332 casper.echo(this.colorize(FAIL, 'RED_BAR') + ' ' + this.formatMessage(message, 'WARNING'));
1333 this.comment(' subject: ' + subject);
1334 this.comment(' pattern: ' + pattern.toString());
1335 this.testResults.failed++;
1336 exporter.addFailure("unknown", message, "test failed; subject: " + subject + "; pattern: " + pattern.toString(), "assertMatch");
1337 }
1302 }; 1338 };
1303 1339
1304 /** 1340 /**
......
...@@ -186,11 +186,12 @@ casper.then(function(self) { ...@@ -186,11 +186,12 @@ casper.then(function(self) {
186 }); 186 });
187 187
188 // Casper.wait() 188 // Casper.wait()
189 var start = new Date().getTime(); 189 var waitStart;
190 casper.wait(1000, function(self) { 190 casper.then(function() {
191 waitStart = new Date().getTime();
192 }).wait(1000, function(self) {
191 self.test.comment('wait'); 193 self.test.comment('wait');
192 self.test.assert(new Date().getTime() - start > 1000, 'Casper.wait() can wait for a given amount of time'); 194 self.test.assert(new Date().getTime() - waitStart > 1000, 'Casper.wait() can wait for a given amount of time');
193
194 // Casper.waitFor() 195 // Casper.waitFor()
195 casper.thenOpen('tests/site/waitFor.html', function(self) { 196 casper.thenOpen('tests/site/waitFor.html', function(self) {
196 casper.test.comment('waitFor'); 197 casper.test.comment('waitFor');
...@@ -206,12 +207,29 @@ casper.wait(1000, function(self) { ...@@ -206,12 +207,29 @@ casper.wait(1000, function(self) {
206 }); 207 });
207 }); 208 });
208 209
210 // History
211 casper
212 .thenOpen('tests/site/page1.html')
213 .thenOpen('tests/site/page2.html')
214 .thenOpen('tests/site/page3.html')
215 .back()
216 .then(function(self) {
217 self.test.comment('navigating history backward');
218 self.test.assertMatch(self.getCurrentUrl(), /tests\/site\/page2\.html$/, 'Casper.back() can go back an history step');
219 })
220 .forward()
221 .then(function(self) {
222 self.test.comment('navigating history forward');
223 self.test.assertMatch(self.getCurrentUrl(), /tests\/site\/page3\.html$/, 'Casper.forward() can go forward an history step');
224 })
225 ;
226
209 // run suite 227 // run suite
210 casper.run(function(self) { 228 casper.run(function(self) {
229 casper.test.comment('history');
230 casper.test.assert(self.history.length > 0, 'Casper.history contains urls');
231 casper.test.assertMatch(self.history[0], /tests\/site\/index\.html$/, 'Casper.history has the correct first url');
211 self.test.comment('logging, again'); 232 self.test.comment('logging, again');
212 self.test.assertEquals(self.result.log.length, 3, 'log() logged messages'); 233 self.test.assertEquals(self.result.log.length, 3, 'log() logged messages');
213 self.test.comment('history');
214 self.test.assert(self.history.length > 0, 'Casper.history contains urls');
215 self.test.assertMatch(self.history[0], /tests\/site\/index\.html$/, 'Casper.history has the correct first url');
216 self.test.renderResults(true, 0, save); 234 self.test.renderResults(true, 0, save);
217 }); 235 });
......