2023-11-22 19:49:51 +08:00
|
|
|
/*
|
|
|
|
Copyright 2023 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2023-12-04 19:02:48 +08:00
|
|
|
import { test as base, expect as baseExpect, Locator, Page, ExpectMatcherState, ElementHandle } from "@playwright/test";
|
2023-11-23 17:09:32 +08:00
|
|
|
import AxeBuilder from "@axe-core/playwright";
|
2023-11-23 18:27:11 +08:00
|
|
|
import _ from "lodash";
|
2023-11-22 19:49:51 +08:00
|
|
|
|
2023-11-23 17:09:32 +08:00
|
|
|
import type mailhog from "mailhog";
|
|
|
|
import type { IConfigOptions } from "../src/IConfigOptions";
|
2023-11-27 22:40:45 +08:00
|
|
|
import { Credentials, Homeserver, HomeserverInstance, StartHomeserverOpts } from "./plugins/homeserver";
|
|
|
|
import { Synapse } from "./plugins/homeserver/synapse";
|
|
|
|
import { Dendrite, Pinecone } from "./plugins/homeserver/dendrite";
|
2023-11-23 17:09:32 +08:00
|
|
|
import { Instance } from "./plugins/mailhog";
|
2023-11-27 20:11:00 +08:00
|
|
|
import { ElementAppPage } from "./pages/ElementAppPage";
|
2023-11-23 18:27:11 +08:00
|
|
|
import { OAuthServer } from "./plugins/oauth_server";
|
2023-11-28 20:08:05 +08:00
|
|
|
import { Crypto } from "./pages/crypto";
|
2023-11-27 18:00:16 +08:00
|
|
|
import { Toasts } from "./pages/toasts";
|
2023-11-30 00:39:27 +08:00
|
|
|
import { Bot, CreateBotOpts } from "./pages/bot";
|
2023-12-04 20:01:06 +08:00
|
|
|
import { Webserver } from "./plugins/webserver";
|
2023-11-22 19:49:51 +08:00
|
|
|
|
2023-11-23 17:09:32 +08:00
|
|
|
const CONFIG_JSON: Partial<IConfigOptions> = {
|
2023-11-22 19:49:51 +08: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-10 02:38:01 +08:00
|
|
|
// The default language is set here for test consistency
|
|
|
|
setting_defaults: {
|
|
|
|
language: "en-GB",
|
|
|
|
},
|
|
|
|
|
2023-11-22 19:49:51 +08:00
|
|
|
// the location tests want a map style url.
|
|
|
|
map_style_url: "https://api.maptiler.com/maps/streets/style.json?key=fU3vlMsMn4Jb6dnEIFsx",
|
|
|
|
};
|
|
|
|
|
2023-11-22 21:20:58 +08:00
|
|
|
export type TestOptions = {
|
2023-11-28 20:08:05 +08:00
|
|
|
cryptoBackend: "legacy" | "rust";
|
2023-11-22 21:20:58 +08:00
|
|
|
};
|
|
|
|
|
2023-11-30 06:46:51 +08:00
|
|
|
interface CredentialsWithDisplayName extends Credentials {
|
|
|
|
displayName: string;
|
|
|
|
}
|
|
|
|
|
2023-11-22 21:20:58 +08:00
|
|
|
export const test = base.extend<
|
|
|
|
TestOptions & {
|
2023-11-23 17:09:32 +08:00
|
|
|
axe: AxeBuilder;
|
|
|
|
checkA11y: () => Promise<void>;
|
2023-11-23 18:27:11 +08:00
|
|
|
// The contents of the config.json to send
|
2023-11-22 21:20:58 +08:00
|
|
|
config: typeof CONFIG_JSON;
|
2023-11-23 18:27:11 +08:00
|
|
|
// The options with which to run the `homeserver` fixture
|
2023-11-22 21:20:58 +08:00
|
|
|
startHomeserverOpts: StartHomeserverOpts | string;
|
|
|
|
homeserver: HomeserverInstance;
|
2023-11-23 18:27:11 +08:00
|
|
|
oAuthServer: { port: number };
|
2023-11-30 06:46:51 +08:00
|
|
|
credentials: CredentialsWithDisplayName;
|
|
|
|
user: CredentialsWithDisplayName;
|
2023-11-23 18:27:11 +08:00
|
|
|
displayName?: string;
|
2023-11-27 20:11:00 +08:00
|
|
|
app: ElementAppPage;
|
2023-11-23 17:09:32 +08:00
|
|
|
mailhog?: { api: mailhog.API; instance: Instance };
|
2023-11-28 20:08:05 +08:00
|
|
|
crypto: Crypto;
|
2023-11-28 21:52:09 +08:00
|
|
|
room?: { roomId: string };
|
2023-11-27 18:00:16 +08:00
|
|
|
toasts: Toasts;
|
2023-11-29 21:50:13 +08:00
|
|
|
uut?: Locator; // Unit Under Test, useful place to refer a prepared locator
|
2023-11-30 00:39:27 +08:00
|
|
|
botCreateOpts: CreateBotOpts;
|
|
|
|
bot: Bot;
|
2023-12-04 20:01:06 +08:00
|
|
|
webserver: Webserver;
|
2023-11-22 21:20:58 +08:00
|
|
|
}
|
|
|
|
>({
|
2023-11-28 20:08:05 +08:00
|
|
|
cryptoBackend: ["legacy", { option: true }],
|
2023-11-22 21:20:58 +08:00
|
|
|
config: CONFIG_JSON,
|
2023-11-28 20:08:05 +08:00
|
|
|
page: async ({ context, page, config, cryptoBackend }, use) => {
|
2023-11-22 19:49:51 +08:00
|
|
|
await context.route(`http://localhost:8080/config.json*`, async (route) => {
|
2023-11-23 18:27:11 +08:00
|
|
|
const json = { ...CONFIG_JSON, ...config };
|
2023-11-28 20:08:05 +08:00
|
|
|
if (cryptoBackend === "rust") {
|
2023-11-22 21:20:58 +08:00
|
|
|
json["features"] = {
|
|
|
|
...json["features"],
|
|
|
|
feature_rust_crypto: true,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
await route.fulfill({ json });
|
2023-11-22 19:49:51 +08:00
|
|
|
});
|
2023-11-23 18:27:11 +08:00
|
|
|
|
2023-11-22 19:49:51 +08:00
|
|
|
await use(page);
|
|
|
|
},
|
|
|
|
|
|
|
|
startHomeserverOpts: "default",
|
|
|
|
homeserver: async ({ request, startHomeserverOpts: opts }, use) => {
|
|
|
|
if (typeof opts === "string") {
|
|
|
|
opts = { template: opts };
|
|
|
|
}
|
|
|
|
|
2023-11-27 22:40:45 +08: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 19:49:51 +08:00
|
|
|
await use(await server.start(opts));
|
|
|
|
await server.stop();
|
|
|
|
},
|
2023-11-23 18:27:11 +08: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-30 06:46:51 +08:00
|
|
|
credentials: async ({ homeserver, displayName: testDisplayName }, use) => {
|
2023-11-23 18:27:11 +08: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 18:32:23 +08:00
|
|
|
const credentials = await homeserver.registerUser("user", password, displayName);
|
|
|
|
console.log(`Registered test user @user:localhost with displayname ${displayName}`);
|
2023-11-23 18:27:11 +08:00
|
|
|
|
2023-11-30 06:46:51 +08:00
|
|
|
await use({
|
|
|
|
...credentials,
|
|
|
|
displayName,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
user: async ({ page, homeserver, credentials }, use) => {
|
2023-11-23 18:27:11 +08: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 },
|
|
|
|
);
|
|
|
|
await page.goto("/");
|
|
|
|
|
|
|
|
await page.waitForSelector(".mx_MatrixChat", { timeout: 30000 });
|
|
|
|
|
2023-11-28 19:02:50 +08:00
|
|
|
await use(credentials);
|
2023-11-23 18:27:11 +08:00
|
|
|
},
|
2023-11-23 17:09:32 +08: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 18:00:16 +08:00
|
|
|
|
2023-11-27 20:11:00 +08:00
|
|
|
app: async ({ page }, use) => {
|
2023-11-30 18:18:18 +08:00
|
|
|
const app = new ElementAppPage(page);
|
|
|
|
await use(app);
|
2023-11-27 20:11:00 +08:00
|
|
|
},
|
2023-11-28 20:08:05 +08:00
|
|
|
crypto: async ({ page, homeserver, request }, use) => {
|
|
|
|
await use(new Crypto(page, homeserver, request));
|
|
|
|
},
|
2023-11-27 18:00:16 +08:00
|
|
|
toasts: async ({ page }, use) => {
|
|
|
|
await use(new Toasts(page));
|
|
|
|
},
|
2023-11-30 00:39:27 +08:00
|
|
|
|
|
|
|
botCreateOpts: {},
|
|
|
|
bot: async ({ page, homeserver, botCreateOpts }, use) => {
|
|
|
|
const bot = new Bot(page, homeserver, botCreateOpts);
|
2023-11-30 18:18:18 +08:00
|
|
|
await bot.prepareClient(); // eagerly register the bot
|
2023-11-30 00:39:27 +08:00
|
|
|
await use(bot);
|
|
|
|
},
|
2023-12-04 20:01:06 +08:00
|
|
|
|
|
|
|
// eslint-disable-next-line no-empty-pattern
|
|
|
|
webserver: async ({}, use) => {
|
|
|
|
const webserver = new Webserver();
|
|
|
|
await use(webserver);
|
|
|
|
webserver.stop();
|
|
|
|
},
|
2023-11-22 19:49:51 +08:00
|
|
|
});
|
|
|
|
|
2023-12-04 19:02:48 +08:00
|
|
|
export const expect = baseExpect.extend({
|
|
|
|
async toMatchScreenshot(this: ExpectMatcherState, receiver: Page | Locator, ...args) {
|
|
|
|
const page = "page" in receiver ? receiver.page() : receiver;
|
|
|
|
|
|
|
|
// We add a custom style tag before taking screenshots
|
|
|
|
const style = (await page.addStyleTag({
|
|
|
|
content: `
|
|
|
|
.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;
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
})) as ElementHandle<Element>;
|
|
|
|
|
|
|
|
await baseExpect(receiver).toHaveScreenshot(...args);
|
|
|
|
|
|
|
|
await style.evaluate((tag) => tag.remove());
|
|
|
|
return { pass: true, message: () => "", name: "toMatchScreenshot" };
|
|
|
|
},
|
|
|
|
});
|
2023-12-07 01:54:47 +08:00
|
|
|
|
|
|
|
test.use({
|
|
|
|
permissions: ["clipboard-read"],
|
|
|
|
});
|