2013-03-05 17:05:12 +01:00
|
|
|
var common = (function () {
|
|
|
|
|
|
|
|
var exports = {};
|
|
|
|
|
2015-10-14 00:21:23 +02:00
|
|
|
var test_credentials = require('../casper_lib/test_credentials.js').test_credentials;
|
2015-08-20 02:57:30 +02:00
|
|
|
|
2013-03-20 23:59:44 +01:00
|
|
|
function timestamp() {
|
|
|
|
return new Date().getTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
// The timestamp of the last message send or get_events result.
|
|
|
|
var last_send_or_update = -1;
|
|
|
|
|
2013-03-05 20:52:54 +01:00
|
|
|
function log_in(credentials) {
|
|
|
|
if (credentials === undefined) {
|
2015-08-20 02:57:30 +02:00
|
|
|
credentials = test_credentials.default_user;
|
2013-03-05 20:52:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
casper.test.info('Logging in');
|
|
|
|
casper.fill('form[action^="/accounts/login"]', {
|
|
|
|
username: credentials.username,
|
|
|
|
password: credentials.password
|
|
|
|
}, true /* submit form */);
|
|
|
|
}
|
2013-03-05 17:05:12 +01:00
|
|
|
|
2013-03-20 23:09:46 +01:00
|
|
|
exports.initialize_casper = function (viewport) {
|
2014-03-07 23:46:33 +01:00
|
|
|
if (casper.zulip_initialized !== undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
casper.zulip_initialized = true;
|
2013-03-20 23:09:46 +01:00
|
|
|
// These initialization steps will fail if they run before
|
|
|
|
// casper.start has been called.
|
|
|
|
|
|
|
|
// Set default viewport size to something reasonable
|
|
|
|
casper.page.viewportSize = viewport || {width: 1280, height: 768};
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
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);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Capture screens from all failures
|
|
|
|
var casper_failure_count = 1;
|
|
|
|
casper.test.on('fail', function failure() {
|
|
|
|
if (casper_failure_count <= 10) {
|
|
|
|
casper.capture("/tmp/casper-failure" + casper_failure_count + ".png");
|
|
|
|
casper_failure_count++;
|
|
|
|
}
|
|
|
|
});
|
2013-03-20 23:59:44 +01:00
|
|
|
|
|
|
|
// Update last_send_or_update whenever get_events returns.
|
|
|
|
casper.on('resource.received', function (resource) {
|
|
|
|
if (/\/json\/get_events/.test(resource.url)) {
|
|
|
|
last_send_or_update = timestamp();
|
|
|
|
}
|
|
|
|
});
|
2014-03-10 22:48:47 +01:00
|
|
|
|
2014-03-07 00:44:15 +01:00
|
|
|
casper.on('load.finished', function () {
|
|
|
|
casper.evaluateOrDie(function () {
|
|
|
|
$(document).trigger($.Event('phantom_page_loaded'));
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-03-10 22:48:47 +01:00
|
|
|
casper.evaluate(function () {
|
|
|
|
window.localStorage.clear();
|
|
|
|
});
|
2013-03-20 23:09:46 +01:00
|
|
|
};
|
|
|
|
|
2013-03-05 20:52:54 +01:00
|
|
|
exports.then_log_in = function (credentials) {
|
2013-03-05 17:05:12 +01:00
|
|
|
casper.then(function () {
|
2013-03-05 20:52:54 +01:00
|
|
|
log_in(credentials);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.start_and_log_in = function (credentials, viewport) {
|
|
|
|
casper.start('http://localhost:9981/accounts/login', function () {
|
2013-03-20 23:09:46 +01:00
|
|
|
exports.initialize_casper(viewport);
|
2013-03-05 20:52:54 +01:00
|
|
|
log_in(credentials);
|
2013-03-05 17:05:12 +01:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-03-05 20:52:54 +01:00
|
|
|
exports.then_log_out = function () {
|
2013-03-05 17:05:12 +01:00
|
|
|
casper.then(function () {
|
|
|
|
casper.test.info('Logging out');
|
|
|
|
casper.click('li[title="Log out"] a');
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-03-22 00:52:38 +01:00
|
|
|
exports.enable_page_console = function () {
|
|
|
|
// Call this (after casper.start) to enable printing page-context
|
|
|
|
// console.log (plus some CasperJS-specific messages) to the
|
|
|
|
// terminal.
|
|
|
|
casper.on('remote.message', function (msg) {
|
|
|
|
casper.echo(msg);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-06-11 23:08:11 +02:00
|
|
|
exports.check_form = function (form_selector, expected, test_name) {
|
|
|
|
var values = casper.getFormValues(form_selector);
|
|
|
|
var k;
|
|
|
|
for (k in expected) {
|
|
|
|
if (expected.hasOwnProperty(k)) {
|
|
|
|
casper.test.assertEqual(values[k], expected[k],
|
|
|
|
test_name ? (test_name + ": " + k) : undefined);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-03-07 23:05:26 +01:00
|
|
|
// Wait for any previous send to finish, then send a message.
|
2014-03-07 20:59:30 +01:00
|
|
|
exports.then_send_message = function (type, params) {
|
2014-03-07 23:05:26 +01:00
|
|
|
casper.waitForSelector('#compose-send-button:enabled');
|
2013-05-15 20:06:06 +02:00
|
|
|
casper.waitForSelector('#new_message_content', function () {
|
|
|
|
if(type === "stream") {
|
|
|
|
casper.page.sendEvent('keypress', "c");
|
|
|
|
}
|
|
|
|
else if (type === "private") {
|
|
|
|
casper.page.sendEvent('keypress', "C");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
casper.test.assertTrue(false, "send_message got valid message type");
|
|
|
|
}
|
2015-12-01 00:18:33 +01:00
|
|
|
casper.fill('form[action^="/json/messages"]', params);
|
2013-03-05 20:52:54 +01:00
|
|
|
casper.click('#compose-send-button');
|
2014-03-07 20:58:18 +01:00
|
|
|
});
|
|
|
|
casper.waitFor(function emptyComposeBox() {
|
2015-12-01 00:18:33 +01:00
|
|
|
return casper.getFormValues('form[action^="/json/messages"]').content === '';
|
2014-03-07 20:58:18 +01:00
|
|
|
}, function () {
|
|
|
|
last_send_or_update = timestamp();
|
2013-03-05 20:52:54 +01:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-03-20 23:59:44 +01:00
|
|
|
// Get message headings (recipient rows) and bodies out of the DOM.
|
|
|
|
// casper.evaluate plays weird tricks with a closure, evaluating
|
|
|
|
// it in the web page's context. Passing arguments from the test
|
|
|
|
// script's context is awkward (c.f. the various appearances of
|
|
|
|
// 'table' here).
|
|
|
|
exports.get_rendered_messages = function (table) {
|
|
|
|
return casper.evaluate(function (table) {
|
|
|
|
var tbl = $('#'+table);
|
|
|
|
return {
|
2014-02-21 17:31:04 +01:00
|
|
|
headings: $.map(tbl.find('.recipient_row .message-header-contents'), function (elem) {
|
2013-03-20 23:59:44 +01:00
|
|
|
return elem.innerText;
|
|
|
|
}),
|
|
|
|
|
|
|
|
bodies: $.map(tbl.find('.message_content'), function (elem) {
|
|
|
|
return elem.innerHTML;
|
|
|
|
})
|
|
|
|
};
|
|
|
|
}, {
|
|
|
|
table: table
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-02-19 16:07:28 +01:00
|
|
|
exports.get_form_field_value = function (selector) {
|
|
|
|
return casper.evaluate(function (selector) {
|
|
|
|
return $(selector).val();
|
|
|
|
}, selector);
|
|
|
|
};
|
|
|
|
|
2013-03-20 23:59:44 +01:00
|
|
|
// Inject key presses by running some jQuery code in page context.
|
2013-03-21 00:07:30 +01:00
|
|
|
// PhantomJS and CasperJS don't provide a clean way to insert key
|
|
|
|
// presses by code, only strings of printable characters.
|
2013-03-20 23:59:44 +01:00
|
|
|
exports.keypress = function (code) {
|
|
|
|
casper.evaluate(function (code) {
|
|
|
|
$('body').trigger($.Event('keydown', { which: code }));
|
|
|
|
}, {
|
|
|
|
code: code
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-03-07 23:05:26 +01:00
|
|
|
// Send a whole list of messages using then_send_message.
|
2014-03-07 20:59:30 +01:00
|
|
|
exports.then_send_many = function (msgs) {
|
2013-03-21 17:46:52 +01:00
|
|
|
msgs.forEach(function (msg) {
|
2014-03-07 23:05:26 +01:00
|
|
|
exports.then_send_message(
|
2013-03-21 17:46:52 +01:00
|
|
|
(msg.stream !== undefined) ? 'stream' : 'private',
|
|
|
|
msg);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-03-20 23:59:44 +01:00
|
|
|
// Wait to receive queued messages.
|
|
|
|
exports.wait_for_receive = function (step) {
|
2013-05-21 19:38:41 +02:00
|
|
|
// Wait until the last send or get_events result was more than 1000 ms ago.
|
2013-03-20 23:59:44 +01:00
|
|
|
casper.waitFor(function () {
|
2013-05-21 19:38:41 +02:00
|
|
|
return (timestamp() - last_send_or_update) > 1000;
|
2013-03-20 23:59:44 +01:00
|
|
|
}, step);
|
|
|
|
};
|
|
|
|
|
2013-08-22 01:44:51 +02:00
|
|
|
// Wait until the loading spinner goes away (helpful just after logging in).
|
|
|
|
exports.wait_for_load = function (step) {
|
2013-10-11 01:20:39 +02:00
|
|
|
casper.waitWhileVisible('#page_loading_indicator', step);
|
2013-08-22 01:44:51 +02:00
|
|
|
};
|
|
|
|
|
2013-03-20 23:59:44 +01:00
|
|
|
// innerText sometimes gives us non-breaking space characters, and occasionally
|
|
|
|
// a different number of spaces than we expect.
|
|
|
|
exports.normalize_spaces = function (str) {
|
|
|
|
return str.replace(/\s+/g, ' ');
|
|
|
|
};
|
|
|
|
|
2013-08-23 01:04:34 +02:00
|
|
|
exports.ltrim = function (str) {
|
|
|
|
return str.replace(/^\s+/g, '');
|
|
|
|
};
|
|
|
|
|
2014-01-07 17:23:14 +01:00
|
|
|
exports.rtrim = function (str) {
|
|
|
|
return str.replace(/\s+$/g, '');
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.trim = function (str) {
|
|
|
|
return exports.rtrim(exports.ltrim(str));
|
|
|
|
};
|
|
|
|
|
2013-03-20 23:59:44 +01:00
|
|
|
// Call get_rendered_messages and then check that the last few headings and
|
|
|
|
// bodies match the specified arrays.
|
|
|
|
exports.expected_messages = function (table, headings, bodies) {
|
|
|
|
casper.test.assertVisible('#'+table,
|
|
|
|
table + ' is visible');
|
|
|
|
|
|
|
|
var msg = exports.get_rendered_messages(table);
|
|
|
|
|
|
|
|
casper.test.assertEquals(
|
2014-01-07 17:23:14 +01:00
|
|
|
msg.headings.slice(-headings.length).map(exports.normalize_spaces).map(exports.trim),
|
|
|
|
headings.map(exports.trim),
|
2013-03-20 23:59:44 +01:00
|
|
|
'Got expected message headings');
|
|
|
|
|
|
|
|
casper.test.assertEquals(
|
|
|
|
msg.bodies.slice(-bodies.length),
|
|
|
|
bodies,
|
|
|
|
'Got expected message bodies');
|
|
|
|
};
|
2013-03-05 20:52:54 +01:00
|
|
|
|
2013-03-29 00:03:11 +01:00
|
|
|
exports.un_narrow = function () {
|
|
|
|
casper.test.info('Un-narrowing');
|
|
|
|
common.keypress(27); // Esc
|
|
|
|
};
|
|
|
|
|
2013-03-05 17:05:12 +01:00
|
|
|
return exports;
|
|
|
|
|
|
|
|
}());
|
|
|
|
|
|
|
|
// For inclusion with CasperJS
|
|
|
|
try {
|
|
|
|
exports.common = common;
|
|
|
|
} catch (e) {
|
2013-03-05 20:52:54 +01:00
|
|
|
}
|