Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
puppeteer: Add common methods needed for 02-messages-basics.js.
Dinesh535fce8c4572
diff --git a/frontend_tests/puppeteer_lib/common.js b/frontend_tests/puppeteer_lib/common.jsindex 55b3b2fdc4c..e335ffc3154 100644--- a/frontend_tests/puppeteer_lib/common.js+++ b/frontend_tests/puppeteer_lib/common.js@@ -47,6 +47,11 @@ class CommonUtils {});}+ async set_pm_recipient(page, recipient) {+ await page.type("#private_message_recipient", recipient);+ await page.keyboard.press("Enter");+ }+/*** This function takes a params object whose fields* are referenced by name attribute of an input field and@@ -120,6 +125,126 @@ class CommonUtils {assert(page.url().includes('/login/'));}+ async ensure_enter_does_not_send(page) {+ await page.$eval("#enter_sends", (el) => {+ if (el.checked) {+ el.click();+ }+ });+ }++ async wait_for_fully_processed_message(page, content) {+ await page.waitFor((content) => {+ /*+ The tricky part about making sure that+ a message has actually been fully processed+ is that we'll "locally echo" the message+ first on the client. Until the server+ actually acks the message, the message will+ have a temporary id and will not have all+ the normal message controls.+ For the Casper tests, we want to avoid all+ the edge cases with locally echoed messages.+ In order to make sure a message is processed,+ we use internals to determine the following:+ - has message_list even been updated with+ the message with out content?+ - has the locally_echoed flag been cleared?+ But for the final steps we look at the+ actual DOM (via JQuery):+ - is it visible?+ - does it look to have been+ re-rendered based on server info?+ */+ const last_msg = current_msg_list.last();+ if (last_msg.raw_content !== content) {+ return false;+ }++ if (last_msg.locally_echoed) {+ return false;+ }++ const row = rows.last_visible();+ if (rows.id(row) !== last_msg.id) {+ return false;+ }++ /*+ Make sure the message is completely+ re-rendered from its original "local echo"+ version by looking for the star icon. We+ don't add the star icon until the server+ responds.+ */+ return row.find('.star').length === 1;+ }, {}, content);+ }++ // Wait for any previous send to finish, then send a message.+ async send_message(page, type, params) {+ // If a message is outside the view, we do not need+ // to wait for it to be processed later.+ const { outside_view } = params;+ delete params.outside_view;++ await page.waitForSelector('#compose-textarea');++ if (type === "stream") {+ await page.keyboard.press('KeyC');+ } else if (type === "private") {+ await page.keyboard.press("KeyX");+ const recipients = params.recipient.split(', ');+ for (let i = 0; i < recipients.length; i += 1) {+ await this.set_pm_recipient(page, recipients[i]);+ }+ delete params.recipient;+ } else {+ assert.fail("`send_message` got invalid message type");+ }++ if (params.stream) {+ params.stream_message_recipient_stream = params.stream;+ delete params.stream;+ }++ if (params.topic) {+ params.stream_message_recipient_topic = params.topic;+ delete params.topic;+ }++ await this.fill_form(page, 'form[action^="/json/messages"]', params);+ await this.ensure_enter_does_not_send(page);+ await page.waitForSelector("#compose-send-button", {visible: true});+ await page.click('#compose-send-button');++ // confirm if compose box is empty.+ const compose_box_element = await page.$("#compose-textarea");+ const compose_box_content = await page.evaluate(element => element.textContent,+ compose_box_element);+ assert.equal(compose_box_content, '', 'Compose box not empty after message sent');++ if (!outside_view) {+ await this.wait_for_fully_processed_message(page, params.content);+ }++ // Close the compose box after sending the message.+ await page.evaluate(() => {+ compose_actions.cancel();+ });+ }++ async send_multiple_messages(page, msgs) {+ for (let msg_index = 0; msg_index < msgs.length; msg_index += 1) {+ const msg = msgs[msg_index];+ await this.send_message(+ page,+ msg.stream !== undefined ? 'stream' : 'private',+ msg+ );+ }+ }+async run_test(test_function) {// Pass a page instance to test so we can take// a screenshot of it when the test fails.