Remove race conditions in casper tests.

Chaining together `wait*` functions can create race conditions in the
frontend tests. To avoid race condition, we need to insert `then`
function between two `wait*` functions.
This commit is contained in:
Umair Khan 2016-09-08 17:10:41 +05:00 committed by Tim Abbott
parent 8fbbb3a471
commit d171e2c8d9
2 changed files with 127 additions and 85 deletions

View File

@ -141,46 +141,58 @@ function un_narrow() {
// Narrow by clicking links. // Narrow by clicking links.
common.wait_for_receive(function () { casper.then(function () {
casper.test.info('Narrowing by clicking stream'); common.wait_for_receive(function () {
casper.click('*[title="Narrow to stream \\\"Verona\\\""]'); casper.test.info('Narrowing by clicking stream');
casper.click('*[title="Narrow to stream \\\"Verona\\\""]');
});
}); });
casper.waitUntilVisible('#zfilt', function () { casper.then(function () {
expect_stream(); casper.waitUntilVisible('#zfilt', function () {
expect_stream();
});
}); });
casper.then(check_narrow_title('Verona - Zulip Dev - Zulip')); casper.then(check_narrow_title('Verona - Zulip Dev - Zulip'));
un_narrow(); un_narrow();
casper.waitUntilVisible('#zhome', function () { casper.then(function () {
expect_home(); casper.waitUntilVisible('#zhome', function () {
casper.test.info('Narrowing by clicking subject'); expect_home();
casper.click('*[title="Narrow to stream \\\"Verona\\\", topic \\\"frontend test\\\""]'); casper.test.info('Narrowing by clicking subject');
}); casper.click('*[title="Narrow to stream \\\"Verona\\\", topic \\\"frontend test\\\""]');
casper.then(check_narrow_title('frontend test - Zulip Dev - Zulip')); });
casper.waitUntilVisible('#zfilt', function () {
expect_stream_subject();
// This time, un-narrow by hitting the search 'x'
casper.test.info('Un-narrowing');
casper.click('#search_exit');
}); });
casper.waitUntilVisible('#zhome', function () { casper.then(function () {
expect_home(); check_narrow_title('frontend test - Zulip Dev - Zulip');
casper.test.info('Narrowing by clicking personal'); casper.waitUntilVisible('#zfilt', function () {
casper.click('*[title="Narrow to your private messages with Cordelia Lear, King Hamlet"]'); expect_stream_subject();
// This time, un-narrow by hitting the search 'x'
casper.test.info('Un-narrowing');
casper.click('#search_exit');
});
}); });
casper.then(check_narrow_title('private - Zulip Dev - Zulip')); casper.then(function () {
casper.waitUntilVisible('#zhome', function () {
expect_home();
casper.test.info('Narrowing by clicking personal');
casper.click('*[title="Narrow to your private messages with Cordelia Lear, King Hamlet"]');
});
});
casper.waitUntilVisible('#zfilt', function () { casper.then(function () {
expect_huddle(); check_narrow_title('private - Zulip Dev - Zulip');
casper.waitUntilVisible('#zfilt', function () {
expect_huddle();
// Un-narrow by clicking "Zulip" // Un-narrow by clicking "Zulip"
casper.test.info('Un-narrowing'); casper.test.info('Un-narrowing');
casper.click('.brand'); casper.click('.brand');
});
}); });
@ -195,27 +207,33 @@ function search_and_check(str, item, check, narrow_title) {
function search_silent_user(str, item) { function search_silent_user(str, item) {
common.select_item_via_typeahead('#search_query', str, item); common.select_item_via_typeahead('#search_query', str, item);
casper.waitUntilVisible('#silent_user', function () { casper.then(function () {
casper.test.info("Empty feed for silent user visible."); casper.waitUntilVisible('#silent_user', function () {
var expected_message = "\n You haven't received any messages sent by this user yet!"+ casper.test.info("Empty feed for silent user visible.");
"\n "; var expected_message = "\n You haven't received any messages sent by this user yet!"+
this.test.assertEquals(casper.fetchText('#silent_user'), expected_message); "\n ";
this.test.assertEquals(casper.fetchText('#silent_user'), expected_message);
});
}); });
un_narrow(); un_narrow();
} }
function search_non_existing_user(str, item) { function search_non_existing_user(str, item) {
common.select_item_via_typeahead('#search_query', str, item); common.select_item_via_typeahead('#search_query', str, item);
casper.waitUntilVisible('#non_existing_user', function () { casper.then(function () {
casper.test.info("Empty feed for non existing user visible."); casper.waitUntilVisible('#non_existing_user', function () {
var expected_message = "\n This user does not exist!"+ casper.test.info("Empty feed for non existing user visible.");
"\n "; var expected_message = "\n This user does not exist!"+
this.test.assertEquals(casper.fetchText('#non_existing_user'), expected_message); "\n ";
this.test.assertEquals(casper.fetchText('#non_existing_user'), expected_message);
});
}); });
un_narrow(); un_narrow();
} }
casper.waitUntilVisible('#zhome', expect_home); casper.then(function () {
casper.waitUntilVisible('#zhome', expect_home);
});
// Test stream / recipient autocomplete in the search bar // Test stream / recipient autocomplete in the search bar
search_and_check('Verona', 'Narrow to stream', expect_stream, search_and_check('Verona', 'Narrow to stream', expect_stream,

View File

@ -19,60 +19,78 @@ casper.waitForText("And reply to this message", function () {
casper.page.sendEvent('keypress', "c"); casper.page.sendEvent('keypress', "c");
}); });
casper.waitUntilVisible('#compose', function () { casper.then(function () {
casper.test.assertVisible('#stream', 'Stream input box visible'); casper.waitUntilVisible('#compose', function () {
common.check_form('#send_message_form', {stream: '', subject: ''}, "Stream empty on new compose"); casper.test.assertVisible('#stream', 'Stream input box visible');
casper.click('body'); common.check_form('#send_message_form', {stream: '', subject: ''}, "Stream empty on new compose");
casper.page.sendEvent('keypress', "C"); casper.click('body');
casper.page.sendEvent('keypress', "C");
});
}); });
casper.waitUntilVisible('#private_message_recipient', function () { casper.then(function () {
common.check_form('#send_message_form', {recipient: ''}, "Recipient empty on new PM"); casper.waitUntilVisible('#private_message_recipient', function () {
casper.click('body'); common.check_form('#send_message_form', {recipient: ''}, "Recipient empty on new PM");
casper.page.sendEvent('keypress', 'c'); casper.click('body');
casper.page.sendEvent('keypress', 'c');
});
}); });
casper.waitUntilVisible('#stream', function () { casper.then(function () {
common.check_form('#send_message_form', {stream: '', subject: ''}, "Stream empty on new compose"); casper.waitUntilVisible('#stream', function () {
common.check_form('#send_message_form', {stream: '', subject: ''}, "Stream empty on new compose");
// Check that when you reply to a message it pre-populates the stream and subject fields // Check that when you reply to a message it pre-populates the stream and subject fields
casper.click('body'); casper.click('body');
});
}); });
casper.waitWhileVisible('#stream', function () { casper.then(function () {
casper.clickLabel("We reply to this message"); casper.waitWhileVisible('#stream', function () {
casper.clickLabel("We reply to this message");
});
}); });
casper.waitUntilVisible('#stream', function () { casper.then(function () {
common.check_form('#send_message_form', {stream: "Verona", subject: "Reply test"}, "Stream populated after reply by click"); casper.waitUntilVisible('#stream', function () {
// Or recipient field common.check_form('#send_message_form', {stream: "Verona", subject: "Reply test"}, "Stream populated after reply by click");
casper.click('body'); // Or recipient field
casper.clickLabel("And reply to this message"); casper.click('body');
casper.clickLabel("And reply to this message");
});
}); });
casper.waitUntilVisible('#private_message_recipient', function () { casper.then(function () {
common.check_form('#send_message_form', {recipient: "cordelia@zulip.com"}, "Recipient populated after PM click"); casper.waitUntilVisible('#private_message_recipient', function () {
common.check_form('#send_message_form', {recipient: "cordelia@zulip.com"}, "Recipient populated after PM click");
common.keypress(27); //escape common.keypress(27); //escape
casper.page.sendEvent('keypress', 'k'); casper.page.sendEvent('keypress', 'k');
casper.page.sendEvent('keypress', 'r'); casper.page.sendEvent('keypress', 'r');
});
}); });
casper.waitUntilVisible('#stream', function () { casper.then(function () {
common.check_form('#send_message_form', {stream: "Verona", subject: "Reply test"}, "Stream populated after reply with `r`"); casper.waitUntilVisible('#stream', function () {
common.check_form('#send_message_form', {stream: "Verona", subject: "Reply test"}, "Stream populated after reply with `r`");
// Test "closing" the compose box // Test "closing" the compose box
casper.click('body'); casper.click('body');
});
}); });
casper.waitWhileVisible('#stream', function () { casper.then(function () {
casper.test.assertNotVisible('#stream', 'Close stream compose box'); casper.waitWhileVisible('#stream', function () {
casper.page.sendEvent('keypress', "C"); casper.test.assertNotVisible('#stream', 'Close stream compose box');
casper.click('body'); casper.page.sendEvent('keypress', "C");
casper.click('body');
});
}); });
casper.waitWhileVisible('#private-message', function () { casper.then(function () {
casper.test.assertNotVisible('#private-message', 'Close PM compose box'); casper.waitWhileVisible('#private-message', function () {
casper.test.assertNotVisible('#private-message', 'Close PM compose box');
});
}); });
// Test focus after narrowing to PMs with a user and typing 'c' // Test focus after narrowing to PMs with a user and typing 'c'
@ -82,10 +100,13 @@ casper.then(function () {
casper.waitUntilVisible('#tab_list li.private_message', function () { casper.waitUntilVisible('#tab_list li.private_message', function () {
casper.page.sendEvent('keypress', 'c'); casper.page.sendEvent('keypress', 'c');
}); });
casper.waitUntilVisible('#compose', function () {
casper.test.assertEval(function () { casper.then(function () {
return document.activeElement === $('#stream')[0]; casper.waitUntilVisible('#compose', function () {
}, 'Stream box focused after narrowing to PMs with a user and pressing `c`'); casper.test.assertEval(function () {
return document.activeElement === $('#stream')[0];
}, 'Stream box focused after narrowing to PMs with a user and pressing `c`');
});
}); });
// Make sure multiple PM recipients display properly. // Make sure multiple PM recipients display properly.
@ -107,14 +128,17 @@ casper.then(function () {
casper.waitUntilVisible('#zhome', function () { casper.waitUntilVisible('#zhome', function () {
casper.clickLabel('A huddle to check spaces'); casper.clickLabel('A huddle to check spaces');
}); });
casper.waitUntilVisible('#compose', function () {
// It may be possible to get the textbox contents with CasperJS, casper.then(function () {
// but it's easier to just evaluate jQuery in page context here. casper.waitUntilVisible('#compose', function () {
var displayed_recipients = casper.evaluate(function () { // It may be possible to get the textbox contents with CasperJS,
return $('#private_message_recipient').val(); // but it's easier to just evaluate jQuery in page context here.
var displayed_recipients = casper.evaluate(function () {
return $('#private_message_recipient').val();
});
casper.test.assertEquals(displayed_recipients, recipients.join(', '),
'Recipients are displayed correctly in a huddle reply');
}); });
casper.test.assertEquals(displayed_recipients, recipients.join(', '),
'Recipients are displayed correctly in a huddle reply');
}); });
common.then_log_out(); common.then_log_out();