Skip to content

Commit 0359794

Browse files
Copilotkobenguyent
andcommitted
Fix custom locator registration timing and format issues
Co-authored-by: kobenguyent <[email protected]>
1 parent 40784b1 commit 0359794

File tree

1 file changed

+30
-24
lines changed

1 file changed

+30
-24
lines changed

lib/helper/Playwright.js

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -895,52 +895,48 @@ class Playwright extends Helper {
895895

896896
async _registerCustomLocatorStrategies() {
897897
if (this._isCustomLocatorStrategyDefined()) {
898+
console.log('[DEBUG] Registering custom locator strategies:', Object.keys(this.customLocatorStrategies))
898899
for (const [strategyName, strategyFunction] of Object.entries(this.customLocatorStrategies)) {
899-
// Skip if already registered
900-
if (registeredCustomLocatorStrategies.has(strategyName)) {
901-
continue
902-
}
903-
904900
try {
901+
console.log(`[DEBUG] Registering custom locator strategy: ${strategyName}`)
905902
this.debugSection('Playwright', `registering custom locator strategy: ${strategyName}`)
906903

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) {
912907
try {
913-
const strategyFunction = ${functionString};
914908
const result = strategyFunction(selector, root);
915909
return Array.isArray(result) ? result[0] : result;
916910
} catch (error) {
917-
console.warn('Error in custom locator "` +
918-
strategyName +
919-
`":', error);
911+
console.warn(`Error in custom locator "${strategyName}":`, error);
920912
return null;
921913
}
922-
};
914+
},
923915

924-
exports.queryAll = (root, selector) => {
916+
queryAll(root, selector) {
925917
try {
926-
const strategyFunction = ${functionString};
927918
const result = strategyFunction(selector, root);
928919
return Array.isArray(result) ? result : result ? [result] : [];
929920
} catch (error) {
930-
console.warn('Error in custom locator "` +
931-
strategyName +
932-
`":', error);
921+
console.warn(`Error in custom locator "${strategyName}":`, error);
933922
return [];
934923
}
935-
};
936-
`
924+
}
925+
}
937926

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}`)
939929
registeredCustomLocatorStrategies.add(strategyName)
940930
} 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+
}
942936
}
943937
}
938+
} else {
939+
console.log('[DEBUG] No custom locator strategies defined')
944940
}
945941
}
946942

@@ -1341,21 +1337,31 @@ class Playwright extends Helper {
13411337
}
13421338

13431339
async _ensureCustomLocatorsRegistered() {
1340+
console.log('[DEBUG] _ensureCustomLocatorsRegistered called')
1341+
console.log('[DEBUG] _customLocatorsRegistered:', this._customLocatorsRegistered)
1342+
console.log('[DEBUG] _isCustomLocatorStrategyDefined:', this._isCustomLocatorStrategyDefined())
1343+
13441344
// Only register once, and only if we have strategies defined
13451345
if (this._customLocatorsRegistered || !this._isCustomLocatorStrategyDefined()) {
1346+
console.log('[DEBUG] Skipping custom locator registration - already registered or no strategies defined')
13461347
return
13471348
}
13481349

1350+
console.log('[DEBUG] Proceeding with custom locator registration')
13491351
try {
13501352
// If browser isn't running yet, start it to register selectors
13511353
if (!this.isRunning && !this.options.manualStart) {
1354+
console.log('[DEBUG] Starting browser to register selectors')
13521355
await this._startBrowser()
13531356
} else {
1357+
console.log('[DEBUG] Browser running, registering selectors now')
13541358
// If browser is running but custom locators not registered, register them now
13551359
await this._registerCustomLocatorStrategies()
13561360
}
13571361
this._customLocatorsRegistered = true
1362+
console.log('[DEBUG] Custom locator registration completed')
13581363
} catch (error) {
1364+
console.log('[DEBUG] Failed to register custom locators:', error)
13591365
console.warn('Failed to register custom locators:', error.message)
13601366
// Continue execution - the error will surface when the locator is actually used
13611367
}

0 commit comments

Comments
 (0)