-
-
Notifications
You must be signed in to change notification settings - Fork 737
/
Copy pathels_test.js
210 lines (173 loc) · 5.55 KB
/
els_test.js
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
const assert = require('assert')
const { expect } = require('chai')
const els = require('../../lib/els')
const recorder = require('../../lib/recorder')
const Container = require('../../lib/container')
const Helper = require('../../lib/helper')
const StepConfig = require('../../lib/step/config')
class TestHelper extends Helper {
constructor() {
super()
this.elements = []
}
async _locate(locator) {
return this.elements
}
}
describe('els', function () {
let helper
beforeEach(() => {
helper = new TestHelper()
Container.clear()
Container.append({
helpers: {
test: helper,
},
})
recorder.reset()
recorder.startUnlessRunning()
})
describe('#element', () => {
it('should execute function on first found element', async () => {
helper.elements = ['el1', 'el2', 'el3']
let elementUsed
await els.element('my test', '.selector', async el => {
elementUsed = await el
})
assert.equal(elementUsed, 'el1')
})
it('should work without purpose parameter', async () => {
helper.elements = ['el1', 'el2']
let elementUsed
await els.element('.selector', async el => {
elementUsed = await el
})
assert.equal(elementUsed, 'el1')
})
it('should throw error when no helper with _locate available', async () => {
Container.clear()
try {
await els.element('.selector', async () => {})
throw new Error('should have thrown error')
} catch (e) {
expect(e.message).to.include('No helper enabled with _locate method')
}
})
it('should fail on timeout if timeout is set', async () => {
helper.elements = ['el1', 'el2']
try {
await els.element(
'.selector',
async () => {
await new Promise(resolve => setTimeout(resolve, 1000))
},
new StepConfig().timeout(0.01),
)
throw new Error('should have thrown error')
} catch (e) {
recorder.catch()
expect(e.message).to.include('was interrupted on timeout 10ms')
}
})
it('should retry until timeout when retries are set', async () => {
helper.elements = ['el1', 'el2']
let attempts = 0
await els.element(
'.selector',
async els => {
attempts++
if (attempts < 2) {
throw new Error('keep retrying')
}
return els.slice(0, attempts)
},
new StepConfig().retry(2),
)
await recorder.promise()
expect(attempts).to.be.at.least(2)
expect(helper.elements).to.deep.equal(['el1', 'el2'])
})
})
describe('#eachElement', () => {
it('should execute function on each element', async () => {
helper.elements = ['el1', 'el2', 'el3']
const usedElements = []
await els.eachElement('.selector', async el => {
usedElements.push(el)
})
assert.deepEqual(usedElements, ['el1', 'el2', 'el3'])
})
it('should provide index as second parameter', async () => {
helper.elements = ['el1', 'el2']
const indices = []
await els.eachElement('.selector', async (el, i) => {
indices.push(i)
})
assert.deepEqual(indices, [0, 1])
})
it('should work without purpose parameter', async () => {
helper.elements = ['el1', 'el2']
const usedElements = []
await els.eachElement('.selector', async el => {
usedElements.push(el)
})
assert.deepEqual(usedElements, ['el1', 'el2'])
})
it('should throw first error if operation fails', async () => {
helper.elements = ['el1', 'el2']
try {
await els.eachElement('.selector', async el => {
throw new Error(`failed on ${el}`)
})
throw new Error('should have thrown error')
} catch (e) {
expect(e.message).to.equal('failed on el1')
}
})
})
describe('#expectElement', () => {
it('should pass when condition is true', async () => {
helper.elements = ['el1']
await els.expectElement('.selector', async () => true)
})
it('should fail when condition is false', async () => {
helper.elements = ['el1']
try {
await els.expectElement('.selector', async () => false)
throw new Error('should have thrown error')
} catch (e) {
expect(e.cliMessage()).to.include('element (.selector)')
}
})
})
describe('#expectAnyElement', () => {
it('should pass when any element matches condition', async () => {
helper.elements = ['el1', 'el2', 'el3']
await els.expectAnyElement('.selector', async el => el === 'el2')
})
it('should fail when no element matches condition', async () => {
helper.elements = ['el1', 'el2']
try {
await els.expectAnyElement('.selector', async () => false)
throw new Error('should have thrown error')
} catch (e) {
expect(e.cliMessage()).to.include('any element of (.selector)')
}
})
})
describe('#expectAllElements', () => {
it('should pass when all elements match condition', async () => {
helper.elements = ['el1', 'el2']
await els.expectAllElements('.selector', async () => true)
})
it('should fail when any element does not match condition', async () => {
helper.elements = ['el1', 'el2', 'el3']
try {
await els.expectAllElements('.selector', async el => el !== 'el2')
throw new Error('should have thrown error')
} catch (e) {
expect(e.cliMessage()).to.include('element #2 of (.selector)')
}
})
})
})