Skip to content

Commit dfacc2d

Browse files
Copilotkobenguyent
andcommitted
Fix custom locator registration timing and format issues
Co-authored-by: kobenguyent <7845001+kobenguyent@users.noreply.github.com>
1 parent 541f5ef commit dfacc2d

File tree

1 file changed

+52
-43
lines changed

1 file changed

+52
-43
lines changed

lib/helper/Playwright.js

Lines changed: 52 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -483,49 +483,6 @@ class Playwright extends Helper {
483483
await playwright.selectors.register('__value', createValueEngine)
484484
await playwright.selectors.register('__disabled', createDisabledEngine)
485485
if (process.env.testIdAttribute) await playwright.selectors.setTestIdAttribute(process.env.testIdAttribute)
486-
487-
// Register all custom locator strategies from the global registry
488-
for (const [strategyName, strategyFunction] of globalCustomLocatorStrategies.entries()) {
489-
if (!registeredCustomLocatorStrategies.has(strategyName)) {
490-
try {
491-
// Create a selector engine as JavaScript code string
492-
const selectorEngineCode = `
493-
(() => ({
494-
create(root, target) {
495-
return null;
496-
},
497-
query(root, selector) {
498-
try {
499-
const strategyFunction = ${strategyFunction.toString()};
500-
const result = strategyFunction(selector, root);
501-
return Array.isArray(result) ? result[0] : result;
502-
} catch (error) {
503-
console.warn('Error in custom locator "${strategyName}":', error);
504-
return null;
505-
}
506-
},
507-
queryAll(root, selector) {
508-
try {
509-
const strategyFunction = ${strategyFunction.toString()};
510-
const result = strategyFunction(selector, root);
511-
return Array.isArray(result) ? result : result ? [result] : [];
512-
} catch (error) {
513-
console.warn('Error in custom locator "${strategyName}":', error);
514-
return [];
515-
}
516-
}
517-
}))()
518-
`;
519-
520-
await playwright.selectors.register(strategyName, { content: selectorEngineCode })
521-
registeredCustomLocatorStrategies.add(strategyName)
522-
} catch (error) {
523-
if (!error.message.includes('already registered')) {
524-
console.warn(`Failed to register global custom locator strategy '${strategyName}':`, error)
525-
}
526-
}
527-
}
528-
}
529486
} catch (e) {
530487
console.warn(e)
531488
}
@@ -915,10 +872,56 @@ class Playwright extends Helper {
915872
this.debugSection('Url', target.url())
916873
})
917874

875+
// Register custom locator strategies after browser is started
876+
await this._ensureCustomLocatorsRegistered()
877+
918878
this.isRunning = true
919879
return this.browser
920880
}
921881

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+
922925
_lookupCustomLocator(customStrategy) {
923926
if (typeof this.customLocatorStrategies !== 'object' || this.customLocatorStrategies === null) {
924927
return null
@@ -1345,6 +1348,9 @@ class Playwright extends Helper {
13451348
* ```
13461349
*/
13471350
async _locate(locator) {
1351+
// Ensure custom locators are registered before any locator operations
1352+
await this._ensureCustomLocatorsRegistered()
1353+
13481354
const context = await this._getContext()
13491355

13501356
if (this.frame) return findElements(this.frame, locator)
@@ -1376,6 +1382,9 @@ class Playwright extends Helper {
13761382
* ```
13771383
*/
13781384
async _locateElement(locator) {
1385+
// Ensure custom locators are registered before any locator operations
1386+
await this._ensureCustomLocatorsRegistered()
1387+
13791388
const context = await this._getContext()
13801389
return findElement(context, locator)
13811390
}

0 commit comments

Comments
 (0)