Skip to content

Commit 3ff7268

Browse files
Copilotkobenguyent
andcommitted
Fix custom locator registration with proper timing and format
Co-authored-by: kobenguyent <[email protected]>
1 parent dfacc2d commit 3ff7268

File tree

1 file changed

+51
-59
lines changed

1 file changed

+51
-59
lines changed

lib/helper/Playwright.js

Lines changed: 51 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -359,8 +359,10 @@ class Playwright extends Helper {
359359
// override defaults with config
360360
this._setConfig(config)
361361

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)
364366
}
365367

366368
_validateConfig(config) {
@@ -477,12 +479,54 @@ class Playwright extends Helper {
477479

478480
async _init() {
479481
// register an internal selector engine for reading value property of elements in a selector
480-
if (defaultSelectorEnginesInitialized) return
481-
defaultSelectorEnginesInitialized = true
482482
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+
}
486530
} catch (e) {
487531
console.warn(e)
488532
}
@@ -872,56 +916,10 @@ class Playwright extends Helper {
872916
this.debugSection('Url', target.url())
873917
})
874918

875-
// Register custom locator strategies after browser is started
876-
await this._ensureCustomLocatorsRegistered()
877-
878919
this.isRunning = true
879920
return this.browser
880921
}
881922

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-
925923
_lookupCustomLocator(customStrategy) {
926924
if (typeof this.customLocatorStrategies !== 'object' || this.customLocatorStrategies === null) {
927925
return null
@@ -1348,9 +1346,6 @@ class Playwright extends Helper {
13481346
* ```
13491347
*/
13501348
async _locate(locator) {
1351-
// Ensure custom locators are registered before any locator operations
1352-
await this._ensureCustomLocatorsRegistered()
1353-
13541349
const context = await this._getContext()
13551350

13561351
if (this.frame) return findElements(this.frame, locator)
@@ -1382,9 +1377,6 @@ class Playwright extends Helper {
13821377
* ```
13831378
*/
13841379
async _locateElement(locator) {
1385-
// Ensure custom locators are registered before any locator operations
1386-
await this._ensureCustomLocatorsRegistered()
1387-
13881380
const context = await this._getContext()
13891381
return findElement(context, locator)
13901382
}

0 commit comments

Comments
 (0)