@@ -3327,21 +3327,9 @@ class Playwright extends Helper {
33273327 const waitTimeout = sec ? sec * 1000 : this . options . waitForTimeout
33283328 locator = new Locator ( locator , 'css' )
33293329
3330- let waiter
33313330 const context = await this . _getContext ( )
3332- if ( ! locator . isXPath ( ) ) {
3333- const valueFn = function ( [ locator ] ) {
3334- return Array . from ( document . querySelectorAll ( locator ) ) . filter ( el => ! el . disabled ) . length > 0
3335- }
3336- waiter = context . waitForFunction ( valueFn , [ locator . value ] , { timeout : waitTimeout } )
3337- } else {
3338- const enabledFn = function ( [ locator , $XPath ] ) {
3339- eval ( $XPath )
3340- return $XPath ( null , locator ) . filter ( el => ! el . disabled ) . length > 0
3341- }
3342- waiter = context . waitForFunction ( enabledFn , [ locator . value , $XPath . toString ( ) ] , { timeout : waitTimeout } )
3343- }
3344- return waiter . catch ( err => {
3331+ const enabledFn = els => els . filter ( el => ! el . disabled ) . length > 0
3332+ return waitForElements ( context , locator , enabledFn , null , waitTimeout ) . catch ( err => {
33453333 throw new Error ( `element (${ locator . toString ( ) } ) still not enabled after ${ waitTimeout / 1000 } sec\n${ err . message } ` )
33463334 } )
33473335 }
@@ -3353,21 +3341,9 @@ class Playwright extends Helper {
33533341 const waitTimeout = sec ? sec * 1000 : this . options . waitForTimeout
33543342 locator = new Locator ( locator , 'css' )
33553343
3356- let waiter
33573344 const context = await this . _getContext ( )
3358- if ( ! locator . isXPath ( ) ) {
3359- const valueFn = function ( [ locator ] ) {
3360- return Array . from ( document . querySelectorAll ( locator ) ) . filter ( el => el . disabled ) . length > 0
3361- }
3362- waiter = context . waitForFunction ( valueFn , [ locator . value ] , { timeout : waitTimeout } )
3363- } else {
3364- const disabledFn = function ( [ locator , $XPath ] ) {
3365- eval ( $XPath )
3366- return $XPath ( null , locator ) . filter ( el => el . disabled ) . length > 0
3367- }
3368- waiter = context . waitForFunction ( disabledFn , [ locator . value , $XPath . toString ( ) ] , { timeout : waitTimeout } )
3369- }
3370- return waiter . catch ( err => {
3345+ const disabledFn = els => els . filter ( el => el . disabled ) . length > 0
3346+ return waitForElements ( context , locator , disabledFn , null , waitTimeout ) . catch ( err => {
33713347 throw new Error ( `element (${ locator . toString ( ) } ) is still enabled after ${ waitTimeout / 1000 } sec\n${ err . message } ` )
33723348 } )
33733349 }
@@ -3378,24 +3354,10 @@ class Playwright extends Helper {
33783354 async waitForValue ( field , value , sec ) {
33793355 const waitTimeout = sec ? sec * 1000 : this . options . waitForTimeout
33803356 const locator = new Locator ( field , 'css' )
3381- const matcher = await this . context
3382- let waiter
3357+
33833358 const context = await this . _getContext ( )
3384- if ( ! locator . isXPath ( ) ) {
3385- const valueFn = function ( [ locator , value ] ) {
3386- return Array . from ( document . querySelectorAll ( locator ) ) . filter ( el => ( el . value || '' ) . indexOf ( value ) !== - 1 ) . length > 0
3387- }
3388- waiter = context . waitForFunction ( valueFn , [ locator . value , value ] , { timeout : waitTimeout } )
3389- } else {
3390- const valueFn = function ( [ locator , $XPath , value ] ) {
3391- eval ( $XPath )
3392- return $XPath ( null , locator ) . filter ( el => ( el . value || '' ) . indexOf ( value ) !== - 1 ) . length > 0
3393- }
3394- waiter = context . waitForFunction ( valueFn , [ locator . value , $XPath . toString ( ) , value ] , {
3395- timeout : waitTimeout ,
3396- } )
3397- }
3398- return waiter . catch ( err => {
3359+ const valueFn = ( els , value ) => els . filter ( el => ( el . value || '' ) . indexOf ( value ) !== - 1 ) . length > 0
3360+ return waitForElements ( context , locator , valueFn , value , waitTimeout ) . catch ( err => {
33993361 const loc = locator . toString ( )
34003362 throw new Error ( `element (${ loc } ) is not in DOM or there is no element(${ loc } ) with value "${ value } " after ${ waitTimeout / 1000 } sec\n${ err . message } ` )
34013363 } )
@@ -3409,27 +3371,14 @@ class Playwright extends Helper {
34093371 const waitTimeout = sec ? sec * 1000 : this . options . waitForTimeout
34103372 locator = new Locator ( locator , 'css' )
34113373
3412- let waiter
34133374 const context = await this . _getContext ( )
3375+ let visibleFn
34143376 if ( locator . isCSS ( ) ) {
3415- const visibleFn = function ( [ locator , num ] ) {
3416- const els = document . querySelectorAll ( locator )
3417- if ( ! els || els . length === 0 ) {
3418- return false
3419- }
3420- return Array . prototype . filter . call ( els , el => el . offsetParent !== null ) . length === num
3421- }
3422- waiter = context . waitForFunction ( visibleFn , [ locator . value , num ] , { timeout : waitTimeout } )
3377+ visibleFn = ( els , num ) => els . length > 0 && els . filter ( el => el . offsetParent !== null ) . length === num
34233378 } else {
3424- const visibleFn = function ( [ locator , $XPath , num ] ) {
3425- eval ( $XPath )
3426- return $XPath ( null , locator ) . filter ( el => el . offsetParent !== null ) . length === num
3427- }
3428- waiter = context . waitForFunction ( visibleFn , [ locator . value , $XPath . toString ( ) , num ] , {
3429- timeout : waitTimeout ,
3430- } )
3379+ visibleFn = ( els , num ) => els . filter ( el => el . offsetParent !== null ) . length === num
34313380 }
3432- return waiter . catch ( err => {
3381+ return waitForElements ( context , locator , visibleFn , num , waitTimeout ) . catch ( err => {
34333382 throw new Error ( `The number of elements (${ locator . toString ( ) } ) is not ${ num } after ${ waitTimeout / 1000 } sec\n${ err . message } ` )
34343383 } )
34353384 }
@@ -4412,6 +4361,20 @@ async function findElements(matcher, locator) {
44124361 return withVisibleLocator ( matcher . locator ( locatorString ) ) . all ( )
44134362}
44144363
4364+ async function waitForElements ( context , locator , fn , arg , timeout ) {
4365+ const elements = context . locator ( buildLocatorString ( locator ) )
4366+ const deadline = Date . now ( ) + timeout
4367+ for ( ; ; ) {
4368+ try {
4369+ if ( await elements . evaluateAll ( fn , arg ) ) return
4370+ } catch ( err ) {
4371+ if ( ! err . message . includes ( 'Execution context was destroyed' ) ) throw err
4372+ }
4373+ if ( Date . now ( ) >= deadline ) throw new Error ( `Timeout ${ timeout } ms exceeded.` )
4374+ await new Promise ( resolve => setTimeout ( resolve , 100 ) )
4375+ }
4376+ }
4377+
44154378async function findElement ( matcher , locator ) {
44164379 if ( locator . pw ) return findByPlaywrightLocator . call ( this , matcher , locator ) . first ( )
44174380
0 commit comments