Skip to content

Commit 35b6052

Browse files
authored
Merge pull request #80 from element-hq/rav/credentials_funcs
Playwright: add `populateLocalStorageWithCredentials` helper
2 parents 5b30a05 + 2214dc0 commit 35b6052

File tree

5 files changed

+53
-31
lines changed

5 files changed

+53
-31
lines changed

packages/element-web-playwright-common/src/fixtures/user.ts

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,32 @@ import { sample, uniqueId } from "lodash-es";
1212
import { test as base } from "./services.js";
1313
import { Credentials } from "../utils/api.js";
1414

15+
/** Adds an initScript to the given page which will populate localStorage appropriately so that Element will use the given credentials. */
16+
export async function populateLocalStorageWithCredentials(page: Page, credentials: Credentials) {
17+
await page.addInitScript(
18+
({ credentials }) => {
19+
window.localStorage.setItem("mx_hs_url", credentials.homeserverBaseUrl);
20+
window.localStorage.setItem("mx_user_id", credentials.userId);
21+
window.localStorage.setItem("mx_access_token", credentials.accessToken);
22+
window.localStorage.setItem("mx_device_id", credentials.deviceId);
23+
window.localStorage.setItem("mx_is_guest", "false");
24+
window.localStorage.setItem("mx_has_pickle_key", "false");
25+
window.localStorage.setItem("mx_has_access_token", "true");
26+
27+
window.localStorage.setItem(
28+
"mx_local_settings",
29+
JSON.stringify({
30+
// Retain any other settings which may have already been set
31+
...JSON.parse(window.localStorage.getItem("mx_local_settings") ?? "{}"),
32+
// Ensure the language is set to a consistent value
33+
language: "en",
34+
}),
35+
);
36+
},
37+
{ credentials },
38+
);
39+
}
40+
1541
export const test = base.extend<{
1642
/**
1743
* The displayname to use for the user registered in {@link #credentials}.
@@ -38,7 +64,7 @@ export const test = base.extend<{
3864

3965
/**
4066
* A (rather poorly-named) test fixture which registers a user per {@link #credentials}, stores
41-
* the credentials into localStorage per {@link #homeserver}, and then loads the front page of the
67+
* the credentials into localStorage per {@link #pageWithCredentials}, and then loads the front page of the
4268
* app.
4369
*/
4470
user: Credentials;
@@ -58,30 +84,8 @@ export const test = base.extend<{
5884
});
5985
},
6086

