@@ -359,8 +359,10 @@ class Playwright extends Helper {
359
359
// override defaults with config
360
360
this . _setConfig ( config )
361
361
362
- // Ensure _init() is called early to register custom selectors
363
- this . _init ( ) . catch ( console . warn )
362
+ // Call _init() to register selector engines - use setTimeout to avoid blocking constructor
363
+ setTimeout ( ( ) => {
364
+ this . _init ( ) . catch ( console . error )
365
+ } , 0 )
364
366
}
365
367
366
368
_validateConfig ( config ) {
@@ -477,12 +479,54 @@ class Playwright extends Helper {
477
479
478
480
async _init ( ) {
479
481
// register an internal selector engine for reading value property of elements in a selector
480
- if ( defaultSelectorEnginesInitialized ) return
481
- defaultSelectorEnginesInitialized = true
482
482
try {
483
- await playwright . selectors . register ( '__value' , createValueEngine )
484
- await playwright . selectors . register ( '__disabled' , createDisabledEngine )
485
- if ( process . env . testIdAttribute ) await playwright . selectors . setTestIdAttribute ( process . env . testIdAttribute )
483
+ if ( ! defaultSelectorEnginesInitialized ) {
484
+ await playwright . selectors . register ( '__value' , createValueEngine )
485
+ await playwright . selectors . register ( '__disabled' , createDisabledEngine )
486
+ if ( process . env . testIdAttribute ) await playwright . selectors . setTestIdAttribute ( process . env . testIdAttribute )
487
+ defaultSelectorEnginesInitialized = true
488
+ }
489
+
490
+ // Register all custom locator strategies from the global registry
491
+ for ( const [ strategyName , strategyFunction ] of globalCustomLocatorStrategies . entries ( ) ) {
492
+ if ( ! registeredCustomLocatorStrategies . has ( strategyName ) ) {
493
+ try {
494
+ // Create a selector engine factory function exactly like createValueEngine pattern
495
+ const createCustomEngine = ( ) => {
496
+ return {
497
+ create ( root , target ) {
498
+ return null ;
499
+ } ,
500
+ query ( root , selector ) {
501
+ try {
502
+ const result = strategyFunction ( selector , root ) ;
503
+ return Array . isArray ( result ) ? result [ 0 ] : result ;
504
+ } catch ( error ) {
505
+ console . warn ( `Error in custom locator "${ strategyName } ":` , error ) ;
506
+ return null ;
507
+ }
508
+ } ,
509
+ queryAll ( root , selector ) {
510
+ try {
511
+ const result = strategyFunction ( selector , root ) ;
512
+ return Array . isArray ( result ) ? result : result ? [ result ] : [ ] ;
513
+ } catch ( error ) {
514
+ console . warn ( `Error in custom locator "${ strategyName } ":` , error ) ;
515
+ return [ ] ;
516
+ }
517
+ }
518
+ } ;
519
+ } ;
520
+
521
+ await playwright . selectors . register ( strategyName , createCustomEngine )
522
+ registeredCustomLocatorStrategies . add ( strategyName )
523
+ } catch ( error ) {
524
+ if ( ! error . message . includes ( 'already registered' ) ) {
525
+ console . warn ( `Failed to register custom locator strategy '${ strategyName } ':` , error )
526
+ }
527
+ }
528
+ }
529
+ }
486
530
} catch ( e ) {
487
531
console . warn ( e )
488
532
}
@@ -872,56 +916,10 @@ class Playwright extends Helper {
872
916
this . debugSection ( 'Url' , target . url ( ) )
873
917
} )
874
918
875
- // Register custom locator strategies after browser is started
876
- await this . _ensureCustomLocatorsRegistered ( )
877
-
878
919
this . isRunning = true
879
920
return this . browser
880
921
}
881
922
882
- async _ensureCustomLocatorsRegistered ( ) {
883
- // Register all custom locator strategies from the global registry
884
- for ( const [ strategyName , strategyFunction ] of globalCustomLocatorStrategies . entries ( ) ) {
885
- if ( ! registeredCustomLocatorStrategies . has ( strategyName ) ) {
886
- try {
887
- // Create a selector engine factory function like createValueEngine
888
- const createCustomEngine = ( ) => {
889
- return {
890
- create ( root , target ) {
891
- return null ;
892
- } ,
893
- query ( root , selector ) {
894
- try {
895
- const result = strategyFunction ( selector , root ) ;
896
- return Array . isArray ( result ) ? result [ 0 ] : result ;
897
- } catch ( error ) {
898
- console . warn ( `Error in custom locator "${ strategyName } ":` , error ) ;
899
- return null ;
900
- }
901
- } ,
902
- queryAll ( root , selector ) {
903
- try {
904
- const result = strategyFunction ( selector , root ) ;
905
- return Array . isArray ( result ) ? result : result ? [ result ] : [ ] ;
906
- } catch ( error ) {
907
- console . warn ( `Error in custom locator "${ strategyName } ":` , error ) ;
908
- return [ ] ;
909
- }
910
- }
911
- } ;
912
- } ;
913
-
914
- await playwright . selectors . register ( strategyName , createCustomEngine )
915
- registeredCustomLocatorStrategies . add ( strategyName )
916
- } catch ( error ) {
917
- if ( ! error . message . includes ( 'already registered' ) ) {
918
- console . warn ( `Failed to register custom locator strategy '${ strategyName } ':` , error )
919
- }
920
- }
921
- }
922
- }
923
- }
924
-
925
923
_lookupCustomLocator ( customStrategy ) {
926
924
if ( typeof this . customLocatorStrategies !== 'object' || this . customLocatorStrategies === null ) {
927
925
return null
@@ -1348,9 +1346,6 @@ class Playwright extends Helper {
1348
1346
* ```
1349
1347
*/
1350
1348
async _locate ( locator ) {
1351
- // Ensure custom locators are registered before any locator operations
1352
- await this . _ensureCustomLocatorsRegistered ( )
1353
-
1354
1349
const context = await this . _getContext ( )
1355
1350
1356
1351
if ( this . frame ) return findElements ( this . frame , locator )
@@ -1382,9 +1377,6 @@ class Playwright extends Helper {
1382
1377
* ```
1383
1378
*/
1384
1379
async _locateElement ( locator ) {
1385
- // Ensure custom locators are registered before any locator operations
1386
- await this . _ensureCustomLocatorsRegistered ( )
1387
-
1388
1380
const context = await this . _getContext ( )
1389
1381
return findElement ( context , locator )
1390
1382
}
0 commit comments