@@ -658,12 +658,16 @@ export class DeviceWrapper {
658658 public async doesElementExist (
659659 args : { text ?: string ; maxWait ?: number } & ( StrategyExtractionObj | LocatorsInterface )
660660 ) {
661- const { text, maxWait } = args ;
662661 const beforeStart = Date . now ( ) ;
663- const maxWaitMSec = maxWait || 30000 ;
662+ const maxWaitMSec = args . maxWait || 30000 ;
664663 const waitPerLoop = 100 ;
665664 let element : AppiumNextElementType | null = null ;
665+
666+ // Build the locator if necessary, and extract the expected text.
667+ // Use the text from the locator if available; otherwise fallback to args.text.
668+ // This ensures that the correct text value is used for matching, preventing false positives in tests.
666669 const locator = args instanceof LocatorsInterface ? args . build ( ) : args ;
670+ const text : string | undefined = ( 'text' in locator ? locator . text : undefined ) || args . text ;
667671
668672 while ( element === null ) {
669673 try {
@@ -674,26 +678,32 @@ export class DeviceWrapper {
674678 element = await this . findMatchingTextInElementArray ( els , text ) ;
675679 if ( element ) {
676680 console . log (
677- `${ locator . strategy } : ${ locator . selector } with matching text ${ text } found`
681+ `${ locator . strategy } : ${ locator . selector } with matching text " ${ text } " found`
678682 ) ;
679683 } else {
680684 console . log (
681- `Couldn't find ${ text } with matching ${ locator . strategy } : ${ locator . selector } `
685+ `Couldn't find " ${ text } " with matching ${ locator . strategy } : ${ locator . selector } `
682686 ) ;
683687 }
684688 }
685689 } catch ( e : any ) {
686- console . info ( `doesElementExist failed with` , ` ${ locator . strategy } ${ locator . selector } `) ;
690+ console . info ( `doesElementExist failed with ${ locator . strategy } ${ locator . selector } ` ) ;
687691 }
688- if ( ! element ) {
689- await sleepFor ( waitPerLoop ) ;
692+ // Break immediately if we found the element
693+ if ( element ) {
694+ break ;
690695 }
691- if ( beforeStart + maxWaitMSec <= Date . now ( ) ) {
692- console . log ( locator . selector , " doesn't exist, time expired" ) ;
696+
697+ // Check for timeout before sleeping
698+ if ( Date . now ( ) >= beforeStart + maxWaitMSec ) {
699+ console . log ( locator . selector , "doesn't exist, time expired" ) ;
693700 break ;
694701 } else {
695702 console . log ( locator . selector , "Doesn't exist but retrying" ) ;
696703 }
704+
705+ // Sleep before trying again
706+ await sleepFor ( waitPerLoop ) ;
697707 }
698708
699709 return element ;
0 commit comments