2021-02-20 05:52:06 +01:00
|
|
|
import {strict as assert} from "assert";
|
|
|
|
import "css.escape";
|
|
|
|
import path from "path";
|
2020-08-01 03:43:15 +02:00
|
|
|
|
2021-03-12 23:13:08 +01:00
|
|
|
import {Browser, ElementHandle, Page, launch} from "puppeteer";
|
2020-07-24 06:02:07 +02:00
|
|
|
|
2021-02-20 05:52:06 +01:00
|
|
|
import {test_credentials} from "../../var/puppeteer/test_credentials";
|
2020-05-15 16:02:16 +02:00
|
|
|
|
2020-08-04 21:48:34 +02:00
|
|
|
const root_dir = path.resolve(__dirname, "../../");
|
|
|
|
const puppeteer_dir = path.join(root_dir, "var/puppeteer");
|
|
|
|
|
2021-02-20 05:52:06 +01:00
|
|
|
type Message = Record<string, string | boolean> & {recipient?: string; content: string};
|
|
|
|
|
2020-05-15 16:02:16 +02:00
|
|
|
class CommonUtils {
|
2021-02-20 05:52:06 +01:00
|
|
|
browser: Browser | null = null;
|
2021-02-20 08:34:39 +01:00
|
|
|
screenshot_id = 0;
|
2021-03-01 06:33:30 +01:00
|
|
|
is_firefox = process.env.PUPPETEER_PRODUCT === "firefox";
|
2021-02-20 08:34:39 +01:00
|
|
|
realm_url = "http://zulip.zulipdev.com:9981/";
|
|
|
|
pm_recipient = {
|
2021-02-20 05:52:06 +01:00
|
|
|
async set(page: Page, recipient: string): Promise<void> {
|
2021-02-20 08:34:39 +01:00
|
|
|
// Without using the delay option here there seems to be
|
|
|
|
// a flake where the typeahead doesn't show up.
|
|
|
|
await page.type("#private_message_recipient", recipient, {delay: 100});
|
2020-07-28 20:51:34 +02:00
|
|
|
|
2021-03-12 23:12:51 +01:00
|
|
|
// We use [style*="display: block"] here to distinguish
|
|
|
|
// the visible typeahead menu from the invisible ones
|
|
|
|
// meant for something else; e.g., the private message
|
|
|
|
// input typeahead is different from the topic input
|
|
|
|
// typeahead but both can be present in the DOM.
|
|
|
|
const entry = await page.waitForSelector(
|
|
|
|
'.typeahead[style*="display: block"] .active a',
|
|
|
|
{visible: true},
|
|
|
|
);
|
|
|
|
await entry!.click();
|
2021-02-20 08:34:39 +01:00
|
|
|
},
|
2020-06-19 21:20:53 +02:00
|
|
|
|
2021-02-20 05:52:06 +01:00
|
|
|
async expect(page: Page, expected: string): Promise<void> {
|
2021-03-01 23:33:52 +01:00
|
|
|
const actual_recipients = await page.evaluate(() =>
|
|
|
|
zulip_test.private_message_recipient(),
|
|
|
|
);
|
2021-02-20 08:34:39 +01:00
|
|
|
assert.equal(actual_recipients, expected);
|
|
|
|
},
|
|
|
|
};
|
2020-07-30 01:34:37 +02:00
|
|
|
|
2021-02-20 05:52:06 +01:00
|
|
|
fullname: Record<string, string> = {
|
2021-02-20 08:34:39 +01:00
|
|
|
cordelia: "Cordelia Lear",
|
|
|
|
othello: "Othello, the Moor of Venice",
|
|
|
|
hamlet: "King Hamlet",
|
|
|
|
};
|
2020-07-30 01:34:37 +02:00
|
|
|
|
2021-02-20 08:34:39 +01:00
|
|
|
window_size = {
|
|
|
|
width: 1400,
|
|
|
|
height: 1024,
|
|
|
|
};
|
2020-05-15 16:02:16 +02:00
|
|
|
|
2021-02-20 05:52:06 +01:00
|
|
|
async ensure_browser(): Promise<Browser> {
|
2020-05-15 16:02:16 +02:00
|
|
|
if (this.browser === null) {
|
2020-07-30 01:34:37 +02:00
|
|
|
const {window_size} = this;
|
2021-02-20 05:52:06 +01:00
|
|
|
this.browser = await launch({
|
2020-07-30 01:34:37 +02:00
|
|
|
args: [
|
|
|
|
`--window-size=${window_size.width},${window_size.height}`,
|
|
|
|
"--no-sandbox",
|
|
|
|
"--disable-setuid-sandbox",
|
|
|
|
],
|
2021-03-01 06:29:13 +01:00
|
|
|
// TODO: Change defaultViewport to 1280x1024 when puppeteer fixes the window size issue with firefox.
|
|
|
|
// Here is link to the issue that is tracking the above problem https://github.com/puppeteer/puppeteer/issues/6442.
|
|
|
|
// @ts-expect-error: Because of https://github.com/puppeteer/puppeteer/issues/6885
|
|
|
|
defaultViewport: null,
|
2020-05-15 16:02:16 +02:00
|
|
|
headless: true,
|
|
|
|
});
|
|
|
|
}
|
2021-02-20 05:52:06 +01:00
|
|
|
return this.browser;
|
2020-05-15 16:02:16 +02:00
|
|
|
}
|
|
|
|
|
2021-02-20 05:52:06 +01:00
|
|
|
async get_page(): Promise<Page> {
|
|
|
|
const browser = await this.ensure_browser();
|
|
|
|
const page = await browser.newPage();
|
2020-05-15 16:02:16 +02:00
|
|
|
return page;
|
|
|
|
}
|
2020-05-15 17:38:25 +02:00
|
|
|
|
2021-02-20 05:52:06 +01:00
|
|
|
async screenshot(page: Page, name: string | null = null): Promise<void> {
|
2020-05-15 19:27:29 +02:00
|
|
|
if (name === null) {
|
|
|
|
name = `${this.screenshot_id}`;
|
|
|
|
this.screenshot_id += 1;
|
|
|
|
}
|
|
|
|
|
2020-08-04 21:48:34 +02:00
|
|
|
const screenshot_path = path.join(puppeteer_dir, `${name}.png`);
|
2020-05-15 19:27:29 +02:00
|
|
|
await page.screenshot({
|
|
|
|
path: screenshot_path,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-03-01 06:38:03 +01:00
|
|
|
async page_url_with_fragment(page: Page): Promise<string> {
|
|
|
|
// `page.url()` does not include the url fragment when running
|
|
|
|
// Puppeteer with Firefox: https://github.com/puppeteer/puppeteer/issues/6787.
|
|
|
|
//
|
|
|
|
// This function hacks around that issue; once it's fixed in
|
|
|
|
// puppeteer upstream, we can delete this function and return
|
|
|
|
// its callers to using `page.url()`
|
|
|
|
return await page.evaluate("location.href");
|
|
|
|
}
|
|
|
|
|
2021-03-06 21:16:35 +01:00
|
|
|
// This function will clear the existing value of the element and
|
|
|
|
// replace it with the text.
|
|
|
|
async clear_and_type(page: Page, selector: string, text: string): Promise<void> {
|
|
|
|
// Select all text currently in the element.
|
|
|
|
await page.click(selector, {clickCount: 3});
|
|
|
|
await page.type(selector, text);
|
|
|
|
}
|
|
|
|
|
2020-05-29 20:46:35 +02:00
|
|
|
/**
|
|
|
|
* This function takes a params object whose fields
|
|
|
|
* are referenced by name attribute of an input field and
|
|
|
|
* the input as a key.
|
|
|
|
*
|
|
|
|
* For example to fill:
|
|
|
|
* <form id="#demo">
|
|
|
|
* <input type="text" name="username">
|
|
|
|
* <input type="checkbox" name="terms">
|
|
|
|
* </form>
|
|
|
|
*
|
|
|
|
* You can call:
|
|
|
|
* common.fill_form(page, '#demo', {
|
|
|
|
* username: 'Iago',
|
|
|
|
* terms: true
|
|
|
|
* });
|
|
|
|
*/
|
2021-02-20 05:52:06 +01:00
|
|
|
async fill_form(
|
|
|
|
page: Page,
|
|
|
|
form_selector: string,
|
|
|
|
params: Record<string, boolean | string>,
|
|
|
|
): Promise<void> {
|
|
|
|
async function is_dropdown(page: Page, name: string): Promise<boolean> {
|
2020-07-14 19:04:51 +02:00
|
|
|
return (await page.$(`select[name="${name}"]`)) !== null;
|
|
|
|
}
|
2020-05-29 20:46:35 +02:00
|
|
|
for (const name of Object.keys(params)) {
|
|
|
|
const name_selector = `${form_selector} [name="${name}"]`;
|
|
|
|
const value = params[name];
|
|
|
|
if (typeof value === "boolean") {
|
2021-02-20 06:27:11 +01:00
|
|
|
await page.$eval(
|
|
|
|
name_selector,
|
|
|
|
(el, value) => {
|
2021-02-20 05:52:06 +01:00
|
|
|
if (el instanceof HTMLInputElement && el.checked !== value) {
|
2021-02-20 06:27:11 +01:00
|
|
|
el.click();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
value,
|
|
|
|
);
|
2020-07-14 19:04:51 +02:00
|
|
|
} else if (await is_dropdown(page, name)) {
|
2021-02-20 05:52:06 +01:00
|
|
|
if (typeof value !== "string") {
|
|
|
|
throw new TypeError(`Expected string for ${name}`);
|
|
|
|
}
|
|
|
|
await page.select(name_selector, value);
|
2020-05-29 20:46:35 +02:00
|
|
|
} else {
|
2020-07-08 15:14:19 +02:00
|
|
|
// clear any existing text in the input field before filling.
|
|
|
|
await page.$eval(name_selector, (el) => {
|
2021-02-20 05:52:06 +01:00
|
|
|
(el as HTMLInputElement).value = "";
|
2020-07-08 15:14:19 +02:00
|
|
|
});
|
2021-02-20 05:52:06 +01:00
|
|
|
await page.type(name_selector, value);
|
2020-05-29 20:46:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-20 05:52:06 +01:00
|
|
|
async check_form_contents(
|
|
|
|
page: Page,
|
|
|
|
form_selector: string,
|
|
|
|
params: Record<string, boolean | string>,
|
|
|
|
): Promise<void> {
|
2020-06-19 21:20:53 +02:00
|
|
|
for (const name of Object.keys(params)) {
|
|
|
|
const name_selector = `${form_selector} [name="${name}"]`;
|
|
|
|
const expected_value = params[name];
|
|
|
|
if (typeof expected_value === "boolean") {
|
|
|
|
assert.equal(
|
2021-02-20 05:52:06 +01:00
|
|
|
await page.$eval(name_selector, (el) => (el as HTMLInputElement).checked),
|
2020-06-19 21:20:53 +02:00
|
|
|
expected_value,
|
|
|
|
"Form content is not as expected.",
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
assert.equal(
|
2021-02-20 05:52:06 +01:00
|
|
|
await page.$eval(name_selector, (el) => (el as HTMLInputElement).value),
|
2020-06-19 21:20:53 +02:00
|
|
|
expected_value,
|
|
|
|
"Form content is not as expected.",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-12 23:13:08 +01:00
|
|
|
async get_element_text(element: ElementHandle<Element>): Promise<string> {
|
|
|
|
return (await element.getProperty("textContent"))!.jsonValue();
|
|
|
|
}
|
|
|
|
|
2021-02-20 05:52:06 +01:00
|
|
|
async get_text_from_selector(page: Page, selector: string): Promise<string> {
|
2021-03-12 23:13:08 +01:00
|
|
|
const elements = await page.$$(selector);
|
|
|
|
const texts = await Promise.all(
|
|
|
|
elements.map(async (element) => this.get_element_text(element)),
|
|
|
|
);
|
|
|
|
return texts.join("").trim();
|
2020-07-07 18:19:41 +02:00
|
|
|
}
|
|
|
|
|
2021-02-20 05:52:06 +01:00
|
|
|
async get_stream_id(page: Page, stream_name: string): Promise<number> {
|
2021-03-01 23:33:52 +01:00
|
|
|
return await page.evaluate(
|
|
|
|
(stream_name: string) => zulip_test.get_stream_id(stream_name),
|
|
|
|
stream_name,
|
|
|
|
);
|
2020-07-07 18:19:41 +02:00
|
|
|
}
|
|
|
|
|
2021-02-20 05:52:06 +01:00
|
|
|
async get_user_id_from_name(page: Page, name: string): Promise<number> {
|
2020-06-19 21:20:53 +02:00
|
|
|
if (this.fullname[name] !== undefined) {
|
|
|
|
name = this.fullname[name];
|
|
|
|
}
|
2021-03-01 23:33:52 +01:00
|
|
|
return await page.evaluate((name: string) => zulip_test.get_user_id_from_name(name), name);
|
2020-06-19 21:20:53 +02:00
|
|
|
}
|
|
|
|
|
2021-02-20 05:52:06 +01:00
|
|
|
async get_internal_email_from_name(page: Page, name: string): Promise<string> {
|
2020-06-19 21:20:53 +02:00
|
|
|
if (this.fullname[name] !== undefined) {
|
|
|
|
name = this.fullname[name];
|
|
|
|
}
|
2021-02-20 05:52:06 +01:00
|
|
|
return await page.evaluate((fullname: string) => {
|
2021-03-01 23:33:52 +01:00
|
|
|
const user_id = zulip_test.get_user_id_from_name(fullname);
|
|
|
|
return zulip_test.get_person_by_user_id(user_id).email;
|
2020-06-19 21:20:53 +02:00
|
|
|
}, name);
|
|
|
|
}
|
|
|
|
|
2021-02-20 05:52:06 +01:00
|
|
|
async log_in(
|
|
|
|
page: Page,
|
|
|
|
credentials: {username: string; password: string} | null = null,
|
|
|
|
): Promise<void> {
|
2020-05-29 15:36:32 +02:00
|
|
|
console.log("Logging in");
|
2020-07-15 01:29:15 +02:00
|
|
|
await page.goto(this.realm_url + "login/");
|
|
|
|
assert.equal(this.realm_url + "login/", page.url());
|
2020-06-06 21:50:14 +02:00
|
|
|
if (credentials === null) {
|
|
|
|
credentials = test_credentials.default_user;
|
|
|
|
}
|
2020-05-29 20:46:35 +02:00
|
|
|
// fill login form
|
|
|
|
const params = {
|
|
|
|
username: credentials.username,
|
|
|
|
password: credentials.password,
|
|
|
|
};
|
2020-07-15 01:29:15 +02:00
|
|
|
await this.fill_form(page, "#login_form", params);
|
2021-02-20 05:52:06 +01:00
|
|
|
await page.$eval("#login_form", (form) => (form as HTMLFormElement).submit());
|
2020-06-06 22:08:51 +02:00
|
|
|
|
2020-09-20 10:14:24 +02:00
|
|
|
await page.waitForSelector("#recent_topics_filter_buttons", {visible: true});
|
2020-05-29 15:36:32 +02:00
|
|
|
}
|
|
|
|
|
2021-02-20 05:52:06 +01:00
|
|
|
async log_out(page: Page): Promise<void> {
|
2020-05-29 15:36:32 +02:00
|
|
|
await page.goto(this.realm_url);
|
2020-07-15 01:29:15 +02:00
|
|
|
const menu_selector = "#settings-dropdown";
|
2020-05-29 15:36:32 +02:00
|
|
|
const logout_selector = 'a[href="#logout"]';
|
2020-08-11 01:47:44 +02:00
|
|
|
console.log("Logging out");
|
2020-05-29 15:36:32 +02:00
|
|
|
await page.waitForSelector(menu_selector, {visible: true});
|
|
|
|
await page.click(menu_selector);
|
|
|
|
await page.waitForSelector(logout_selector);
|
|
|
|
await page.click(logout_selector);
|
|
|
|
|
|
|
|
// Wait for a email input in login page so we know login
|
|
|
|
// page is loaded. Then check that we are at the login url.
|
|
|
|
await page.waitForSelector('input[name="username"]');
|
2020-07-15 01:29:15 +02:00
|
|
|
assert(page.url().includes("/login/"));
|
2020-05-29 15:36:32 +02:00
|
|
|
}
|
|
|
|
|
2021-02-20 05:52:06 +01:00
|
|
|
async ensure_enter_does_not_send(page: Page): Promise<void> {
|
2020-06-12 17:32:12 +02:00
|
|
|
await page.$eval("#enter_sends", (el) => {
|
2021-02-20 05:52:06 +01:00
|
|
|
if ((el as HTMLInputElement).checked) {
|
|
|
|
(el as HTMLInputElement).click();
|
2020-06-12 17:32:12 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-02-20 05:52:06 +01:00
|
|
|
async assert_compose_box_content(page: Page, expected_value: string): Promise<void> {
|
2021-01-20 14:19:21 +01:00
|
|
|
await page.waitForSelector("#compose-textarea");
|
|
|
|
|
|
|
|
const compose_box_element = await page.$("#compose-textarea");
|
|
|
|
const compose_box_content = await page.evaluate(
|
2021-02-20 05:52:06 +01:00
|
|
|
(element: HTMLTextAreaElement) => element.value,
|
2021-01-20 14:19:21 +01:00
|
|
|
compose_box_element,
|
|
|
|
);
|
|
|
|
assert.equal(
|
|
|
|
compose_box_content,
|
|
|
|
expected_value,
|
|
|
|
`Compose box content did not match with the expected value '{${expected_value}}'`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-02-20 05:52:06 +01:00
|
|
|
async wait_for_fully_processed_message(page: Page, content: string): Promise<void> {
|
2020-09-26 11:48:37 +02:00
|
|
|
await page.waitForFunction(
|
2021-02-20 05:52:06 +01:00
|
|
|
(content: string) => {
|
2020-07-15 00:34:28 +02:00
|
|
|
/*
|
2020-06-12 17:32:12 +02:00
|
|
|
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.
|
2020-08-30 13:48:09 +02:00
|
|
|
For the Puppeteer tests, we want to avoid all
|
2020-06-12 17:32:12 +02:00
|
|
|
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?
|
|
|
|
*/
|
2020-07-15 00:34:28 +02:00
|
|
|
const last_msg = current_msg_list.last();
|
|
|
|
if (last_msg === undefined) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-06-25 18:48:43 +02:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
if (last_msg.raw_content !== content) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-06-12 17:32:12 +02:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
if (last_msg.locally_echoed) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-06-12 17:32:12 +02:00
|
|
|
|
2021-03-01 23:33:52 +01:00
|
|
|
const row = zulip_test.last_visible_row();
|
|
|
|
if (zulip_test.row_id(row) !== last_msg.id) {
|
2020-07-15 00:34:28 +02:00
|
|
|
return false;
|
|
|
|
}
|
2020-06-12 17:32:12 +02:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
/*
|
2020-06-12 17:32:12 +02:00
|
|
|
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.
|
|
|
|
*/
|
2020-07-15 00:34:28 +02:00
|
|
|
return row.find(".star").length === 1;
|
|
|
|
},
|
|
|
|
{},
|
|
|
|
content,
|
|
|
|
);
|
2020-06-12 17:32:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for any previous send to finish, then send a message.
|
2021-02-20 05:52:06 +01:00
|
|
|
async send_message(page: Page, type: "stream" | "private", params: Message): Promise<void> {
|
2020-06-12 17:32:12 +02:00
|
|
|
// If a message is outside the view, we do not need
|
|
|
|
// to wait for it to be processed later.
|
2021-01-23 02:44:02 +01:00
|
|
|
const outside_view = params.outside_view;
|
2020-06-12 17:32:12 +02:00
|
|
|
delete params.outside_view;
|
|
|
|
|
2021-01-20 14:19:21 +01:00
|
|
|
// Compose box content should be empty before sending the message.
|
|
|
|
await this.assert_compose_box_content(page, "");
|
2020-06-12 17:32:12 +02:00
|
|
|
|
|
|
|
if (type === "stream") {
|
2020-07-15 01:29:15 +02:00
|
|
|
await page.keyboard.press("KeyC");
|
2020-06-12 17:32:12 +02:00
|
|
|
} else if (type === "private") {
|
|
|
|
await page.keyboard.press("KeyX");
|
2021-02-20 05:52:06 +01:00
|
|
|
const recipients = params.recipient!.split(", ");
|
2020-10-07 10:41:58 +02:00
|
|
|
for (const recipient of recipients) {
|
|
|
|
await this.pm_recipient.set(page, recipient);
|
2020-06-12 17:32:12 +02:00
|
|
|
}
|
|
|
|
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);
|
2021-01-20 14:19:21 +01:00
|
|
|
await this.assert_compose_box_content(page, params.content);
|
2020-06-12 17:32:12 +02:00
|
|
|
await this.ensure_enter_does_not_send(page);
|
|
|
|
await page.waitForSelector("#compose-send-button", {visible: true});
|
2020-07-15 01:29:15 +02:00
|
|
|
await page.click("#compose-send-button");
|
2020-06-12 17:32:12 +02:00
|
|
|
|
2021-01-20 14:19:21 +01:00
|
|
|
// Sending should clear compose box content.
|
2021-03-01 07:15:14 +01:00
|
|
|
await this.assert_compose_box_content(page, "");
|
2020-06-12 17:32:12 +02:00
|
|
|
|
|
|
|
if (!outside_view) {
|
|
|
|
await this.wait_for_fully_processed_message(page, params.content);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close the compose box after sending the message.
|
2021-03-01 23:33:52 +01:00
|
|
|
await page.evaluate(() => zulip_test.cancel_compose());
|
2020-06-19 21:20:53 +02:00
|
|
|
// Make sure the compose box is closed.
|
|
|
|
await page.waitForSelector("#compose-textarea", {hidden: true});
|
2020-06-12 17:32:12 +02:00
|
|
|
}
|
|
|
|
|
2021-02-20 05:52:06 +01:00
|
|
|
async send_multiple_messages(page: Page, msgs: Message[]): Promise<void> {
|
2020-10-07 10:41:58 +02:00
|
|
|
for (const msg of msgs) {
|
2020-07-15 00:34:28 +02:00
|
|
|
await this.send_message(page, msg.stream !== undefined ? "stream" : "private", msg);
|
2020-06-12 17:32:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-12 17:37:56 +02:00
|
|
|
/**
|
2020-06-16 20:21:28 +02:00
|
|
|
* This method returns a array, which is formmated as:
|
|
|
|
* [
|
|
|
|
* ['stream > topic', ['message 1', 'message 2']],
|
|
|
|
* ['You and Cordelia Lear', ['message 1', 'message 2']]
|
|
|
|
* ]
|
|
|
|
*
|
|
|
|
* The messages are sorted chronologically.
|
2020-06-12 17:37:56 +02:00
|
|
|
*/
|
2021-02-20 05:52:06 +01:00
|
|
|
async get_rendered_messages(page: Page, table = "zhome"): Promise<[string, string[]][]> {
|
2021-03-13 02:38:44 +01:00
|
|
|
const recipient_rows = await page.$$(`#${CSS.escape(table)} .recipient_row`);
|
|
|
|
return Promise.all(
|
|
|
|
recipient_rows.map(
|
|
|
|
async (element): Promise<[string, string[]]> => {
|
|
|
|
const stream_label = await element.$(".stream_label");
|
|
|
|
const stream_name = (await this.get_element_text(stream_label!)).trim();
|
|
|
|
const topic_label = await element.$(".stream_topic a");
|
|
|
|
const topic_name =
|
|
|
|
topic_label === null
|
|
|
|
? ""
|
|
|
|
: (await this.get_element_text(topic_label)).trim();
|
|
|
|
let key = stream_name;
|
|
|
|
if (topic_name !== "") {
|
|
|
|
// If topic_name is '' then this is PMs, so only
|
|
|
|
// append > topic_name if we are not in PMs or Group PMs.
|
|
|
|
key = `${stream_name} > ${topic_name}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
const messages = await Promise.all(
|
|
|
|
(
|
|
|
|
await element.$$(".message_row .message_content")
|
|
|
|
).map(async (message_row) =>
|
|
|
|
(await this.get_element_text(message_row)).trim(),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
return [key, messages];
|
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
2020-06-12 17:37:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// This method takes in page, table to fetch the messages
|
|
|
|
// from, and expected messages. The format of expected
|
|
|
|
// message is { "stream > topic": [messages] }.
|
|
|
|
// The method will only check that all the messages in the
|
|
|
|
// messages array passed exist in the order they are passed.
|
2021-02-20 05:52:06 +01:00
|
|
|
async check_messages_sent(
|
|
|
|
page: Page,
|
|
|
|
table: string,
|
|
|
|
messages: [string, string[]][],
|
|
|
|
): Promise<void> {
|
2021-02-03 23:23:32 +01:00
|
|
|
await page.waitForSelector(`#${CSS.escape(table)}`, {visible: true});
|
2020-06-12 17:37:56 +02:00
|
|
|
const rendered_messages = await this.get_rendered_messages(page, table);
|
2020-06-16 20:21:28 +02:00
|
|
|
|
|
|
|
// We only check the last n messages because if we run
|
|
|
|
// the test with --interactive there will be duplicates.
|
|
|
|
const last_n_messages = rendered_messages.slice(-messages.length);
|
|
|
|
assert.deepStrictEqual(last_n_messages, messages);
|
2020-06-12 17:37:56 +02:00
|
|
|
}
|
|
|
|
|
2021-02-20 05:52:06 +01:00
|
|
|
async manage_organization(page: Page): Promise<void> {
|
2020-08-07 22:05:18 +02:00
|
|
|
const menu_selector = "#settings-dropdown";
|
|
|
|
await page.waitForSelector(menu_selector, {visible: true});
|
|
|
|
await page.click(menu_selector);
|
|
|
|
|
|
|
|
const organization_settings = 'a[href="#organization"]';
|
|
|
|
await page.click(organization_settings);
|
|
|
|
await page.waitForSelector("#settings_overlay_container.show", {visible: true});
|
|
|
|
|
2021-03-01 06:38:03 +01:00
|
|
|
const url = await this.page_url_with_fragment(page);
|
2020-08-07 22:05:18 +02:00
|
|
|
assert(/^http:\/\/[^/]+\/#organization/.test(url), "Unexpected manage organization URL");
|
|
|
|
|
|
|
|
const organization_settings_data_section = "li[data-section='organization-settings']";
|
|
|
|
await page.click(organization_settings_data_section);
|
|
|
|
}
|
|
|
|
|
2021-02-20 05:52:06 +01:00
|
|
|
async select_item_via_typeahead(
|
|
|
|
page: Page,
|
|
|
|
field_selector: string,
|
|
|
|
str: string,
|
|
|
|
item: string,
|
|
|
|
): Promise<void> {
|
2020-08-09 14:02:38 +02:00
|
|
|
console.log(`Looking in ${field_selector} to select ${str}, ${item}`);
|
2021-03-13 03:39:22 +01:00
|
|
|
await this.clear_and_type(page, field_selector, str);
|
|
|
|
const entry = await page.waitForXPath(
|
|
|
|
`//*[@class="typeahead dropdown-menu" and contains(@style, "display: block")]//li[contains(normalize-space(), "${item}")]//a`,
|
2020-08-09 14:02:38 +02:00
|
|
|
);
|
2021-03-13 03:39:22 +01:00
|
|
|
await entry!.click();
|
2020-08-09 14:02:38 +02:00
|
|
|
}
|
|
|
|
|
2021-02-20 05:52:06 +01:00
|
|
|
async run_test(test_function: (page: Page) => Promise<void>): Promise<void> {
|
2020-05-16 19:14:31 +02:00
|
|
|
// Pass a page instance to test so we can take
|
|
|
|
// a screenshot of it when the test fails.
|
2021-02-20 05:52:06 +01:00
|
|
|
const browser = await this.ensure_browser();
|
2020-05-16 19:14:31 +02:00
|
|
|
const page = await this.get_page();
|
2020-05-15 17:38:25 +02:00
|
|
|
try {
|
2020-05-16 19:14:31 +02:00
|
|
|
await test_function(page);
|
2021-02-20 05:52:06 +01:00
|
|
|
} catch (error: unknown) {
|
2020-10-07 10:20:41 +02:00
|
|
|
console.log(error);
|
2020-05-16 19:14:31 +02:00
|
|
|
|
|
|
|
// Take a screenshot, and increment the screenshot_id.
|
|
|
|
await this.screenshot(page, `failure-${this.screenshot_id}`);
|
|
|
|
this.screenshot_id += 1;
|
|
|
|
|
2021-02-20 05:52:06 +01:00
|
|
|
await browser.close();
|
2020-05-15 17:38:25 +02:00
|
|
|
process.exit(1);
|
|
|
|
} finally {
|
2021-02-20 05:52:06 +01:00
|
|
|
await browser.close();
|
2020-05-15 17:38:25 +02:00
|
|
|
}
|
|
|
|
}
|
2020-05-15 16:02:16 +02:00
|
|
|
}
|
|
|
|
|
2021-02-20 05:52:06 +01:00
|
|
|
export default new CommonUtils();
|