33 * Copyright IBM Corp. 2023, 2026
44 */
55import path = require( 'path' ) ;
6- import { Workbench , InputBox , DefaultTreeItem , ModalDialog , VSBrowser , WaitHelper , BottomBarPanel , OutputView , DebugToolbar } from 'vscode-extension-tester' ;
6+ import { Workbench , InputBox , DefaultTreeItem , ModalDialog , VSBrowser , WaitHelper , BottomBarPanel , OutputView , DebugToolbar , SideBarView } from 'vscode-extension-tester' ;
77import * as fs from 'fs' ;
88import { STOP_DASHBOARD_MAC_ACTION } from '../definitions/constants' ;
99import { MapContextMenuforMac } from './macUtils' ;
@@ -86,8 +86,8 @@ export async function waitForLanguageServerInit(
8686 return false ;
8787 }
8888 } , {
89- timeout : timeout * 1000 ,
90- pollInterval : 2000 ,
89+ timeout : seconds ( timeout ) ,
90+ pollInterval : seconds ( 2 ) ,
9191 message : `The ${ channelName } output channel did not initialize within ${ timeout } seconds`
9292 } ) ;
9393}
@@ -177,6 +177,13 @@ export async function waitForSuccess(func: () => Promise<any>, timeout: number =
177177 } , timeout ) ;
178178}
179179
180+ /**
181+ * Convert seconds to milliseconds for use in timeout parameters.
182+ */
183+ export function seconds ( s : number ) : number {
184+ return s * 1000 ;
185+ }
186+
180187export function getMvnProjectPath ( ) : string {
181188 const mvnProjectPath = path . join ( __dirname , ".." , ".." , ".." , "src" , "test" , "resources" , "maven" , "liberty-maven-test-wrapper-app" ) ;
182189 logger . info ( "Path is : " + mvnProjectPath ) ;
@@ -198,20 +205,33 @@ export function getGradle9ProjectPath(): string {
198205
199206export async function getDashboardSection ( sidebar : any ) : Promise < any > {
200207 logger . info ( "Getting Liberty Tools section" ) ;
201- return await waitForCondition ( async ( ) => {
202- // Get fresh content on each iteration to avoid stale references
203- const contentPart = sidebar . getContent ( ) ;
204- const sections = await contentPart . getSections ( ) ;
205-
206- // Find the Liberty Tools section
207- for ( const sec of sections ) {
208- const title = await sec . getTitle ( ) ;
209- if ( title === 'Liberty Tools' ) {
210- return sec ;
208+ const wait = getWaitHelper ( ) ;
209+ return await wait . forCondition ( async ( ) => {
210+ try {
211+ // Re-create SideBarView on every iteration — the captured sidebar
212+ // reference goes stale on workspace transitions and getSections()
213+ // throws on every call, bypassing the retry loop.
214+ const freshSidebar = new SideBarView ( ) ;
215+ const sections = await freshSidebar . getContent ( ) . getSections ( ) ;
216+ for ( const sec of sections ) {
217+ const title = await sec . getTitle ( ) ;
218+ if ( title === 'Liberty Tools' ) {
219+ return sec ;
220+ }
221+ }
222+ } catch ( error : any ) {
223+ if ( error . name === 'StaleElementReferenceError' ||
224+ error . name === 'ElementNotInteractableError' ) {
225+ return ;
211226 }
227+ throw error ;
212228 }
213229 return ;
214- } , 30 ) ;
230+ } , {
231+ timeout : seconds ( 30 ) ,
232+ pollInterval : seconds ( 3 ) ,
233+ message : 'Liberty Tools section was not found in sidebar within 30 seconds'
234+ } ) ;
215235}
216236
217237export async function getDashboardItem ( section : any , projectName : string ) : Promise < DefaultTreeItem > {
@@ -239,7 +259,7 @@ export async function getDashboardItem(section: any, projectName: string): Promi
239259 }
240260 throw error ;
241261 }
242- } , { timeout : 10000 , message : 'Dashboard items did not appear after expansion' } ) ;
262+ } , { timeout : seconds ( 10 ) , message : 'Dashboard items did not appear after expansion' } ) ;
243263
244264 // Find the item
245265 return await waitForCondition ( async ( ) => {
@@ -372,8 +392,8 @@ export async function checkTerminalforServerState(serverStatusCode: string): Pro
372392 return ;
373393 }
374394 } , {
375- timeout : 300000 , // 300 seconds (5 minutes) max wait - increased for custom params
376- pollInterval : 10000 , // check every 10 seconds
395+ timeout : seconds ( 5 * 60 ) ,
396+ pollInterval : seconds ( 10 ) ,
377397 message : `Server state '${ serverStatusCode } ' not found in terminal`
378398 } ) ;
379399
@@ -400,8 +420,8 @@ export async function checkTestStatus(testStatus: string): Promise<boolean> {
400420 }
401421 return ;
402422 } , {
403- timeout : 120000 , // 120 seconds (2 minutes) max wait for tests to complete
404- pollInterval : 5000 , // check every 5 seconds
423+ timeout : seconds ( 2 * 60 ) ,
424+ pollInterval : seconds ( 5 ) ,
405425 message : `Test status '${ testStatus } ' not found in terminal`
406426 } ) ;
407427
@@ -442,7 +462,7 @@ export async function clearCommandPalette() {
442462 } catch {
443463 return ;
444464 }
445- } , { timeout : 5000 , message : 'Clear command history dialog did not appear' } ) ;
465+ } , { timeout : seconds ( 5 ) , message : 'Clear command history dialog did not appear' } ) ;
446466
447467 await waitForSuccess ( async ( ) => {
448468 const dialog = new ModalDialog ( ) ;
@@ -489,8 +509,8 @@ export async function waitForDashboardToLoad(section: any): Promise<void> {
489509 }
490510 return ;
491511 } , {
492- timeout : 120000 , // 2 minutes max
493- pollInterval : 5000 , // check every 5 seconds
512+ timeout : seconds ( 3 * 60 ) ,
513+ pollInterval : seconds ( 5 ) ,
494514 message : 'Dashboard items did not load'
495515 } ) ;
496516}
@@ -541,8 +561,8 @@ export async function waitForTestReport(reportPath: string, alternatePath?: stri
541561 }
542562 return ;
543563 } , {
544- timeout : 50000 , // 50 seconds max (for slow systems)
545- pollInterval : 1000 , // check every 1 second (faster detection)
564+ timeout : seconds ( 50 ) ,
565+ pollInterval : seconds ( 1 ) ,
546566 message : alternatePath
547567 ? `Test report not found at either ${ reportPath } or ${ alternatePath } `
548568 : `Test report not found at ${ reportPath } `
@@ -563,7 +583,7 @@ export async function waitForDebuggerAttach(): Promise<boolean> {
563583
564584 try {
565585 // Wait for the debug toolbar to appear, which indicates debugger is attached
566- const findDebugBarTimeout = 30000 ;
586+ const findDebugBarTimeout = seconds ( 30 ) ;
567587 await DebugToolbar . create ( findDebugBarTimeout ) ;
568588 logger . info ( 'DebugToolbar appeared - debugger attached successfully' ) ;
569589 return true ;
0 commit comments