61-
pageWithCredentials: async ({ page, homeserver, credentials }, use) => {
62-
await page.addInitScript(
63-
({ baseUrl, credentials }) => {
64-
// Seed the localStorage with the required credentials
65-
window.localStorage.setItem("mx_hs_url", baseUrl);
66-
window.localStorage.setItem("mx_user_id", credentials.userId);
67-
window.localStorage.setItem("mx_access_token", credentials.accessToken);
68-
window.localStorage.setItem("mx_device_id", credentials.deviceId);
69-
window.localStorage.setItem("mx_is_guest", "false");
70-
window.localStorage.setItem("mx_has_pickle_key", "false");
71-
window.localStorage.setItem("mx_has_access_token", "true");
72-
73-
window.localStorage.setItem(
74-
"mx_local_settings",
75-
JSON.stringify({
76-
// Retain any other settings which may have already been set
77-
...JSON.parse(window.localStorage.getItem("mx_local_settings") ?? "{}"),
78-
// Ensure the language is set to a consistent value
79-
language: "en",
80-
}),
81-
);
82-
},
83-
{ baseUrl: homeserver.baseUrl, credentials },
84-
);
87+
pageWithCredentials: async ({ page, credentials }, use) => {
88+
await populateLocalStorageWithCredentials(page, credentials);
8589
await use(page);
8690
},
8791

packages/element-web-playwright-common/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import { type Config as BaseConfig } from "@element-hq/element-web-module-api";
1010

1111
import { test as base } from "./fixtures/index.js";
1212

13+
export { populateLocalStorageWithCredentials } from "./fixtures/user.js";
14+
1315
// Enable experimental service worker support
1416
// See https://playwright.dev/docs/service-workers-experimental#how-to-enable
1517
process.env["PW_EXPERIMENTAL_SERVICE_WORKER_NETWORK_EVENTS"] = "1";

packages/element-web-playwright-common/src/testcontainers/mas.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ export class StartedMatrixAuthenticationServiceContainer extends AbstractStarted
302302
password: string,
303303
displayName?: string,
304304
admin = false,
305-
): Promise<Credentials> {
305+
): Promise<Omit<Credentials, "homeserverBaseUrl">> {
306306
const userId = await this.manageRegisterUser(username, password, displayName, admin);
307307
const { deviceId, accessToken } = await this.manageIssueCompatibilityToken(username, admin);
308308

@@ -319,11 +319,16 @@ export class StartedMatrixAuthenticationServiceContainer extends AbstractStarted
319319

320320
/**
321321
* Registers a user
322+
*
322323
* @param username - the username of the user to register
323324
* @param password - the password of the user to register
324325
* @param displayName - optional display name to set on the newly registered user
325326
*/
326-
public async registerUser(username: string, password: string, displayName?: string): Promise<Credentials> {
327+
public async registerUser(
328+
username: string,
329+
password: string,
330+
displayName?: string,
331+
): Promise<Omit<Credentials, "homeserverBaseUrl">> {
327332
return this.registerUserInternal(username, password, displayName, false);
328333
}
329334

packages/element-web-playwright-common/src/testcontainers/synapse.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,7 @@ export class StartedSynapseContainer extends AbstractStartedContainer implements
389389

390390
return {
391391
homeServer: data.home_server || data.user_id.split(":").slice(1).join(":"),
392+
homeserverBaseUrl: this.baseUrl,
392393
accessToken: data.access_token,
393394
userId: data.user_id,
394395
deviceId: data.device_id,
@@ -433,7 +434,10 @@ export class StartedSynapseContainer extends AbstractStartedContainer implements
433434
* @param password - login password
434435
*/
435436
public async loginUser(userId: string, password: string): Promise<Credentials> {
436-
return this.csApi.loginUser(userId, password);
437+
return {
438+
...(await this.csApi.loginUser(userId, password)),
439+
homeserverBaseUrl: this.baseUrl,
440+
};
437441
}
438442

439443
/**
@@ -480,8 +484,9 @@ export class StartedSynapseWithMasContainer extends StartedSynapseContainer {
480484
* @param password - the password of the user to register
481485
* @param displayName - optional display name to set on the newly registered user
482486
*/
483-
public registerUser(username: string, password: string, displayName?: string): Promise<Credentials> {
484-
return this.mas.registerUser(username, password, displayName);
487+
public async registerUser(username: string, password: string, displayName?: string): Promise<Credentials> {
488+
const registered = await this.mas.registerUser(username, password, displayName);
489+
return { ...registered, homeserverBaseUrl: this.baseUrl };
485490
}
486491

487492
/**

packages/element-web-playwright-common/src/utils/api.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,16 @@ export class Api {
6464
* Credentials for a user.
6565
*/
6666
export interface Credentials {
67+
/** The base URL of the homeserver's CS API. */
68+
homeserverBaseUrl: string;
69+
6770
accessToken: string;
6871
userId: string;
6972
deviceId: string;
73+
74+
/** The domain part of the user's matrix ID. */
7075
homeServer: string;
76+
7177
password: string | null; // null for password-less users
7278
displayName?: string;
7379
username: string; // the localpart of the userId
@@ -86,7 +92,7 @@ export class ClientServerApi extends Api {
8692
* @param userId - The user ID to register.
8793
* @param password - The password to use for the user.
8894
*/
89-
public async loginUser(userId: string, password: string): Promise<Credentials> {
95+
public async loginUser(userId: string, password: string): Promise<Omit<Credentials, "homeserverBaseUrl">> {
9096
const json = await this.request<{
9197
access_token: string;
9298
user_id: string;

0 commit comments

Comments
 (0)