Fix and stabilise sliding sync playwright tests (#28809)

* Fix and stabilise sliding sync playwright tests

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>

* Iterate

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>

* Revert test enablement

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>

* Iterate

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>

* Debug

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>

* Iterate

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>

* Unskip now fixed tests

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>

---------

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
This commit is contained in:
Michael Telatynski
2025-01-02 20:20:13 +00:00
committed by GitHub
parent 0555701829
commit bd3e93e8dd
8 changed files with 147 additions and 84 deletions

View File

@@ -37,6 +37,10 @@ export class ElementAppPage {
return this._timeline;
}
public async cleanup() {
await this._client?.cleanup();
}
/**
* Open the top left user menu, returning a Locator to the resulting context menu.
*/

View File

@@ -52,6 +52,10 @@ export class Client {
this.network = new Network(page, this);
}
public async cleanup() {
await this.network.destroyRoute();
}
public evaluate<R, Arg, O extends MatrixClient = MatrixClient>(
pageFunction: PageFunctionOn<O, Arg, R>,
arg: Arg,

View File

@@ -6,19 +6,22 @@ SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
Please see LICENSE files in the repository root for full details.
*/
import type { Page, Request } from "@playwright/test";
import type { Page, Request, Route } from "@playwright/test";
import type { Client } from "./client";
/**
* Utility class to simulate offline mode by blocking all requests to the homeserver.
* Will not affect any requests before `setupRoute` is called,
* which happens implicitly using the goOffline/goOnline methods.
*/
export class Network {
private isOffline = false;
private readonly setupPromise: Promise<void>;
private setupPromise?: Promise<void>;
constructor(
private page: Page,
private client: Client,
) {
this.setupPromise = this.setupRoute();
}
) {}
/**
* Checks if the request is from the client associated with this network object.
@@ -30,25 +33,47 @@ export class Network {
return authHeader === `Bearer ${accessToken}`;
}
private async setupRoute() {
await this.page.route("**/_matrix/**", async (route) => {
if (this.isOffline && (await this.isRequestFromOurClient(route.request()))) {
route.abort();
} else {
route.continue();
}
});
private handler = async (route: Route) => {
if (this.isOffline && (await this.isRequestFromOurClient(route.request()))) {
await route.abort();
} else {
await route.continue();
}
};
/**
* Intercept all /_matrix/ networking requests for client ready to continue/abort them based on offline status
* which is set by the goOffline/goOnline methods
*/
public async setupRoute() {
if (!this.setupPromise) {
this.setupPromise = this.page.route("**/_matrix/**", this.handler);
}
await this.setupPromise;
}
// Intercept all /_matrix/ networking requests for client and fail them
/**
* Cease intercepting all /_matrix/ networking requests for client
*/
public async destroyRoute() {
if (!this.setupPromise) return;
await this.page.unroute("**/_matrix/**", this.handler);
this.setupPromise = undefined;
}
/**
* Reject all /_matrix/ networking requests for client
*/
async goOffline(): Promise<void> {
await this.setupPromise;
await this.setupRoute();
this.isOffline = true;
}
// Remove intercept on all /_matrix/ networking requests for this client
/**
* Continue all /_matrix/ networking requests for this client
*/
async goOnline(): Promise<void> {
await this.setupPromise;
await this.setupRoute();
this.isOffline = false;
}
}