From a94903221ea0d726b552daf49af775f215722c4a Mon Sep 17 00:00:00 2001 From: Sid Vishnoi <8426945+sidvishnoi@users.noreply.github.com> Date: Wed, 9 Oct 2024 18:08:48 +0530 Subject: [PATCH] don't hardcode continueWaitTimeMs --- tests/e2e/connectAutoKeyFynbos.spec.ts | 8 +++++- tests/e2e/helpers/fynbos.ts | 34 +++++++++++++++++++++++++- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/tests/e2e/connectAutoKeyFynbos.spec.ts b/tests/e2e/connectAutoKeyFynbos.spec.ts index 2b384fdf..91b57ed9 100644 --- a/tests/e2e/connectAutoKeyFynbos.spec.ts +++ b/tests/e2e/connectAutoKeyFynbos.spec.ts @@ -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, @@ -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((resolve) => { page.on('request', async function interceptApplicationName(req) { @@ -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( diff --git a/tests/e2e/helpers/fynbos.ts b/tests/e2e/helpers/fynbos.ts index 0774af8e..7a4f595c 100644 --- a/tests/e2e/helpers/fynbos.ts +++ b/tests/e2e/helpers/fynbos.ts @@ -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`; @@ -21,6 +23,36 @@ export async function waitForGrantConsentPage(page: Page) { }); } +export async function getContinueWaitTime( + context: BrowserContext, + params: Pick, +) { + 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((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();