Skip to content

Commit 31558c1

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

File tree

2 files changed

+139
-91
lines changed

2 files changed

+139
-91
lines changed

lib/helper/Playwright.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -898,14 +898,17 @@ class Playwright extends Helper {
898898

899899
// Convert the function to a string and create the selector engine content
900900
const functionString = strategyFunction.toString()
901-
const selectorEngine = `
901+
const selectorEngine =
902+
`
902903
exports.query = (root, selector) => {
903904
try {
904905
const strategyFunction = ${functionString};
905906
const result = strategyFunction(selector, root);
906907
return Array.isArray(result) ? result[0] : result;
907908
} catch (error) {
908-
console.warn('Error in custom locator "${strategyName}":', error);
909+
console.warn('Error in custom locator "` +
910+
strategyName +
911+
`":', error);
909912
return null;
910913
}
911914
};
@@ -916,7 +919,9 @@ class Playwright extends Helper {
916919
const result = strategyFunction(selector, root);
917920
return Array.isArray(result) ? result : result ? [result] : [];
918921
} catch (error) {
919-
console.warn('Error in custom locator "${strategyName}":', error);
922+
console.warn('Error in custom locator "` +
923+
strategyName +
924+
`":', error);
920925
return [];
921926
}
922927
};

test/helper/Playwright_test.js

Lines changed: 131 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,117 +1081,160 @@ describe('Playwright', function () {
10811081
I.see('Information')
10821082
})
10831083
})
1084+
})
1085+
1086+
describe('Playwright - Custom Locator Strategies Configuration', () => {
1087+
let customI
10841088

1085-
describe('#customLocatorStrategies', () => {
1086-
let customI
1087-
1088-
before(async () => {
1089-
// Create a new Playwright instance with custom locator strategies
1090-
customI = new Playwright({
1091-
url: siteUrl,
1092-
browser: process.env.BROWSER || 'chromium',
1093-
show: false,
1094-
waitForTimeout: 5000,
1095-
timeout: 2000,
1096-
restart: true,
1097-
chrome: {
1098-
args: ['--no-sandbox', '--disable-setuid-sandbox'],
1089+
before(async () => {
1090+
// Create a new Playwright instance with custom locator strategies
1091+
customI = new Playwright({
1092+
url: siteUrl,
1093+
browser: process.env.BROWSER || 'chromium',
1094+
show: false,
1095+
waitForTimeout: 5000,
1096+
timeout: 2000,
1097+
restart: true,
1098+
chrome: {
1099+
args: ['--no-sandbox', '--disable-setuid-sandbox'],
1100+
},
1101+
customLocatorStrategies: {
1102+
byRole: (selector, root) => {
1103+
return root.querySelector(`[role="${selector}"]`)
10991104
},
1100-
customLocatorStrategies: {
1101-
byRole: (selector, root) => {
1102-
return root.querySelector(`[role="${selector}"]`)
1103-
},
1104-
byTestId: (selector, root) => {
1105-
return root.querySelector(`[data-testid="${selector}"]`)
1106-
},
1107-
byDataQa: (selector, root) => {
1108-
const elements = root.querySelectorAll(`[data-qa="${selector}"]`)
1109-
return Array.from(elements) // Return all matching elements
1110-
},
1105+
byTestId: (selector, root) => {
1106+
return root.querySelector(`[data-testid="${selector}"]`)
11111107
},
1112-
})
1113-
await customI._init()
1114-
// Skip browser initialization for basic config tests
1108+
byDataQa: (selector, root) => {
1109+
const elements = root.querySelectorAll(`[data-qa="${selector}"]`)
1110+
return Array.from(elements) // Return all matching elements
1111+
},
1112+
},
11151113
})
1114+
// Note: Skip _init() for configuration-only tests to avoid browser dependency
1115+
// await customI._init()
1116+
// Skip browser initialization for basic config tests
1117+
})
11161118

1117-
after(async () => {
1118-
if (customI) {
1119-
try {
1120-
await customI._after()
1121-
} catch (e) {
1122-
// Ignore cleanup errors if browser wasn't initialized
1123-
}
1119+
after(async () => {
1120+
if (customI) {
1121+
try {
1122+
await customI._after()
1123+
} catch (e) {
1124+
// Ignore cleanup errors if browser wasn't initialized
11241125
}
1125-
})
1126+
}
1127+
})
11261128

1127-
it('should have custom locator strategies defined', () => {
1128-
expect(customI.customLocatorStrategies).to.not.be.undefined
1129-
expect(customI.customLocatorStrategies.byRole).to.be.a('function')
1130-
expect(customI.customLocatorStrategies.byTestId).to.be.a('function')
1131-
expect(customI.customLocatorStrategies.byDataQa).to.be.a('function')
1132-
})
1129+
it('should have custom locator strategies defined', () => {
1130+
expect(customI.customLocatorStrategies).to.not.be.undefined
1131+
expect(customI.customLocatorStrategies.byRole).to.be.a('function')
1132+
expect(customI.customLocatorStrategies.byTestId).to.be.a('function')
1133+
expect(customI.customLocatorStrategies.byDataQa).to.be.a('function')
1134+
})
11331135

1134-
it('should detect custom locator strategies are defined', () => {
1135-
expect(customI._isCustomLocatorStrategyDefined()).to.be.true
1136-
})
1136+
it('should detect custom locator strategies are defined', () => {
1137+
expect(customI._isCustomLocatorStrategyDefined()).to.be.true
1138+
})
11371139

1138-
it('should lookup custom locator functions', () => {
1139-
const byRoleFunction = customI._lookupCustomLocator('byRole')
1140-
expect(byRoleFunction).to.be.a('function')
1140+
it('should lookup custom locator functions', () => {
1141+
const byRoleFunction = customI._lookupCustomLocator('byRole')
1142+
expect(byRoleFunction).to.be.a('function')
11411143

1142-
const nonExistentFunction = customI._lookupCustomLocator('nonExistent')
1143-
expect(nonExistentFunction).to.be.null
1144-
})
1144+
const nonExistentFunction = customI._lookupCustomLocator('nonExistent')
1145+
expect(nonExistentFunction).to.be.null
1146+
})
11451147

1146-
it('should identify custom locators correctly', () => {
1147-
const customLocator = { byRole: 'button' }
1148-
expect(customI._isCustomLocator(customLocator)).to.be.true
1148+
it('should identify custom locators correctly', () => {
1149+
const customLocator = { byRole: 'button' }
1150+
expect(customI._isCustomLocator(customLocator)).to.be.true
11491151

1150-
const standardLocator = { css: '#test' }
1151-
expect(customI._isCustomLocator(standardLocator)).to.be.false
1152-
})
1152+
const standardLocator = { css: '#test' }
1153+
expect(customI._isCustomLocator(standardLocator)).to.be.false
1154+
})
11531155

1154-
it('should throw error for undefined custom locator strategy', () => {
1155-
const invalidLocator = { nonExistent: 'test' }
1156+
it('should throw error for undefined custom locator strategy', () => {
1157+
const invalidLocator = { nonExistent: 'test' }
11561158

1157-
try {
1158-
customI._isCustomLocator(invalidLocator)
1159-
expect.fail('Should have thrown an error')
1160-
} catch (error) {
1161-
expect(error.message).to.include('Please define "customLocatorStrategies"')
1162-
}
1159+
try {
1160+
customI._isCustomLocator(invalidLocator)
1161+
expect.fail('Should have thrown an error')
1162+
} catch (error) {
1163+
expect(error.message).to.include('Please define "customLocatorStrategies"')
1164+
}
1165+
})
1166+
})
1167+
1168+
describe('Playwright - Custom Locator Strategies Browser Tests', () => {
1169+
let customI
1170+
1171+
before(async () => {
1172+
// Create a new Playwright instance with custom locator strategies
1173+
customI = new Playwright({
1174+
url: siteUrl,
1175+
browser: process.env.BROWSER || 'chromium',
1176+
show: false,
1177+
waitForTimeout: 5000,
1178+
timeout: 2000,
1179+
restart: true,
1180+
chrome: {
1181+
args: ['--no-sandbox', '--disable-setuid-sandbox'],
1182+
},
1183+
customLocatorStrategies: {
1184+
byRole: (selector, root) => {
1185+
return root.querySelector(`[role="${selector}"]`)
1186+
},
1187+
byTestId: (selector, root) => {
1188+
return root.querySelector(`[data-testid="${selector}"]`)
1189+
},
1190+
byDataQa: (selector, root) => {
1191+
const elements = root.querySelectorAll(`[data-qa="${selector}"]`)
1192+
return Array.from(elements) // Return all matching elements
1193+
},
1194+
},
11631195
})
1196+
await customI._init()
1197+
})
11641198

1165-
it('should use custom locator to find elements on page', async function () {
1166-
// Skip if browser can't be initialized
1199+
after(async () => {
1200+
if (customI) {
11671201
try {
1168-
await customI._beforeSuite()
1169-
await customI._before()
1202+
await customI._after()
11701203
} catch (e) {
1171-
this.skip() // Skip if browser not available
1204+
// Ignore cleanup errors if browser wasn't initialized
11721205
}
1206+
}
1207+
})
11731208

1174-
await customI.amOnPage('/form/example1')
1209+
it('should use custom locator to find elements on page', async function () {
1210+
// Skip if browser can't be initialized
1211+
try {
1212+
await customI._beforeSuite()
1213+
await customI._before()
1214+
} catch (e) {
1215+
this.skip() // Skip if browser not available
1216+
}
11751217

1176-
// Test byRole locator - assuming the page has elements with role attributes
1177-
// This test assumes there's a button with role="button" on the form page
1178-
// If the test fails, it means the page doesn't have the expected elements
1179-
// but the custom locator mechanism is working if no errors are thrown
1218+
await customI.amOnPage('/form/example1')
11801219

1181-
try {
1182-
const elements = await customI._locate({ byRole: 'button' })
1183-
// If we get here without error, the custom locator is working
1184-
expect(elements).to.be.an('array')
1185-
} catch (error) {
1186-
// If the error is about element not found, that's ok - means locator works but element doesn't exist
1187-
// If it's about custom locator not being recognized, that's a real failure
1188-
if (error.message.includes('Please define "customLocatorStrategies"')) {
1189-
throw error
1190-
}
1191-
// Element not found is acceptable - means the custom locator is working
1192-
console.log('Custom locator working but element not found (expected):', error.message)
1220+
// Test byRole locator - assuming the page has elements with role attributes
1221+
// This test assumes there's a button with role="button" on the form page
1222+
// If the test fails, it means the page doesn't have the expected elements
1223+
// but the custom locator mechanism is working if no errors are thrown
1224+
1225+
try {
1226+
const elements = await customI._locate({ byRole: 'button' })
1227+
// If we get here without error, the custom locator is working
1228+
expect(elements).to.be.an('array')
1229+
} catch (error) {
1230+
// If the error is about element not found, that's ok - means locator works but element doesn't exist
1231+
// If it's about custom locator not being recognized, that's a real failure
1232+
if (error.message.includes('Please define "customLocatorStrategies"')) {
1233+
throw error
11931234
}
1194-
})
1235+
// Element not found is acceptable - means the custom locator is working
1236+
console.log('Custom locator working but element not found (expected):', error.message)
1237+
}
11951238
})
11961239
})
11971240

0 commit comments

Comments
 (0)