Skip to content

Commit 5d05e62

Browse files
Copilotkobenguyent
andcommitted
Changes before error encountered
Co-authored-by: kobenguyent <[email protected]>
1 parent 81eb97d commit 5d05e62

File tree

2 files changed

+22
-18
lines changed

2 files changed

+22
-18
lines changed

lib/helper/Playwright.js

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -896,30 +896,33 @@ class Playwright extends Helper {
896896
try {
897897
this.debugSection('Playwright', `registering custom locator strategy: ${strategyName}`)
898898

899-
// Create a selector engine for this custom strategy
900-
const engine = {
901-
query(root, selector) {
899+
// Convert the function to a string and create the selector engine content
900+
const functionString = strategyFunction.toString()
901+
const selectorEngine = `
902+
exports.query = (root, selector) => {
902903
try {
903-
const result = strategyFunction(selector, root)
904-
return Array.isArray(result) ? result[0] : result
904+
const strategyFunction = ${functionString};
905+
const result = strategyFunction(selector, root);
906+
return Array.isArray(result) ? result[0] : result;
905907
} catch (error) {
906-
console.warn(`Error in custom locator '${strategyName}':`, error)
907-
return null
908+
console.warn('Error in custom locator "${strategyName}":', error);
909+
return null;
908910
}
909-
},
910-
911-
queryAll(root, selector) {
911+
};
912+
913+
exports.queryAll = (root, selector) => {
912914
try {
913-
const result = strategyFunction(selector, root)
914-
return Array.isArray(result) ? result : result ? [result] : []
915+
const strategyFunction = ${functionString};
916+
const result = strategyFunction(selector, root);
917+
return Array.isArray(result) ? result : result ? [result] : [];
915918
} catch (error) {
916-
console.warn(`Error in custom locator '${strategyName}':`, error)
917-
return []
919+
console.warn('Error in custom locator "${strategyName}":', error);
920+
return [];
918921
}
919-
},
920-
}
922+
};
923+
`
921924

922-
await playwright.selectors.register(strategyName, engine)
925+
await playwright.selectors.register(strategyName, { content: selectorEngine })
923926
} catch (error) {
924927
console.warn(`Failed to register custom locator strategy '${strategyName}':`, error)
925928
}

test/acceptance/codecept.Playwright.CustomLocators.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ const { config } = require('../acceptance/codecept.Playwright')
33
// Extend the base Playwright configuration to add custom locator strategies
44
const customLocatorConfig = {
55
...config,
6+
grep: null, // Remove grep filter to run custom locator tests
67
helpers: {
78
...config.helpers,
89
Playwright: {
910
...config.helpers.Playwright,
10-
browser: process.env.BROWSER || 'chrome',
11+
browser: process.env.BROWSER || 'chromium',
1112
customLocatorStrategies: {
1213
byRole: (selector, root) => {
1314
return root.querySelector(`[role="${selector}"]`)

0 commit comments

Comments
 (0)