From e588323f5d2050e57b2989b126349d7046244822 Mon Sep 17 00:00:00 2001 From: Zev Benjamin Date: Tue, 5 Mar 2013 11:05:12 -0500 Subject: [PATCH] tests: Move log int and log out functions into a common.js (imported from commit d6c2f45af775dea9c99426aa46b08ed45eb76d17) --- zephyr/tests/frontend/common.js | 47 +++++++++++++++++++++++++ zephyr/tests/frontend/tests/00-login.js | 10 ++++++ zephyr/tests/frontend/tests/01-site.js | 9 ++--- 3 files changed, 62 insertions(+), 4 deletions(-) create mode 100644 zephyr/tests/frontend/common.js diff --git a/zephyr/tests/frontend/common.js b/zephyr/tests/frontend/common.js new file mode 100644 index 0000000000..8b120dfdf6 --- /dev/null +++ b/zephyr/tests/frontend/common.js @@ -0,0 +1,47 @@ +var common = (function () { + +var exports = {}; + +exports.log_in = function () { + casper.start('http://localhost:9981/accounts/login', function () { + // Fail if we get a JavaScript error in the page's context. + // Based on the example at http://phantomjs.org/release-1.5.html + // + // casper.on('error') doesn't work (it never gets called) so we + // set this at the PhantomJS level. We do it inside 'start' so + // that we know we have a page object. + casper.page.onError = function (msg, trace) { + casper.test.error(msg); + casper.echo('Traceback:'); + trace.forEach(function (item) { + casper.echo(' ' + item.file + ':' + item.line); + }); + casper.exit(1); + }; + }); + + casper.then(function () { + casper.test.info('Logging in'); + casper.fill('form[action^="/accounts/login"]', { + username: 'iago@humbughq.com', + password: 'FlokrWdZefyEWkfI' + }, true /* submit form */); + }); +}; + +exports.log_out = function () { + casper.then(function () { + casper.test.info('Logging out'); + casper.click('li[title="Log out"] a'); + }); +}; + +return exports; + +}()); + +// For inclusion with CasperJS +try { + exports.common = common; +} catch (e) { +} \ No newline at end of file diff --git a/zephyr/tests/frontend/tests/00-login.js b/zephyr/tests/frontend/tests/00-login.js index de4774c78b..0d60837bfe 100644 --- a/zephyr/tests/frontend/tests/00-login.js +++ b/zephyr/tests/frontend/tests/00-login.js @@ -29,6 +29,16 @@ casper.then(function () { }, true /* submit form */); }); +casper.then(function () { + casper.test.info('Logging out'); + casper.click('li[title="Log out"] a'); +}); + +casper.then(function () { + casper.test.assertHttpStatus(200); + casper.test.assertUrlMatch(/accounts\/login\/$/); +}); + // Run the above queued actions. casper.run(function () { casper.test.done(); diff --git a/zephyr/tests/frontend/tests/01-site.js b/zephyr/tests/frontend/tests/01-site.js index 907bc561ae..854a7fed45 100644 --- a/zephyr/tests/frontend/tests/01-site.js +++ b/zephyr/tests/frontend/tests/01-site.js @@ -10,6 +10,8 @@ // For example, utils.dump() prints an Object with nice formatting. var utils = require('utils'); +var common = require('../common.js').common; + // Uncomment this to get page-context console.log in the CasperJS output // (plus some CasperJS-specific messages) /* @@ -119,10 +121,7 @@ function un_narrow() { keypress(27); // Esc } -casper.start('http://localhost:9981/', function () { - // URL like http://localhost:9981/ or http://localhost:9981/# - casper.test.assertUrlMatch(/^http:\/\/[^\/]+\/#?$/, 'On home page'); -}); +common.log_in(); casper.then(function () { casper.test.info('Sanity-checking existing messages'); @@ -322,6 +321,8 @@ casper.then(function() { }); +common.log_out(); + // Run the above queued actions. casper.run(function () { casper.test.done();