Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 4 additions & 26 deletions packages/element-web-playwright-common/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ Please see LICENSE files in the repository root for full details.
import { type Config as BaseConfig } from "@element-hq/element-web-module-api";

import { test as base } from "./fixtures/index.js";
import { routeConfigJson } from "./utils/config_json.js";

export * from "./utils/config_json.js";

// Enable experimental service worker support
// See https://playwright.dev/docs/service-workers-experimental#how-to-enable
Expand Down Expand Up @@ -68,32 +71,7 @@ export const test = base.extend<TestFixtures>({
labsFlags: async ({}, use) => use([]),
disablePresence: async ({}, use) => use(false),
page: async ({ homeserver, context, page, config, labsFlags, disablePresence }, use) => {
await context.route(`http://localhost:8080/config.json*`, async (route) => {
const json = {
...CONFIG_JSON,
...config,
default_server_config: {
"m.homeserver": {
base_url: homeserver.baseUrl,
},
...config.default_server_config,
},
};
json["features"] = {
...json["features"],
// Enable the lab features
...labsFlags.reduce<NonNullable<(typeof CONFIG_JSON)["features"]>>((obj, flag) => {
obj[flag] = true;
return obj;
}, {}),
};
if (disablePresence) {
json["enable_presence_by_hs_url"] = {
[homeserver.baseUrl]: false,
};
}
await route.fulfill({ json });
});
await routeConfigJson(context, homeserver.baseUrl, config, labsFlags, disablePresence);
await use(page);
},
});
Expand Down
71 changes: 71 additions & 0 deletions packages/element-web-playwright-common/src/utils/config_json.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
Copyright 2025 New Vector Ltd.

SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE files in the repository root for full details.
*/

import { BrowserContext, Page } from "@playwright/test";

import { Config, CONFIG_JSON } from "../index.js";

/** Construct a suitable config.json for the given homeserver
*
* @param homeserverBaseUrl - The `baseUrl` of the homeserver that the client should be configured to connect to.
* @param additionalConfig - Additional config to add to the default config.json.
* @param labsFlags - Lab flags to enable in the client.
* @param disablePresence - Whether to disable presence for the given homeserver.
*/
export function buildConfigJson(
homeserverBaseUrl: string,
additionalConfig: Partial<Config> = {},
labsFlags: string[] = [],
disablePresence: boolean = false,
): Partial<Config> {
const json = {
...CONFIG_JSON,
...additionalConfig,
default_server_config: {
"m.homeserver": {
base_url: homeserverBaseUrl,
},
...additionalConfig.default_server_config,
},
};
json["features"] = {
...json["features"],
// Enable the lab features
...labsFlags.reduce<NonNullable<(typeof CONFIG_JSON)["features"]>>((obj, flag) => {
obj[flag] = true;
return obj;
}, {}),
};
if (disablePresence) {
json["enable_presence_by_hs_url"] = {
[homeserverBaseUrl]: false,
};
}
return json;
}

/**
* Add a route to the browser context/page which will serve a suitable config.json for the given homeserver.
*
* @param context - The browser context or page to route the config.json to.
* @param homeserverBaseUrl - The `baseUrl` of the homeserver that the client should be configured to connect to.
* @param additionalConfig - Additional config to add to the default config.json.
* @param labsFlags - Lab flags to enable in the client.
* @param disablePresence - Whether to disable presence for the given homeserver.
*/
export async function routeConfigJson(
context: BrowserContext | Page,
homeserverBaseUrl: string,
additionalConfig: Partial<Config> = {},
labsFlags: string[] = [],
disablePresence: boolean = false,
): Promise<void> {
await context.route(`http://localhost:8080/config.json*`, async (route) => {
const json = buildConfigJson(homeserverBaseUrl, additionalConfig, labsFlags, disablePresence);
await route.fulfill({ json });
});
}