Skip to content

Commit

Permalink
don't hardcode continueWaitTimeMs
Browse files Browse the repository at this point in the history
  • Loading branch information
sidvishnoi committed Oct 9, 2024
1 parent d618f34 commit a949032
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
8 changes: 7 additions & 1 deletion tests/e2e/connectAutoKeyFynbos.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ensureEnd } from '@/shared/helpers';
import { disconnectWallet, fillPopup } from './pages/popup';
import {
acceptGrant,
getContinueWaitTime,
KEYS_PAGE_URL,
LOGIN_PAGE_URL,
revokeKey,
Expand Down Expand Up @@ -79,6 +80,10 @@ test('Connect to Fynbos with automatic key addition when not logged-in to wallet
return openedPage;
});

const continueWaitMsPromise = getContinueWaitTime(context, {
walletAddressUrl,
});

const revokeInfo = await test.step('adds key to wallet', async () => {
const applicationName = await new Promise<string>((resolve) => {
page.on('request', async function interceptApplicationName(req) {
Expand Down Expand Up @@ -151,7 +156,8 @@ test('Connect to Fynbos with automatic key addition when not logged-in to wallet
});

await test.step('connects', async () => {
await acceptGrant(page, 2000);
const continueWaitMs = await continueWaitMsPromise;
await acceptGrant(page, continueWaitMs);
await waitForWelcomePage(page);

expect(
Expand Down
34 changes: 33 additions & 1 deletion tests/e2e/helpers/fynbos.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { Page } from '@playwright/test';
import type { BrowserContext, Page } from '@playwright/test';
import type { ConnectDetails } from '../pages/popup';
import { waitForWelcomePage } from './common';
import { getWalletInformation } from '@/shared/helpers';

export const KEYS_PAGE_URL = `https://eu1.fynbos.dev/settings/keys`;
export const LOGIN_PAGE_URL = `https://eu1.fynbos.dev/login?returnTo=%2Fsettings%2Fkeys`;
Expand All @@ -21,6 +23,36 @@ export async function waitForGrantConsentPage(page: Page) {
});
}

export async function getContinueWaitTime(
context: BrowserContext,
params: Pick<ConnectDetails, 'walletAddressUrl'>,
) {
const continueWaitMs = await (async () => {
const defaultWaitMs = 1001;
if (process.env.PW_EXPERIMENTAL_SERVICE_WORKER_NETWORK_EVENTS !== '1') {
return Promise.resolve(defaultWaitMs);
}
const walletInfo = await getWalletInformation(params.walletAddressUrl);
return await new Promise<number>((resolve) => {
const authServer = new URL(walletInfo.authServer).href;
context.on('requestfinished', async function intercept(req) {
if (!req.serviceWorker()) return;
if (new URL(req.url()).href !== authServer) return;

const res = await req.response();
if (!res) return;
const json = await res.json();
context.off('requestfinished', intercept);
if (typeof json?.continue?.wait !== 'number') {
return resolve(defaultWaitMs);
}
return resolve(json.continue.wait * 1000);
});
});
})();
return continueWaitMs;
}

export async function acceptGrant(page: Page, continueWaitMs: number) {
await page.waitForTimeout(continueWaitMs);
await page.getByRole('button', { name: 'Approve', exact: true }).click();
Expand Down

0 comments on commit a949032

Please sign in to comment.