-
-
Notifications
You must be signed in to change notification settings - Fork 750
Expand file tree
/
Copy pathCustomLocator_test.js
More file actions
247 lines (214 loc) · 9.04 KB
/
CustomLocator_test.js
File metadata and controls
247 lines (214 loc) · 9.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import * as chai from 'chai'
import Playwright from '../../lib/helper/Playwright.js'
import Locator from '../../lib/locator.js'
const expect = chai.expect
describe('Custom Locator Strategies', function () {
this.timeout(5000)
let helper
beforeEach(() => {
helper = new Playwright({
url: 'http://localhost',
browser: 'chromium',
customLocatorStrategies: {
byRole: (selector, root) => {
return root.querySelector(`[role="${selector}"]`)
},
byTestId: (selector, root) => {
return root.querySelector(`[data-testid="${selector}"]`)
},
byDataQa: (selector, root) => {
const elements = root.querySelectorAll(`[data-qa="${selector}"]`)
return Array.from(elements)
},
},
})
})
describe('#configuration', () => {
it('should store custom locator strategies in helper', () => {
expect(helper.customLocatorStrategies).to.not.be.undefined
expect(helper.customLocatorStrategies).to.be.an('object')
expect(Object.keys(helper.customLocatorStrategies)).to.have.length(3)
})
it('should have all configured strategies as functions', () => {
expect(helper.customLocatorStrategies.byRole).to.be.a('function')
expect(helper.customLocatorStrategies.byTestId).to.be.a('function')
expect(helper.customLocatorStrategies.byDataQa).to.be.a('function')
})
})
describe('#_isCustomLocatorStrategyDefined', () => {
it('should return true when strategies are defined', () => {
expect(helper._isCustomLocatorStrategyDefined()).to.be.true
})
it('should return false when no strategies are defined', () => {
const emptyHelper = new Playwright({
url: 'http://localhost',
browser: 'chromium',
})
expect(emptyHelper._isCustomLocatorStrategyDefined()).to.be.false
})
it('should return false when strategies is empty object', () => {
const emptyHelper = new Playwright({
url: 'http://localhost',
browser: 'chromium',
customLocatorStrategies: {},
})
expect(emptyHelper._isCustomLocatorStrategyDefined()).to.be.false
})
})
describe('#_lookupCustomLocator', () => {
it('should find existing custom locator function', () => {
const strategy = helper._lookupCustomLocator('byRole')
expect(strategy).to.be.a('function')
})
it('should return null for non-existent strategy', () => {
const strategy = helper._lookupCustomLocator('nonExistent')
expect(strategy).to.be.null
})
it('should return null when customLocatorStrategies is not defined', () => {
const emptyHelper = new Playwright({
url: 'http://localhost',
browser: 'chromium',
})
const strategy = emptyHelper._lookupCustomLocator('byRole')
expect(strategy).to.be.null
})
it('should return null when customLocatorStrategies is null', () => {
const helper = new Playwright({
url: 'http://localhost',
browser: 'chromium',
})
helper.customLocatorStrategies = null
const strategy = helper._lookupCustomLocator('byRole')
expect(strategy).to.be.null
})
it('should return null when strategy exists but is not a function', () => {
const helper = new Playwright({
url: 'http://localhost',
browser: 'chromium',
customLocatorStrategies: {
notAFunction: 'this is a string, not a function',
},
})
const strategy = helper._lookupCustomLocator('notAFunction')
expect(strategy).to.be.null
})
})
describe('#_isCustomLocator', () => {
it('should identify custom locators correctly', () => {
expect(helper._isCustomLocator({ byRole: 'button' })).to.be.true
expect(helper._isCustomLocator({ byTestId: 'submit' })).to.be.true
expect(helper._isCustomLocator({ byDataQa: 'form' })).to.be.true
})
it('should not identify standard locators as custom', () => {
expect(helper._isCustomLocator({ css: '#test' })).to.be.false
expect(helper._isCustomLocator({ xpath: '//div' })).to.be.false
expect(helper._isCustomLocator({ id: 'test' })).to.be.false
expect(helper._isCustomLocator({ name: 'test' })).to.be.false
})
it('should throw error for undefined custom strategy', () => {
expect(() => {
helper._isCustomLocator({ undefinedStrategy: 'test' })
}).to.throw('Please define "customLocatorStrategies"')
})
it('should handle string locators correctly', () => {
expect(helper._isCustomLocator('#test')).to.be.false
expect(helper._isCustomLocator('//div')).to.be.false
})
it('should handle edge case locators', () => {
expect(helper._isCustomLocator(null)).to.be.false
expect(helper._isCustomLocator(undefined)).to.be.false
expect(helper._isCustomLocator({})).to.be.false
})
})
describe('Locator class integration', () => {
it('should identify custom locator types via Locator class', () => {
const customLocator = new Locator({ byRole: 'button' })
expect(customLocator.isCustom()).to.be.true
expect(customLocator.type).to.equal('byRole')
expect(customLocator.value).to.equal('button')
})
it('should not identify standard locator types as custom', () => {
const standardLocator = new Locator({ css: '#test' })
expect(standardLocator.isCustom()).to.be.false
})
it('should handle multiple custom locator strategies in one helper', () => {
const multiHelper = new Playwright({
url: 'http://localhost',
browser: 'chromium',
customLocatorStrategies: {
byRole: (s, r) => r.querySelector(`[role="${s}"]`),
byTestId: (s, r) => r.querySelector(`[data-testid="${s}"]`),
byClass: (s, r) => r.querySelector(`.${s}`),
byAttribute: (s, r) => r.querySelector(`[${s}]`),
byCustom: (s, r) => r.querySelector(s),
},
})
expect(multiHelper._isCustomLocatorStrategyDefined()).to.be.true
expect(Object.keys(multiHelper.customLocatorStrategies)).to.have.length(5)
// Test each strategy
expect(multiHelper._isCustomLocator({ byRole: 'button' })).to.be.true
expect(multiHelper._isCustomLocator({ byTestId: 'test' })).to.be.true
expect(multiHelper._isCustomLocator({ byClass: 'active' })).to.be.true
expect(multiHelper._isCustomLocator({ byAttribute: 'disabled' })).to.be.true
expect(multiHelper._isCustomLocator({ byCustom: '#custom' })).to.be.true
})
})
describe('Error handling', () => {
it('should handle malformed configuration gracefully', () => {
// Test with non-object customLocatorStrategies
const badHelper1 = new Playwright({
url: 'http://localhost',
browser: 'chromium',
customLocatorStrategies: 'not an object',
})
expect(badHelper1._isCustomLocatorStrategyDefined()).to.be.false
expect(badHelper1._lookupCustomLocator('anything')).to.be.null
// Test with null customLocatorStrategies
const badHelper2 = new Playwright({
url: 'http://localhost',
browser: 'chromium',
customLocatorStrategies: null,
})
expect(badHelper2._isCustomLocatorStrategyDefined()).to.be.false
expect(badHelper2._lookupCustomLocator('anything')).to.be.null
})
it('should validate strategy functions correctly', () => {
const mixedHelper = new Playwright({
url: 'http://localhost',
browser: 'chromium',
customLocatorStrategies: {
validStrategy: (s, r) => r.querySelector(s),
invalidStrategy1: 'not a function',
invalidStrategy2: null,
invalidStrategy3: undefined,
invalidStrategy4: {},
invalidStrategy5: [],
},
})
expect(mixedHelper._lookupCustomLocator('validStrategy')).to.be.a('function')
expect(mixedHelper._lookupCustomLocator('invalidStrategy1')).to.be.null
expect(mixedHelper._lookupCustomLocator('invalidStrategy2')).to.be.null
expect(mixedHelper._lookupCustomLocator('invalidStrategy3')).to.be.null
expect(mixedHelper._lookupCustomLocator('invalidStrategy4')).to.be.null
expect(mixedHelper._lookupCustomLocator('invalidStrategy5')).to.be.null
})
})
describe('buildLocatorString integration', () => {
it('should build correct locator strings for custom strategies', () => {
// We can't easily test buildLocatorString directly since it's not exported,
// but we can test that custom locators are properly identified
const locator1 = new Locator({ byRole: 'button' })
const locator2 = new Locator({ byTestId: 'submit' })
const locator3 = new Locator({ byDataQa: 'form-section' })
expect(locator1.isCustom()).to.be.true
expect(locator2.isCustom()).to.be.true
expect(locator3.isCustom()).to.be.true
expect(locator1.type).to.equal('byRole')
expect(locator1.value).to.equal('button')
expect(locator2.type).to.equal('byTestId')
expect(locator2.value).to.equal('submit')
expect(locator3.type).to.equal('byDataQa')
expect(locator3.value).to.equal('form-section')
})
})
})