gui/modules/desktop/test/specs/waitutils.ts
ABevier 67edf9971b
more e2e tuning (#614)
* more e2e tuning

* prettier
2023-05-16 07:53:27 +08:00

34 lines
898 B
TypeScript

export const sleep = (delayMs: number) => new Promise((resolve) => setTimeout(resolve, delayMs));
export type WaitOpts = { timeout?: number; interval?: number };
// wait for a query to return a non-truthy value
export const waitForNotExist = async (
query: () => Promise<WebdriverIO.Element | null>,
opts: WaitOpts = {}
) => {
return waitFor(async () => await expect(await query()).not.toExist(), opts);
};
export const waitFor = async (
action: () => Promise<void>,
{ interval = 1000, timeout = 5000 }: WaitOpts = {}
) => {
const start = Date.now();
let lastErr;
console.log("Wait For", { interval, timeout });
while (Date.now() - start < timeout) {
try {
await action();
return;
} catch (e) {
// swallow it and keep trying
lastErr = e;
}
await sleep(interval);
}
throw new Error(`waitFor timed out. Last error: ${lastErr}`);
};