@@ -10,6 +10,7 @@ import {
10
10
ElementHandle ,
11
11
devices ,
12
12
Locator ,
13
+ errors as PlaywrightErrors ,
13
14
} from 'playwright'
14
15
import path from 'path'
15
16
@@ -427,16 +428,36 @@ export class Playwright extends BrowserInterface {
427
428
)
428
429
}
429
430
430
- waitForElementByCss ( selector , timeout = 10_000 ) {
431
- return this . chain ( ( ) => {
432
- return page
431
+ waitForElementByCss ( selector : string , timeout = 10_000 ) {
432
+ return this . chain ( async ( ) => {
433
+ const el = await page
433
434
. waitForSelector ( selector , { timeout, state : 'attached' } )
434
- . then ( async ( el ) => {
435
- // it seems selenium waits longer and tests rely on this behavior
436
- // so we wait for the load event fire before returning
437
- await page . waitForLoadState ( )
438
- return this . wrapElement ( el , selector )
435
+ . catch ( ( err ) => {
436
+ if ( err instanceof PlaywrightErrors . TimeoutError ) {
437
+ // The default error message from playwright doesn't tell us which selector we were waiting for.
438
+ throw new Error (
439
+ `Playwright.waitForElementByCss: Timeout ${ timeout } ms exceeded while waiting for '${ selector } '`
440
+ )
441
+ } else {
442
+ throw err
443
+ }
439
444
} )
445
+
446
+ // it seems selenium waits longer and tests rely on this behavior
447
+ // so we wait for the load event fire before returning
448
+ const targetLoadState : Parameters < Page [ 'waitForLoadState' ] > [ 0 ] = 'load'
449
+ await page . waitForLoadState ( targetLoadState , { timeout } ) . catch ( ( err ) => {
450
+ if ( err instanceof PlaywrightErrors . TimeoutError ) {
451
+ // The default error message from playwright doesn't tell us which selector we were waiting for.
452
+ throw new Error (
453
+ `Playwright.waitForElementByCss: Timeout ${ timeout } ms exceeded while waiting for the '${ targetLoadState } ' event after finding '${ selector } '`
454
+ )
455
+ } else {
456
+ throw err
457
+ }
458
+ } )
459
+
460
+ return this . wrapElement ( el , selector )
440
461
} )
441
462
}
442
463
0 commit comments