/
retries.ui.cy.ts
358 lines (301 loc) · 11.8 KB
/
retries.ui.cy.ts
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
import { loadSpec } from './support/spec-loader'
import { snapshotReporter } from './support/snapshot-reporter'
// Returns wrapped attempt tag found within runnable containing selector
const getAttemptTag = (sel: string) => {
return cy.get(`.test.runnable:contains(${sel}) .attempt-tag`)
}
describe('runner/cypress retries.ui.spec', {
viewportWidth: 1024,
viewportHeight: 900,
numTestsKeptInMemory: 1,
}, () => {
afterEach(() => {
// @ts-ignore
if (cy.state('test').state === 'passed') {
snapshotReporter()
}
})
it('collapses tests that retry and pass', () => {
loadSpec({
filePath: 'retries/collapse-after-pass.retries.cy.js',
passCount: 2,
failCount: 0,
})
cy.get('.test').should('have.length', 2)
})
it('collapses prev attempts and keeps final one open on failure', () => {
loadSpec({
filePath: 'retries/collapse-prev-attempts.retries.cy.js',
passCount: 1,
failCount: 1,
})
cy.get('.runnable-err-print').should('be.visible')
})
it('can toggle failed prev attempt open and log its error', { viewportHeight: 1200 }, () => {
loadSpec({
filePath: 'retries/all-retry-one-failure.retries.cy.js',
passCount: 2,
failCount: 1,
})
cy.window().then((win) => {
cy.spy(win.console, 'error').as('console_error')
})
cy.contains('Attempt 1')
.click()
.closest('.attempt-item')
.find('.runnable-err-print')
.click()
cy.get('@console_error').should('be.calledWithMatch', 'AssertionError: test 2')
})
// TODO: Determine how best to access the inner Cypress instance prior to test execution
// spy on its actions during the test run (https://cypress-io.atlassian.net/browse/UNIFY-1153)
it.skip('opens attempt on each attempt failure for the screenshot, and closes after test passes', () => {
let stub
const cyReject = (fn) => {
return () => {
try {
fn()
} catch (e) {
// @ts-ignore
cy.fail(e)
}
}
}
loadSpec({
filePath: 'retries/opens-attempt-for-screenshot.retries.cy.js',
passCount: 3,
failCount: 0,
setup: () => {
let attempt = 0
stub = cy.stub().callsFake(cyReject(() => {
attempt++
expect(cy.$$('.attempt-item > .is-open').length).to.equal(attempt)
}))
cy.window().then((win) => {
// @ts-ignore
win.Cypress.Screenshot.onAfterScreenshot = stub
})
},
})
cy.get('.test.runnable:contains(t2)').then(($el) => {
expect($el).not.class('is-open')
expect(stub).callCount(3)
})
})
it('includes routes, spies, hooks, and commands in attempt', () => {
const attemptTag = (sel) => `.attempt-tag:contains(Attempt ${sel})`
loadSpec({
filePath: 'retries/includes-all-in-attempt.retries.cy.js',
passCount: 1,
failCount: 0,
})
cy.get(attemptTag(1)).click().parentsUntil('.collapsible').last().parent().within(() => {
cy.get('.instruments-container').should('contain', 'Spies / Stubs (1)')
cy.get('.instruments-container').should('contain', 'Routes (1)')
cy.get('.runnable-err').should('contain', 'AssertionError')
})
cy.get(attemptTag(2)).click().parentsUntil('.collapsible').last().parent().within(() => {
cy.get('.instruments-container').should('contain', 'Spies / Stubs (2)')
cy.get('.instruments-container').should('contain', 'Routes (2)')
cy.get('.runnable-err').should('contain', 'AssertionError')
})
cy.get(attemptTag(3)).parentsUntil('.collapsible').last().parent().within(() => {
cy.get('.instruments-container').should('contain', 'Spies / Stubs (2)')
cy.get('.instruments-container').should('contain', 'Routes (2)')
cy.get('.runnable-err').should('not.exist')
})
})
describe('only', () => {
it('test retry with [only]', () => {
loadSpec({
filePath: 'retries/only.retries.cy.js',
passCount: 1,
failCount: 0,
})
cy.contains('test 2')
cy.contains('test 1').should('not.exist')
cy.contains('test 3').should('not.exist')
})
})
describe('beforeAll', () => {
it('tests do not retry when beforeAll fails', () => {
loadSpec({
filePath: 'retries/before-all-failure.retries.cy.js',
passCount: 0,
failCount: 1,
})
cy.contains('Although you have test retries')
})
it('before all hooks are not run on the second attempt when fails outside of beforeAll', () => {
loadSpec({
filePath: 'retries/before-all-called-once.retries.cy.js',
passCount: 1,
failCount: 0,
})
cy.contains('test')
cy.contains('after all')
cy.contains('before all').should('not.exist')
})
})
describe('beforeEach', () => {
it('beforeEach hooks retry on failure, but only run same-level afterEach hooks', { viewportHeight: 1550 }, () => {
loadSpec({
filePath: 'retries/before-each-failure.retries.cy.js',
passCount: 1,
failCount: 0,
})
cy.contains('Attempt 1').click()
cy.get('.attempt-1 .hook-item .collapsible:contains(before each)').find('.command-state-failed')
cy.get('.attempt-1 .hook-item .collapsible:contains(before each (2))').should('not.exist')
cy.get('.attempt-1 .hook-item .collapsible:contains(test body)').should('not.exist')
cy.get('.attempt-1 .hook-item .collapsible:contains(after each)').should('not.exist')
cy.get('.attempt-1 .hook-item .collapsible:contains(after all)').should('not.exist')
cy.contains('Attempt 2').click()
cy.get('.attempt-2 .hook-item .collapsible:contains(before each)')
cy.get('.attempt-2 .hook-item .collapsible:contains(before each (2))')
cy.get('.attempt-2 .hook-item .collapsible:contains(before each (3))').find('.command-state-failed')
cy.get('.attempt-2 .hook-item .collapsible:contains(test body)').should('not.exist')
cy.get('.attempt-2 .hook-item .collapsible:contains(after each)')
cy.get('.attempt-2 .hook-item .collapsible:contains(after all)').should('not.exist')
cy.get('.attempt-3 .hook-item .collapsible:contains(before each)')
cy.get('.attempt-3 .hook-item .collapsible:contains(before each (2))')
cy.get('.attempt-3 .hook-item .collapsible:contains(before each (3))')
cy.get('.attempt-3 .hook-item .collapsible:contains(before each (4))')
cy.get('.attempt-3 .hook-item .collapsible:contains(test body)')
cy.get('.attempt-3 .hook-item .collapsible:contains(after each)')
cy.get('.attempt-3 .hook-item .collapsible:contains(after all)')
})
it('beforeEach retried tests skip remaining tests in suite', () => {
loadSpec({
filePath: 'retries/before-each-skip.retries.cy.js',
passCount: 0,
failCount: 1,
pendingCount: 0,
})
// ensure the page is loaded before taking snapshot
cy.contains('skips this')
})
})
describe('afterEach', () => {
it('afterEach hooks retry on failure, but only run higher-level afterEach hooks', () => {
const shouldBeOpen = ($el) => cy.wrap($el).parentsUntil('.collapsible').last().parent().should('have.class', 'is-open')
loadSpec({
filePath: 'retries/after-each-failure.retries.cy.js',
passCount: 1,
failCount: 0,
})
cy.contains('Attempt 1')
.click()
.then(shouldBeOpen)
cy.get('.attempt-1 .hook-item .collapsible:contains(after each (1))').find('.command-state-failed')
cy.get('.attempt-1 .hook-item .collapsible:contains(after each (2))')
cy.get('.attempt-1 .hook-item .collapsible:contains(after each (3))').should('not.exist')
cy.get('.attempt-1 .hook-item .collapsible:contains(after all)').should('not.exist')
cy.contains('Attempt 2').click()
.then(shouldBeOpen)
cy.get('.attempt-2 .hook-item .collapsible:contains(after each (1))')
cy.get('.attempt-2 .hook-item .collapsible:contains(after each (2))')
cy.get('.attempt-2 .hook-item .collapsible:contains(after each (3))').find('.command-state-failed')
cy.get('.attempt-2 .hook-item .collapsible:contains(after all)').should('not.exist')
cy.get('.attempt-tag').should('have.length', 3)
cy.get('.attempt-2 .hook-item .collapsible:contains(after each (1))')
cy.get('.attempt-2 .hook-item .collapsible:contains(after each (2))')
cy.get('.attempt-2 .hook-item .collapsible:contains(after each (3))')
cy.get('.attempt-3 .hook-item .collapsible:contains(after all)')
})
it('afterEach retried tests skip remaining tests in suite', () => {
loadSpec({
filePath: 'retries/after-each-skip.retries.cy.js',
passCount: 0,
failCount: 1,
pendingCount: 0,
})
})
})
describe('afterAll', () => {
it('only run afterAll hook on last attempt', () => {
loadSpec({
filePath: 'retries/after-all-once.retries.cy.js',
passCount: 3,
failCount: 0,
})
cy.contains('test 3').click()
getAttemptTag('test 3').first().click()
cy.contains('.attempt-1', 'after all').should('not.exist')
cy.contains('.attempt-2', 'after all')
})
it('tests do not retry when afterAll fails', () => {
loadSpec({
filePath: 'retries/after-all-failure.retries.cy.js',
passCount: 0,
failCount: 1,
})
cy.window().then((win) => {
cy.spy(win.console, 'error').as('console_error')
})
cy.contains('Although you have test retries')
cy.get('.runnable-err-print').click()
cy.get('@console_error').its('lastCall').should('be.calledWithMatch', 'Error')
})
})
describe('can configure retries', () => {
after(() => {
// @ts-ignore
window.top.__cySkipValidateConfig = false
})
const haveCorrectError = ($el) => {
return (
cy.wrap($el).last().parentsUntil('.collapsible').last().parent()
.find('.runnable-err').should('contain', 'Unspecified AssertionError')
)
}
const containText = (text) => {
return (($el) => {
expect($el[0]).property('innerText').contain(text)
})
}
// @ts-ignore
window.top.__cySkipValidateConfig = true
it('via config value', () => {
loadSpec({
filePath: 'retries/configure-retries.retries.cy.js',
passCount: 0,
failCount: 7,
})
getAttemptTag('[no retry]').should('have.length', 1).then(haveCorrectError)
getAttemptTag('[1 retry]').should('have.length', 2).then(haveCorrectError)
getAttemptTag('[2 retries]').should('have.length', 3).then(haveCorrectError)
getAttemptTag('[open mode, no retry]').should('have.length', 1).then(haveCorrectError)
getAttemptTag('[run mode, retry]').should('have.length', 2).then(haveCorrectError)
getAttemptTag('[open mode, 2 retries]').should('have.length', 3).then(haveCorrectError)
getAttemptTag('[set retries on suite]').should('have.length', 2).then(haveCorrectError)
})
it('throws when set via this.retries in test', () => {
loadSpec({
filePath: 'retries/configure-in-test.retries.cy.js',
passCount: 0,
failCount: 1,
})
cy.get('.runnable-err')
.should(containText(`it('tries to set mocha retries', { retries: 2 }, () => `))
})
it('throws when set via this.retries in hook', () => {
loadSpec({
filePath: 'retries/configure-in-hook.retries.cy.js',
passCount: 0,
failCount: 1,
})
cy.get('.runnable-err')
.should(containText(`describe('suite 1', { retries: 0 }, () => `))
})
it('throws when set via this.retries in suite', () => {
loadSpec({
filePath: 'retries/configure-in-suite.retries.cy.js',
passCount: 0,
failCount: 1,
})
cy.get('.runnable-err')
.should(containText(`describe('suite 1', { retries: 3 }, () => `))
})
})
})