2023-11-22 12:49:51 +01:00
|
|
|
/*
|
2024-09-09 15:57:16 +02:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2023-11-22 12:49:51 +01:00
|
|
|
Copyright 2023 The Matrix.org Foundation C.I.C.
|
|
|
|
|
2024-09-09 15:57:16 +02:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2023-11-22 12:49:51 +01:00
|
|
|
*/
|
|
|
|
|
2023-12-04 12:02:48 +01:00
|
|
|
import { test as base, expect as baseExpect, Locator, Page, ExpectMatcherState, ElementHandle } from "@playwright/test";
|
2024-07-15 20:23:20 +02:00
|
|
|
import { sanitizeForFilePath } from "playwright-core/lib/utils";
|
2023-11-23 10:09:32 +01:00
|
|
|
import AxeBuilder from "@axe-core/playwright";
|
2023-11-23 11:27:11 +01:00
|
|
|
import _ from "lodash";
|
2024-07-15 20:23:20 +02:00
|
|
|
import { basename, extname } from "node:path";
|
2023-11-22 12:49:51 +01:00
|
|
|
|
2023-11-23 10:09:32 +01:00
|
|
|
import type mailhog from "mailhog";
|
|
|
|
import type { IConfigOptions } from "../src/IConfigOptions";
|
2023-11-27 15:40:45 +01:00
|
|
|
import { Credentials, Homeserver, HomeserverInstance, StartHomeserverOpts } from "./plugins/homeserver";
|
|
|
|
import { Synapse } from "./plugins/homeserver/synapse";
|
|
|
|
import { Dendrite, Pinecone } from "./plugins/homeserver/dendrite";
|
2024-02-19 18:26:32 +01:00
|
|
|
import { Instance, MailHogServer } from "./plugins/mailhog";
|
2023-11-27 13:11:00 +01:00
|
|
|
import { ElementAppPage } from "./pages/ElementAppPage";
|
2023-11-23 11:27:11 +01:00
|
|
|
import { OAuthServer } from "./plugins/oauth_server";
|
2023-11-28 13:08:05 +01:00
|
|
|
import { Crypto } from "./pages/crypto";
|
2023-11-27 11:00:16 +01:00
|
|
|
import { Toasts } from "./pages/toasts";
|
2023-11-29 17:39:27 +01:00
|
|
|
import { Bot, CreateBotOpts } from "./pages/bot";
|
2023-12-16 11:01:26 +01:00
|
|
|
import { ProxyInstance, SlidingSyncProxy } from "./plugins/sliding-sync-proxy";
|
2023-12-04 13:01:06 +01:00
|
|
|
import { Webserver } from "./plugins/webserver";
|
2023-11-22 12:49:51 +01:00
|
|
|
|
2024-05-03 00:19:55 +02:00
|
|
|
// Enable experimental service worker support
|
|
|
|
// See https://playwright.dev/docs/service-workers-experimental#how-to-enable
|
|
|
|
process.env["PW_EXPERIMENTAL_SERVICE_WORKER_NETWORK_EVENTS"] = "1";
|
|
|
|
|
2023-11-23 10:09:32 +01:00
|
|
|
const CONFIG_JSON: Partial<IConfigOptions> = {
|
2023-11-22 12:49:51 +01:00
|
|
|
// This is deliberately quite a minimal config.json, so that we can test that the default settings
|
|
|
|
// actually work.
|
|
|
|
//
|
|
|
|
// The only thing that we really *need* (otherwise Element refuses to load) is a default homeserver.
|
|
|
|
// We point that to a guaranteed-invalid domain.
|
|
|
|
default_server_config: {
|
|
|
|
"m.homeserver": {
|
|
|
|
base_url: "https://server.invalid",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2023-12-09 19:38:01 +01:00
|
|
|
// The default language is set here for test consistency
|
|
|
|
setting_defaults: {
|
|
|
|
language: "en-GB",
|
|
|
|
},
|
|
|
|
|
2023-11-22 12:49:51 +01:00
|
|
|
// the location tests want a map style url.
|
|
|
|
map_style_url: "https://api.maptiler.com/maps/streets/style.json?key=fU3vlMsMn4Jb6dnEIFsx",
|
2024-04-08 10:43:59 +02:00
|
|
|
|
|
|
|
features: {
|
|
|
|
// We don't want to go through the feature announcement during the e2e test
|
|
|
|
feature_release_announcement: false,
|
|
|
|
},
|
2023-11-22 12:49:51 +01:00
|
|
|
};
|
|
|
|
|
2023-11-29 23:46:51 +01:00
|
|
|
interface CredentialsWithDisplayName extends Credentials {
|
|
|
|
displayName: string;
|
|
|
|
}
|
|
|
|
|
2024-06-24 12:30:59 +02:00
|
|
|
export const test = base.extend<{
|
|
|
|
axe: AxeBuilder;
|
|
|
|
checkA11y: () => Promise<void>;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The contents of the config.json to send when the client requests it.
|
|
|
|
*/
|
|
|
|
config: typeof CONFIG_JSON;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The options with which to run the {@link #homeserver} fixture.
|
|
|
|
*/
|
|
|
|
startHomeserverOpts: StartHomeserverOpts | string;
|
|
|
|
|
|
|
|
homeserver: HomeserverInstance;
|
|
|
|
oAuthServer: { port: number };
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The displayname to use for the user registered in {@link #credentials}.
|
|
|
|
*
|
|
|
|
* To set it, call `test.use({ displayName: "myDisplayName" })` in the test file or `describe` block.
|
|
|
|
* See {@link https://playwright.dev/docs/api/class-test#test-use}.
|
|
|
|
*/
|
|
|
|
displayName?: string;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A test fixture which registers a test user on the {@link #homeserver} and supplies the details
|
|
|
|
* of the registered user.
|
|
|
|
*/
|
|
|
|
credentials: CredentialsWithDisplayName;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The same as {@link https://playwright.dev/docs/api/class-fixtures#fixtures-page|`page`},
|
|
|
|
* but adds an initScript which will populate localStorage with the user's details from
|
|
|
|
* {@link #credentials} and {@link #homeserver}.
|
|
|
|
*
|
|
|
|
* Similar to {@link #user}, but doesn't load the app.
|
|
|
|
*/
|
|
|
|
pageWithCredentials: Page;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A (rather poorly-named) test fixture which registers a user per {@link #credentials}, stores
|
|
|
|
* the credentials into localStorage per {@link #homeserver}, and then loads the front page of the
|
|
|
|
* app.
|
|
|
|
*/
|
|
|
|
user: CredentialsWithDisplayName;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The same as {@link https://playwright.dev/docs/api/class-fixtures#fixtures-page|`page`},
|
|
|
|
* but wraps the returned `Page` in a class of utilities for interacting with the Element-Web UI,
|
|
|
|
* {@link ElementAppPage}.
|
|
|
|
*/
|
|
|
|
app: ElementAppPage;
|
|
|
|
|
|
|
|
mailhog: { api: mailhog.API; instance: Instance };
|
|
|
|
crypto: Crypto;
|
|
|
|
room?: { roomId: string };
|
|
|
|
toasts: Toasts;
|
|
|
|
uut?: Locator; // Unit Under Test, useful place to refer a prepared locator
|
|
|
|
botCreateOpts: CreateBotOpts;
|
|
|
|
bot: Bot;
|
|
|
|
slidingSyncProxy: ProxyInstance;
|
|
|
|
labsFlags: string[];
|
|
|
|
webserver: Webserver;
|
|
|
|
}>({
|
2023-11-22 14:20:58 +01:00
|
|
|
config: CONFIG_JSON,
|
2024-06-24 12:30:59 +02:00
|
|
|
page: async ({ context, page, config, labsFlags }, use) => {
|
2023-11-22 12:49:51 +01:00
|
|
|
await context.route(`http://localhost:8080/config.json*`, async (route) => {
|
2023-11-23 11:27:11 +01:00
|
|
|
const json = { ...CONFIG_JSON, ...config };
|
2023-12-12 15:08:36 +01:00
|
|
|
json["features"] = {
|
|
|
|
...json["features"],
|
|
|
|
// Enable the lab features
|
|
|
|
...labsFlags.reduce((obj, flag) => {
|
|
|
|
obj[flag] = true;
|
|
|
|
return obj;
|
|
|
|
}, {}),
|
|
|
|
};
|
2023-11-22 14:20:58 +01:00
|
|
|
await route.fulfill({ json });
|
2023-11-22 12:49:51 +01:00
|
|
|
});
|
|
|
|
await use(page);
|
|
|
|
},
|
|
|
|
|
|
|
|
startHomeserverOpts: "default",
|
2023-12-14 14:49:35 +01:00
|
|
|
homeserver: async ({ request, startHomeserverOpts: opts }, use, testInfo) => {
|
2023-11-22 12:49:51 +01:00
|
|
|
if (typeof opts === "string") {
|
|
|
|
opts = { template: opts };
|
|
|
|
}
|
|
|
|
|
2023-11-27 15:40:45 +01:00
|
|
|
let server: Homeserver;
|
|
|
|
const homeserverName = process.env["PLAYWRIGHT_HOMESERVER"];
|
|
|
|
switch (homeserverName) {
|
|
|
|
case "dendrite":
|
|
|
|
server = new Dendrite(request);
|
|
|
|
break;
|
|
|
|
case "pinecone":
|
|
|
|
server = new Pinecone(request);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
server = new Synapse(request);
|
|
|
|
}
|
|
|
|
|
2023-11-22 12:49:51 +01:00
|
|
|
await use(await server.start(opts));
|
2023-12-14 14:49:35 +01:00
|
|
|
const logs = await server.stop();
|
|
|
|
|
|
|
|
if (testInfo.status !== "passed") {
|
|
|
|
for (const path of logs) {
|
|
|
|
await testInfo.attach(`homeserver-${basename(path)}`, {
|
|
|
|
path,
|
|
|
|
contentType: "text/plain",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2023-11-22 12:49:51 +01:00
|
|
|
},
|
2023-11-23 11:27:11 +01:00
|
|
|
// eslint-disable-next-line no-empty-pattern
|
|
|
|
oAuthServer: async ({}, use) => {
|
|
|
|
const server = new OAuthServer();
|
|
|
|
const port = server.start();
|
|
|
|
await use({ port });
|
|
|
|
server.stop();
|
|
|
|
},
|
|
|
|
|
|
|
|
displayName: undefined,
|
2023-11-29 23:46:51 +01:00
|
|
|
credentials: async ({ homeserver, displayName: testDisplayName }, use) => {
|
2023-11-23 11:27:11 +01:00
|
|
|
const names = ["Alice", "Bob", "Charlie", "Daniel", "Eve", "Frank", "Grace", "Hannah", "Isaac", "Judy"];
|
|
|
|
const password = _.uniqueId("password_");
|
|
|
|
const displayName = testDisplayName ?? _.sample(names)!;
|
|
|
|
|
2023-12-07 11:32:23 +01:00
|
|
|
const credentials = await homeserver.registerUser("user", password, displayName);
|
|
|
|
console.log(`Registered test user @user:localhost with displayname ${displayName}`);
|
2023-11-23 11:27:11 +01:00
|
|
|
|
2023-11-29 23:46:51 +01:00
|
|
|
await use({
|
|
|
|
...credentials,
|
|
|
|
displayName,
|
|
|
|
});
|
|
|
|
},
|
2023-12-12 15:08:36 +01:00
|
|
|
labsFlags: [],
|
2024-01-17 08:14:49 +01:00
|
|
|
|
|
|
|
pageWithCredentials: async ({ page, homeserver, credentials }, use) => {
|
2023-11-23 11:27:11 +01:00
|
|
|
await page.addInitScript(
|
|
|
|
({ baseUrl, credentials }) => {
|
|
|
|
// Seed the localStorage with the required credentials
|
|
|
|
window.localStorage.setItem("mx_hs_url", baseUrl);
|
|
|
|
window.localStorage.setItem("mx_user_id", credentials.userId);
|
|
|
|
window.localStorage.setItem("mx_access_token", credentials.accessToken);
|
|
|
|
window.localStorage.setItem("mx_device_id", credentials.deviceId);
|
|
|
|
window.localStorage.setItem("mx_is_guest", "false");
|
|
|
|
window.localStorage.setItem("mx_has_pickle_key", "false");
|
|
|
|
window.localStorage.setItem("mx_has_access_token", "true");
|
|
|
|
|
|
|
|
// Ensure the language is set to a consistent value
|
|
|
|
window.localStorage.setItem("mx_local_settings", '{"language":"en"}');
|
|
|
|
},
|
|
|
|
{ baseUrl: homeserver.config.baseUrl, credentials },
|
|
|
|
);
|
2024-01-17 08:14:49 +01:00
|
|
|
await use(page);
|
|
|
|
},
|
|
|
|
|
|
|
|
user: async ({ pageWithCredentials: page, credentials }, use) => {
|
2023-11-23 11:27:11 +01:00
|
|
|
await page.goto("/");
|
|
|
|
await page.waitForSelector(".mx_MatrixChat", { timeout: 30000 });
|
2023-11-28 12:02:50 +01:00
|
|
|
await use(credentials);
|
2023-11-23 11:27:11 +01:00
|
|
|
},
|
2023-11-23 10:09:32 +01:00
|
|
|
|
|
|
|
axe: async ({ page }, use) => {
|
|
|
|
await use(new AxeBuilder({ page }));
|
|
|
|
},
|
|
|
|
checkA11y: async ({ axe }, use, testInfo) =>
|
|
|
|
use(async () => {
|
|
|
|
const results = await axe.analyze();
|
|
|
|
|
|
|
|
await testInfo.attach("accessibility-scan-results", {
|
|
|
|
body: JSON.stringify(results, null, 2),
|
|
|
|
contentType: "application/json",
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(results.violations).toEqual([]);
|
|
|
|
}),
|
2023-11-27 11:00:16 +01:00
|
|
|
|
2023-11-27 13:11:00 +01:00
|
|
|
app: async ({ page }, use) => {
|
2023-11-30 11:18:18 +01:00
|
|
|
const app = new ElementAppPage(page);
|
|
|
|
await use(app);
|
2023-11-27 13:11:00 +01:00
|
|
|
},
|
2023-11-28 13:08:05 +01:00
|
|
|
crypto: async ({ page, homeserver, request }, use) => {
|
|
|
|
await use(new Crypto(page, homeserver, request));
|
|
|
|
},
|
2023-11-27 11:00:16 +01:00
|
|
|
toasts: async ({ page }, use) => {
|
|
|
|
await use(new Toasts(page));
|
|
|
|
},
|
2023-11-29 17:39:27 +01:00
|
|
|
|
|
|
|
botCreateOpts: {},
|
2023-12-12 09:55:29 +01:00
|
|
|
bot: async ({ page, homeserver, botCreateOpts, user }, use) => {
|
2023-11-29 17:39:27 +01:00
|
|
|
const bot = new Bot(page, homeserver, botCreateOpts);
|
2023-11-30 11:18:18 +01:00
|
|
|
await bot.prepareClient(); // eagerly register the bot
|
2023-11-29 17:39:27 +01:00
|
|
|
await use(bot);
|
|
|
|
},
|
2023-12-04 13:01:06 +01:00
|
|
|
|
2024-02-19 18:26:32 +01:00
|
|
|
// eslint-disable-next-line no-empty-pattern
|
|
|
|
mailhog: async ({}, use) => {
|
|
|
|
const mailhog = new MailHogServer();
|
|
|
|
const instance = await mailhog.start();
|
|
|
|
await use(instance);
|
|
|
|
await mailhog.stop();
|
|
|
|
},
|
|
|
|
|
2023-12-16 11:01:26 +01:00
|
|
|
slidingSyncProxy: async ({ page, user, homeserver }, use) => {
|
|
|
|
const proxy = new SlidingSyncProxy(homeserver.config.dockerUrl);
|
|
|
|
const proxyInstance = await proxy.start();
|
|
|
|
const proxyAddress = `http://localhost:${proxyInstance.port}`;
|
|
|
|
await page.addInitScript((proxyAddress) => {
|
|
|
|
window.localStorage.setItem(
|
|
|
|
"mx_local_settings",
|
|
|
|
JSON.stringify({
|
|
|
|
feature_sliding_sync_proxy_url: proxyAddress,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
window.localStorage.setItem("mx_labs_feature_feature_sliding_sync", "true");
|
|
|
|
}, proxyAddress);
|
|
|
|
await page.goto("/");
|
|
|
|
await page.waitForSelector(".mx_MatrixChat", { timeout: 30000 });
|
|
|
|
await use(proxyInstance);
|
|
|
|
await proxy.stop();
|
|
|
|
},
|
|
|
|
|
2023-12-04 13:01:06 +01:00
|
|
|
// eslint-disable-next-line no-empty-pattern
|
|
|
|
webserver: async ({}, use) => {
|
|
|
|
const webserver = new Webserver();
|
|
|
|
await use(webserver);
|
|
|
|
webserver.stop();
|
|
|
|
},
|
2023-11-22 12:49:51 +01:00
|
|
|
});
|
|
|
|
|
2024-07-15 20:23:20 +02:00
|
|
|
// Based on https://github.com/microsoft/playwright/blob/2b77ed4d7aafa85a600caa0b0d101b72c8437eeb/packages/playwright/src/util.ts#L206C8-L210C2
|
|
|
|
function sanitizeFilePathBeforeExtension(filePath: string): string {
|
|
|
|
const ext = extname(filePath);
|
|
|
|
const base = filePath.substring(0, filePath.length - ext.length);
|
|
|
|
return sanitizeForFilePath(base) + ext;
|
|
|
|
}
|
|
|
|
|
2023-12-04 12:02:48 +01:00
|
|
|
export const expect = baseExpect.extend({
|
2023-12-11 15:36:30 +01:00
|
|
|
async toMatchScreenshot(
|
|
|
|
this: ExpectMatcherState,
|
|
|
|
receiver: Page | Locator,
|
2024-07-15 20:23:20 +02:00
|
|
|
name: `${string}.png`,
|
2023-12-11 15:36:30 +01:00
|
|
|
options?: {
|
|
|
|
mask?: Array<Locator>;
|
2024-07-30 10:20:07 +02:00
|
|
|
includeDialogBackground?: boolean;
|
|
|
|
showTooltips?: boolean;
|
2023-12-11 15:36:30 +01:00
|
|
|
timeout?: number;
|
|
|
|
css?: string;
|
|
|
|
},
|
|
|
|
) {
|
2024-07-15 20:23:20 +02:00
|
|
|
const testInfo = test.info();
|
|
|
|
if (!testInfo) throw new Error(`toMatchScreenshot() must be called during the test`);
|
|
|
|
|
2023-12-04 12:02:48 +01:00
|
|
|
const page = "page" in receiver ? receiver.page() : receiver;
|
|
|
|
|
2024-07-30 10:20:07 +02:00
|
|
|
let css = `
|
|
|
|
.mx_MessagePanel_myReadMarker {
|
|
|
|
display: none !important;
|
|
|
|
}
|
|
|
|
.mx_RoomView_MessageList {
|
|
|
|
height: auto !important;
|
|
|
|
}
|
|
|
|
.mx_DisambiguatedProfile_displayName {
|
|
|
|
color: var(--cpd-color-blue-1200) !important;
|
|
|
|
}
|
|
|
|
.mx_BaseAvatar {
|
|
|
|
background-color: var(--cpd-color-fuchsia-1200) !important;
|
|
|
|
color: white !important;
|
|
|
|
}
|
|
|
|
.mx_ReplyChain {
|
|
|
|
border-left-color: var(--cpd-color-blue-1200) !important;
|
|
|
|
}
|
|
|
|
/* Avoid flakiness from hover styling */
|
|
|
|
.mx_ReplyChain_show {
|
|
|
|
color: var(--cpd-color-text-secondary) !important;
|
|
|
|
}
|
|
|
|
/* Use monospace font for timestamp for consistent mask width */
|
|
|
|
.mx_MessageTimestamp {
|
|
|
|
font-family: Inconsolata !important;
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
|
|
|
if (!options?.showTooltips) {
|
|
|
|
css += `
|
2024-09-26 12:14:53 +02:00
|
|
|
[role="tooltip"],
|
2024-07-12 13:23:53 +02:00
|
|
|
.mx_Tooltip_visible {
|
|
|
|
visibility: hidden !important;
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
|
2024-07-30 10:20:07 +02:00
|
|
|
if (!options?.includeDialogBackground) {
|
|
|
|
css += `
|
|
|
|
/* Make the dialog backdrop solid so any dialog screenshots don't show any components behind them */
|
|
|
|
.mx_Dialog_background {
|
|
|
|
background-color: #030c1b !important;
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options?.css) {
|
|
|
|
css += options.css;
|
|
|
|
}
|
|
|
|
|
2023-12-04 12:02:48 +01:00
|
|
|
// We add a custom style tag before taking screenshots
|
|
|
|
const style = (await page.addStyleTag({
|
2024-07-30 10:20:07 +02:00
|
|
|
content: css,
|
2023-12-04 12:02:48 +01:00
|
|
|
})) as ElementHandle<Element>;
|
|
|
|
|
2024-07-15 20:23:20 +02:00
|
|
|
const screenshotName = sanitizeFilePathBeforeExtension(name);
|
|
|
|
await baseExpect(receiver).toHaveScreenshot(screenshotName, options);
|
2023-12-04 12:02:48 +01:00
|
|
|
|
|
|
|
await style.evaluate((tag) => tag.remove());
|
2024-07-15 20:23:20 +02:00
|
|
|
|
|
|
|
testInfo.annotations.push({
|
|
|
|
// `_` prefix hides it from the HTML reporter
|
|
|
|
type: "_screenshot",
|
|
|
|
// include a path relative to `playwright/snapshots/`
|
|
|
|
description: testInfo.snapshotPath(screenshotName).split("/playwright/snapshots/", 2)[1],
|
|
|
|
});
|
|
|
|
|
2023-12-04 12:02:48 +01:00
|
|
|
return { pass: true, message: () => "", name: "toMatchScreenshot" };
|
|
|
|
},
|
|
|
|
});
|