@@ -895,52 +895,48 @@ class Playwright extends Helper {
895
895
896
896
async _registerCustomLocatorStrategies ( ) {
897
897
if ( this . _isCustomLocatorStrategyDefined ( ) ) {
898
+ console . log ( '[DEBUG] Registering custom locator strategies:' , Object . keys ( this . customLocatorStrategies ) )
898
899
for ( const [ strategyName , strategyFunction ] of Object . entries ( this . customLocatorStrategies ) ) {
899
- // Skip if already registered
900
- if ( registeredCustomLocatorStrategies . has ( strategyName ) ) {
901
- continue
902
- }
903
-
904
900
try {
901
+ console . log ( `[DEBUG] Registering custom locator strategy: ${ strategyName } ` )
905
902
this . debugSection ( 'Playwright' , `registering custom locator strategy: ${ strategyName } ` )
906
903
907
- // Convert the function to a string and create the selector engine content
908
- const functionString = strategyFunction . toString ( )
909
- const selectorEngine =
910
- `
911
- exports.query = (root, selector) => {
904
+ // Create a selector engine object similar to the default engines
905
+ const selectorEngine = {
906
+ query ( root , selector ) {
912
907
try {
913
- const strategyFunction = ${ functionString } ;
914
908
const result = strategyFunction ( selector , root ) ;
915
909
return Array . isArray ( result ) ? result [ 0 ] : result ;
916
910
} catch ( error ) {
917
- console.warn('Error in custom locator "` +
918
- strategyName +
919
- `":', error);
911
+ console . warn ( `Error in custom locator "${ strategyName } ":` , error ) ;
920
912
return null ;
921
913
}
922
- };
914
+ } ,
923
915
924
- exports. queryAll = (root, selector) => {
916
+ queryAll ( root , selector ) {
925
917
try {
926
- const strategyFunction = ${ functionString } ;
927
918
const result = strategyFunction ( selector , root ) ;
928
919
return Array . isArray ( result ) ? result : result ? [ result ] : [ ] ;
929
920
} catch ( error ) {
930
- console.warn('Error in custom locator "` +
931
- strategyName +
932
- `":', error);
921
+ console . warn ( `Error in custom locator "${ strategyName } ":` , error ) ;
933
922
return [ ] ;
934
923
}
935
- };
936
- `
924
+ }
925
+ }
937
926
938
- await playwright . selectors . register ( strategyName , { content : selectorEngine } )
927
+ await playwright . selectors . register ( strategyName , selectorEngine )
928
+ console . log ( `[DEBUG] Successfully registered custom locator strategy: ${ strategyName } ` )
939
929
registeredCustomLocatorStrategies . add ( strategyName )
940
930
} catch ( error ) {
941
- console . warn ( `Failed to register custom locator strategy '${ strategyName } ':` , error )
931
+ console . log ( `[DEBUG] Error registering custom locator strategy '${ strategyName } ':` , error )
932
+ // Ignore "already registered" errors, warn about others
933
+ if ( ! error . message . includes ( 'already registered' ) ) {
934
+ console . warn ( `Failed to register custom locator strategy '${ strategyName } ':` , error )
935
+ }
942
936
}
943
937
}
938
+ } else {
939
+ console . log ( '[DEBUG] No custom locator strategies defined' )
944
940
}
945
941
}
946
942
@@ -1341,21 +1337,31 @@ class Playwright extends Helper {
1341
1337
}
1342
1338
1343
1339
async _ensureCustomLocatorsRegistered ( ) {
1340
+ console . log ( '[DEBUG] _ensureCustomLocatorsRegistered called' )
1341
+ console . log ( '[DEBUG] _customLocatorsRegistered:' , this . _customLocatorsRegistered )
1342
+ console . log ( '[DEBUG] _isCustomLocatorStrategyDefined:' , this . _isCustomLocatorStrategyDefined ( ) )
1343
+
1344
1344
// Only register once, and only if we have strategies defined
1345
1345
if ( this . _customLocatorsRegistered || ! this . _isCustomLocatorStrategyDefined ( ) ) {
1346
+ console . log ( '[DEBUG] Skipping custom locator registration - already registered or no strategies defined' )
1346
1347
return
1347
1348
}
1348
1349
1350
+ console . log ( '[DEBUG] Proceeding with custom locator registration' )
1349
1351
try {
1350
1352
// If browser isn't running yet, start it to register selectors
1351
1353
if ( ! this . isRunning && ! this . options . manualStart ) {
1354
+ console . log ( '[DEBUG] Starting browser to register selectors' )
1352
1355
await this . _startBrowser ( )
1353
1356
} else {
1357
+ console . log ( '[DEBUG] Browser running, registering selectors now' )
1354
1358
// If browser is running but custom locators not registered, register them now
1355
1359
await this . _registerCustomLocatorStrategies ( )
1356
1360
}
1357
1361
this . _customLocatorsRegistered = true
1362
+ console . log ( '[DEBUG] Custom locator registration completed' )
1358
1363
} catch ( error ) {
1364
+ console . log ( '[DEBUG] Failed to register custom locators:' , error )
1359
1365
console . warn ( 'Failed to register custom locators:' , error . message )
1360
1366
// Continue execution - the error will surface when the locator is actually used
1361
1367
}
0 commit comments