@@ -62,7 +62,7 @@ export async function toggleNotifications(disable: boolean) {
6262
6363export async function setupBallerinaIntegrator ( ) {
6464 await page . selectSidebarItem ( 'WSO2 Integrator: BI' ) ;
65- const webview = await switchToIFrame ( 'WSO2 Integrator: BI' , page . page ) ;
65+ const webview = await getWebview ( 'WSO2 Integrator: BI' , page ) ;
6666 if ( ! webview ) {
6767 throw new Error ( 'WSO2 Integrator: BI webview not found' ) ;
6868 }
@@ -86,10 +86,51 @@ export async function setupBallerinaIntegrator() {
8686 }
8787}
8888
89+ async function getWebview ( viewName : string , page : ExtendedPage ) {
90+ let webview ;
91+ let retryCount = 0 ;
92+ const maxRetries = 3 ;
93+
94+ while ( retryCount < maxRetries ) {
95+ try {
96+ await page . page . waitForLoadState ( 'domcontentloaded' ) ;
97+ await page . page . waitForTimeout ( 1000 ) ;
98+
99+ webview = await switchToIFrame ( viewName , page . page ) ;
100+ if ( webview ) {
101+ return webview ;
102+ }
103+ // If webview is falsy, treat it as a failed attempt
104+ console . log ( `Attempt ${ retryCount + 1 } failed: switchToIFrame returned ${ webview } ` ) ;
105+ } catch ( error ) {
106+ const message = error instanceof Error ? error . message : String ( error ) ;
107+ if ( message . includes ( 'Frame was detached' ) ) {
108+ console . log ( `Frame was detached, retrying (${ retryCount + 1 } /${ maxRetries } )` ) ;
109+ } else {
110+ console . log ( `Attempt ${ retryCount + 1 } failed to access iframe:` , message ) ;
111+ }
112+ }
113+
114+ // Always increment retry count after each attempt
115+ retryCount ++ ;
116+
117+ // Only retry if we haven't reached max retries
118+ if ( retryCount < maxRetries ) {
119+ await page . page . waitForTimeout ( 2000 ) ;
120+ try {
121+ await page . selectSidebarItem ( viewName ) ;
122+ } catch ( sidebarError ) {
123+ console . log ( 'Failed to reselect sidebar item:' , sidebarError ) ;
124+ }
125+ }
126+ }
127+ throw new Error ( `Failed to access iframe for ${ viewName } after ${ maxRetries } attempts` ) ;
128+ }
129+
89130export async function createProject ( page : ExtendedPage , projectName ?: string ) {
90131 console . log ( 'Creating new project' ) ;
91132 await setupBallerinaIntegrator ( ) ;
92- const webview = await switchToIFrame ( 'WSO2 Integrator: BI' , page . page , 60000 ) ;
133+ const webview = await getWebview ( 'WSO2 Integrator: BI' , page ) ;
93134 if ( ! webview ) {
94135 throw new Error ( 'WSO2 Integrator: BI webview not found' ) ;
95136 }
@@ -108,7 +149,7 @@ export async function createProject(page: ExtendedPage, projectName?: string) {
108149 }
109150 } ) ;
110151 await form . submit ( 'Create Integration' ) ;
111- const artifactWebView = await switchToIFrame ( 'WSO2 Integrator: BI' , page . page ) ;
152+ const artifactWebView = await getWebview ( 'WSO2 Integrator: BI' , page ) ;
112153 if ( ! artifactWebView ) {
113154 throw new Error ( 'WSO2 Integrator: BI webview not found' ) ;
114155 }
@@ -148,7 +189,7 @@ export function initTest(newProject: boolean = false, skipProjectCreation: boole
148189
149190export async function addArtifact ( artifactName : string , testId : string ) {
150191 console . log ( `Adding artifact: ${ artifactName } ` ) ;
151- const artifactWebView = await switchToIFrame ( 'WSO2 Integrator: BI' , page . page ) ;
192+ const artifactWebView = await getWebview ( 'WSO2 Integrator: BI' , page ) ;
152193 if ( ! artifactWebView ) {
153194 throw new Error ( 'WSO2 Integrator: BI webview not found' ) ;
154195 }
@@ -162,7 +203,7 @@ export async function addArtifact(artifactName: string, testId: string) {
162203
163204export async function enableICP ( ) {
164205 console . log ( 'Enabling ICP' ) ;
165- const webview = await switchToIFrame ( 'WSO2 Integrator: BI' , page . page ) ;
206+ const webview = await getWebview ( 'WSO2 Integrator: BI' , page ) ;
166207 if ( ! webview ) {
167208 throw new Error ( 'WSO2 Integrator: BI webview not found' ) ;
168209 }
0 commit comments