From 487b571c3deea4650f458dd42a3d0436202e22c0 Mon Sep 17 00:00:00 2001 From: sachiniSam Date: Tue, 8 Jul 2025 10:47:26 +0530 Subject: [PATCH] Fix intermittent test failure on projectSetup --- .../src/test/e2e-playwright-tests/utils.ts | 51 +++++++++++++++++-- 1 file changed, 46 insertions(+), 5 deletions(-) diff --git a/workspaces/bi/bi-extension/src/test/e2e-playwright-tests/utils.ts b/workspaces/bi/bi-extension/src/test/e2e-playwright-tests/utils.ts index 96a61922c61..9bc6f0b2369 100644 --- a/workspaces/bi/bi-extension/src/test/e2e-playwright-tests/utils.ts +++ b/workspaces/bi/bi-extension/src/test/e2e-playwright-tests/utils.ts @@ -62,7 +62,7 @@ export async function toggleNotifications(disable: boolean) { export async function setupBallerinaIntegrator() { await page.selectSidebarItem('WSO2 Integrator: BI'); - const webview = await switchToIFrame('WSO2 Integrator: BI', page.page); + const webview = await getWebview('WSO2 Integrator: BI', page); if (!webview) { throw new Error('WSO2 Integrator: BI webview not found'); } @@ -86,10 +86,51 @@ export async function setupBallerinaIntegrator() { } } +async function getWebview(viewName: string, page: ExtendedPage) { + let webview; + let retryCount = 0; + const maxRetries = 3; + + while (retryCount < maxRetries) { + try { + await page.page.waitForLoadState('domcontentloaded'); + await page.page.waitForTimeout(1000); + + webview = await switchToIFrame(viewName, page.page); + if (webview) { + return webview; + } + // If webview is falsy, treat it as a failed attempt + console.log(`Attempt ${retryCount + 1} failed: switchToIFrame returned ${webview}`); + } catch (error) { + const message = error instanceof Error ? error.message : String(error); + if (message.includes('Frame was detached')) { + console.log(`Frame was detached, retrying (${retryCount + 1}/${maxRetries})`); + } else { + console.log(`Attempt ${retryCount + 1} failed to access iframe:`, message); + } + } + + // Always increment retry count after each attempt + retryCount++; + + // Only retry if we haven't reached max retries + if (retryCount < maxRetries) { + await page.page.waitForTimeout(2000); + try { + await page.selectSidebarItem(viewName); + } catch (sidebarError) { + console.log('Failed to reselect sidebar item:', sidebarError); + } + } + } + throw new Error(`Failed to access iframe for ${viewName} after ${maxRetries} attempts`); +} + export async function createProject(page: ExtendedPage, projectName?: string) { console.log('Creating new project'); await setupBallerinaIntegrator(); - const webview = await switchToIFrame('WSO2 Integrator: BI', page.page, 60000); + const webview = await getWebview('WSO2 Integrator: BI', page); if (!webview) { throw new Error('WSO2 Integrator: BI webview not found'); } @@ -108,7 +149,7 @@ export async function createProject(page: ExtendedPage, projectName?: string) { } }); await form.submit('Create Integration'); - const artifactWebView = await switchToIFrame('WSO2 Integrator: BI', page.page); + const artifactWebView = await getWebview('WSO2 Integrator: BI', page); if (!artifactWebView) { throw new Error('WSO2 Integrator: BI webview not found'); } @@ -148,7 +189,7 @@ export function initTest(newProject: boolean = false, skipProjectCreation: boole export async function addArtifact(artifactName: string, testId: string) { console.log(`Adding artifact: ${artifactName}`); - const artifactWebView = await switchToIFrame('WSO2 Integrator: BI', page.page); + const artifactWebView = await getWebview('WSO2 Integrator: BI', page); if (!artifactWebView) { throw new Error('WSO2 Integrator: BI webview not found'); } @@ -162,7 +203,7 @@ export async function addArtifact(artifactName: string, testId: string) { export async function enableICP() { console.log('Enabling ICP'); - const webview = await switchToIFrame('WSO2 Integrator: BI', page.page); + const webview = await getWebview('WSO2 Integrator: BI', page); if (!webview) { throw new Error('WSO2 Integrator: BI webview not found'); }