2012-11-09 00:44:00 +01:00
|
|
|
/* Script for testing the web client.
|
|
|
|
|
|
|
|
This runs under CasperJS. It's an end-to-end black-box sort of test. It
|
|
|
|
simulates clicking around in the app, sending messages, etc. We run against
|
|
|
|
a real development server instance and avoid mocking as much as possible.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Provides a few utility functions.
|
|
|
|
// See http://casperjs.org/api.html#utils
|
|
|
|
// For example, utils.dump() prints an Object with nice formatting.
|
|
|
|
var utils = require('utils');
|
|
|
|
|
2013-03-05 17:05:12 +01:00
|
|
|
var common = require('../common.js').common;
|
|
|
|
|
2013-01-09 00:16:04 +01:00
|
|
|
// Uncomment this to get page-context console.log in the CasperJS output
|
|
|
|
// (plus some CasperJS-specific messages)
|
|
|
|
/*
|
|
|
|
casper.on('remote.message', function (msg) {
|
|
|
|
casper.echo(msg);
|
|
|
|
});
|
|
|
|
*/
|
|
|
|
|
2012-11-09 00:44:00 +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).
|
|
|
|
function get_rendered_messages(table) {
|
|
|
|
return casper.evaluate(function (table) {
|
|
|
|
var tbl = $('#'+table);
|
|
|
|
return {
|
|
|
|
headings: $.map(tbl.find('.recipient_row .right_part'), function (elem) {
|
|
|
|
return elem.innerText;
|
|
|
|
}),
|
|
|
|
|
|
|
|
bodies: $.map(tbl.find('.message_content'), function (elem) {
|
|
|
|
return elem.innerHTML;
|
|
|
|
})
|
|
|
|
};
|
|
|
|
}, {
|
|
|
|
table: table
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2012-12-12 20:12:27 +01:00
|
|
|
// Inject key presses by running some jQuery code in page context.
|
|
|
|
// If we upgrade to CasperJS 1.0 and PhantomJS 1.7+, we can do this
|
|
|
|
// in a more straightforward way.
|
|
|
|
function keypress(code) {
|
|
|
|
casper.evaluate(function (code) {
|
|
|
|
$('body').trigger($.Event('keydown', { which: code }));
|
|
|
|
}, {
|
|
|
|
code: code
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2012-11-14 00:13:53 +01:00
|
|
|
function timestamp() {
|
|
|
|
return new Date().getTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
// The timestamp of the last message send or get_updates result.
|
|
|
|
var last_send_or_update = -1;
|
|
|
|
|
|
|
|
// Update that variable whenever get_updates returns.
|
|
|
|
casper.on('resource.received', function (resource) {
|
2013-03-28 23:17:20 +01:00
|
|
|
if (/\/json\/get_events/.test(resource.url)) {
|
2012-11-14 00:13:53 +01:00
|
|
|
last_send_or_update = timestamp();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2012-11-09 00:44:00 +01:00
|
|
|
// Send a Humbug message.
|
|
|
|
function send_message(type, params) {
|
2012-11-14 00:13:53 +01:00
|
|
|
last_send_or_update = timestamp();
|
|
|
|
|
2013-03-05 20:52:54 +01:00
|
|
|
common.send_message(type, params);
|
2012-11-09 00:44:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for any previous send to finish, then send a message.
|
|
|
|
function wait_and_send(type, params) {
|
|
|
|
casper.waitForSelector('#compose-send-button:enabled', function () {
|
|
|
|
send_message(type, params);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait to receive queued messages.
|
|
|
|
function wait_for_receive(step) {
|
2013-01-02 22:58:33 +01:00
|
|
|
// Wait until the last send or get_updates result was more than 300 ms ago.
|
2012-11-14 00:13:53 +01:00
|
|
|
casper.waitFor(function () {
|
2013-01-02 22:30:10 +01:00
|
|
|
return (timestamp() - last_send_or_update) > 300;
|
2012-11-14 00:13:53 +01:00
|
|
|
}, step);
|
2012-11-09 00:44:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// innerText sometimes gives us non-breaking space characters, and occasionally
|
|
|
|
// a different number of spaces than we expect.
|
|
|
|
function normalize_spaces(str) {
|
|
|
|
return str.replace(/\s+/g, ' ');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call get_rendered_messages and then check that the last few headings and
|
|
|
|
// bodies match the specified arrays.
|
|
|
|
function expected_messages(table, headings, bodies) {
|
|
|
|
casper.test.assertVisible('#'+table,
|
|
|
|
table + ' is visible');
|
|
|
|
|
|
|
|
var msg = get_rendered_messages(table);
|
|
|
|
|
|
|
|
casper.test.assertEquals(
|
|
|
|
msg.headings.slice(-headings.length).map(normalize_spaces),
|
|
|
|
headings,
|
|
|
|
'Got expected message headings');
|
|
|
|
|
|
|
|
casper.test.assertEquals(
|
|
|
|
msg.bodies.slice(-bodies.length),
|
|
|
|
bodies,
|
|
|
|
'Got expected message bodies');
|
|
|
|
}
|
|
|
|
|
|
|
|
function un_narrow() {
|
|
|
|
casper.test.info('Un-narrowing');
|
2012-12-12 20:22:18 +01:00
|
|
|
keypress(27); // Esc
|
2012-11-09 00:44:00 +01:00
|
|
|
}
|
|
|
|
|
2013-03-05 20:52:54 +01:00
|
|
|
common.start_and_log_in();
|
2012-11-09 00:44:00 +01:00
|
|
|
|
2013-03-05 16:32:55 +01:00
|
|
|
casper.then(function () {
|
2012-11-09 00:44:00 +01:00
|
|
|
casper.test.info('Sanity-checking existing messages');
|
|
|
|
|
|
|
|
var msg = get_rendered_messages('zhome');
|
|
|
|
|
|
|
|
msg.headings.forEach(function (heading) {
|
|
|
|
casper.test.assertMatch(normalize_spaces(heading),
|
2013-02-15 18:14:59 +01:00
|
|
|
/(^You and )|( > )/,
|
2012-11-09 00:44:00 +01:00
|
|
|
'Heading is well-formed');
|
|
|
|
});
|
|
|
|
|
|
|
|
msg.bodies.forEach(function (body) {
|
|
|
|
casper.test.assertMatch(body,
|
|
|
|
/^(<p>(.|\n)*<\/p>)?$/,
|
|
|
|
'Body is well-formed');
|
|
|
|
});
|
|
|
|
|
2013-03-18 18:40:47 +01:00
|
|
|
casper.test.info('Sending messages');
|
2013-03-12 16:05:46 +01:00
|
|
|
});
|
|
|
|
|
2013-02-28 18:01:08 +01:00
|
|
|
wait_and_send('stream', {
|
|
|
|
stream: 'Verona',
|
|
|
|
subject: 'frontend test',
|
|
|
|
content: 'test message A'
|
2012-11-09 00:44:00 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
wait_and_send('stream', {
|
|
|
|
stream: 'Verona',
|
|
|
|
subject: 'frontend test',
|
|
|
|
content: 'test message B'
|
|
|
|
});
|
|
|
|
|
|
|
|
wait_and_send('stream', {
|
|
|
|
stream: 'Verona',
|
|
|
|
subject: 'other subject',
|
|
|
|
content: 'test message C'
|
|
|
|
});
|
|
|
|
|
2012-11-08 00:38:21 +01:00
|
|
|
wait_and_send('private', {
|
2012-11-09 00:44:00 +01:00
|
|
|
recipient: 'cordelia@humbughq.com, hamlet@humbughq.com',
|
|
|
|
content: 'personal A'
|
|
|
|
});
|
|
|
|
|
2012-11-08 00:38:21 +01:00
|
|
|
wait_and_send('private', {
|
2012-11-09 00:44:00 +01:00
|
|
|
recipient: 'cordelia@humbughq.com, hamlet@humbughq.com',
|
|
|
|
content: 'personal B'
|
|
|
|
});
|
|
|
|
|
2012-11-08 00:38:21 +01:00
|
|
|
wait_and_send('private', {
|
2012-11-09 00:44:00 +01:00
|
|
|
recipient: 'cordelia@humbughq.com',
|
|
|
|
content: 'personal C'
|
|
|
|
});
|
|
|
|
|
|
|
|
wait_for_receive(function () {
|
|
|
|
expected_messages('zhome', [
|
2013-02-15 16:47:09 +01:00
|
|
|
'Verona > frontend test',
|
|
|
|
'Verona > other subject',
|
2012-11-09 00:44:00 +01:00
|
|
|
'You and Cordelia Lear, King Hamlet',
|
|
|
|
'You and Cordelia Lear'
|
|
|
|
], [
|
|
|
|
'<p>test message A</p>',
|
|
|
|
'<p>test message B</p>',
|
|
|
|
'<p>test message C</p>',
|
|
|
|
'<p>personal A</p>',
|
|
|
|
'<p>personal B</p>',
|
|
|
|
'<p>personal C</p>'
|
|
|
|
]);
|
|
|
|
|
|
|
|
casper.test.info('Sending more messages');
|
|
|
|
|
|
|
|
send_message('stream', {
|
|
|
|
stream: 'Verona',
|
|
|
|
subject: 'frontend test',
|
|
|
|
content: 'test message D'
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2012-11-08 00:38:21 +01:00
|
|
|
wait_and_send('private', {
|
2012-11-09 00:44:00 +01:00
|
|
|
recipient: 'cordelia@humbughq.com, hamlet@humbughq.com',
|
|
|
|
content: 'personal D'
|
|
|
|
});
|
|
|
|
|
|
|
|
wait_for_receive(function () {
|
|
|
|
casper.test.info('Narrowing to stream');
|
|
|
|
casper.click('*[title="Narrow to stream \\\"Verona\\\""]');
|
|
|
|
});
|
|
|
|
|
|
|
|
casper.then(function () {
|
|
|
|
expected_messages('zfilt', [
|
2013-02-15 16:47:09 +01:00
|
|
|
'Verona > frontend test',
|
|
|
|
'Verona > other subject',
|
2013-02-19 04:31:25 +01:00
|
|
|
'Verona > frontend test'
|
2012-11-09 00:44:00 +01:00
|
|
|
], [
|
|
|
|
'<p>test message A</p>',
|
|
|
|
'<p>test message B</p>',
|
|
|
|
'<p>test message C</p>',
|
2013-02-19 04:31:25 +01:00
|
|
|
'<p>test message D</p>'
|
2012-11-09 00:44:00 +01:00
|
|
|
]);
|
|
|
|
|
|
|
|
un_narrow();
|
|
|
|
});
|
|
|
|
|
|
|
|
casper.then(function () {
|
|
|
|
expected_messages('zhome', [
|
2013-02-15 16:47:09 +01:00
|
|
|
'Verona > frontend test',
|
2012-11-09 00:44:00 +01:00
|
|
|
'You and Cordelia Lear, King Hamlet'
|
|
|
|
], [
|
|
|
|
'<p>test message D</p>',
|
|
|
|
'<p>personal D</p>'
|
|
|
|
]);
|
|
|
|
|
|
|
|
casper.test.info('Narrowing to subject');
|
|
|
|
casper.click('*[title="Narrow to stream \\\"Verona\\\", subject \\\"frontend test\\\""]');
|
|
|
|
});
|
|
|
|
|
|
|
|
casper.then(function () {
|
|
|
|
expected_messages('zfilt', [
|
2013-02-19 04:31:25 +01:00
|
|
|
'Verona > frontend test'
|
2012-11-09 00:44:00 +01:00
|
|
|
], [
|
|
|
|
'<p>test message A</p>',
|
|
|
|
'<p>test message B</p>',
|
2013-02-19 04:31:25 +01:00
|
|
|
'<p>test message D</p>'
|
2012-11-09 00:44:00 +01:00
|
|
|
]);
|
|
|
|
|
|
|
|
un_narrow();
|
|
|
|
});
|
|
|
|
|
|
|
|
casper.then(function () {
|
|
|
|
casper.test.info('Narrowing to personals');
|
|
|
|
casper.click('*[title="Narrow to your private messages with Cordelia Lear, King Hamlet"]');
|
|
|
|
});
|
|
|
|
|
|
|
|
casper.then(function () {
|
|
|
|
expected_messages('zfilt', [
|
2013-02-19 04:31:25 +01:00
|
|
|
'You and Cordelia Lear, King Hamlet'
|
2012-11-09 00:44:00 +01:00
|
|
|
], [
|
|
|
|
'<p>personal A</p>',
|
|
|
|
'<p>personal B</p>',
|
2013-02-19 04:31:25 +01:00
|
|
|
'<p>personal D</p>'
|
2012-11-09 00:44:00 +01:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
2013-03-05 20:52:54 +01:00
|
|
|
common.then_log_out();
|
2013-03-05 17:05:12 +01:00
|
|
|
|
2012-11-09 00:44:00 +01:00
|
|
|
// Run the above queued actions.
|
|
|
|
casper.run(function () {
|
2012-12-07 17:11:12 +01:00
|
|
|
casper.test.done();
|
2012-11-09 00:44:00 +01:00
|
|
|
});
|