forked from cypress-io/cypress-example-recipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spec.js
124 lines (111 loc) · 3.44 KB
/
spec.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
// @ts-check
/// <reference types="Cypress" />
import { recurse } from 'cypress-recurse'
describe('page reloads', () => {
// ⛔️ THIS WILL NOT WORK
// NOTE: eventually crashes, cannot use "while" loop with async commands
it.skip('crashes when using while loop', () => {
cy.visit('public/index.html')
.then(() => {
let found7 = false
while (!found7) {
// this schedules an infinite number
// of "cy.get..." commands, eventually crashing
// before any of them have a chance to run
// and set found7 to true
cy.get('#result').should('not.be.empty')
.invoke('text').then(parseInt)
.then((number) => {
if (number === 7) {
found7 = true
cy.log('lucky **7**')
} else {
cy.wait(500, { log: false })
cy.reload()
}
})
}
})
})
// ✅ the right way to schedule more commands
// after checking the element on the page
it('until the number 7 appears', () => {
const checkAndReload = () => {
// get the element's text, convert into a number
cy.get('#result').should('not.be.empty')
.invoke('text').then(parseInt)
.then((number) => {
// if the expected number is found
// stop adding any more commands
if (number === 7) {
cy.log('lucky **7**')
} else {
// otherwise insert more Cypress commands
// by calling the function after reload
cy.wait(500, { log: false })
cy.reload()
checkAndReload()
}
})
}
cy.visit('public/index.html')
checkAndReload()
})
it('until the number 7 appears with reload limit', () => {
let reloadCount = 0
const reloadLimit = 100
const checkAndReload = () => {
// get the element's text, convert into a number
cy.get('#result', { log: false })
.invoke({ log: false }, 'text').then(parseInt)
.then((number) => {
// if the expected number is found
// stop adding any more commands
if (number === 7) {
cy.log('lucky **7**')
} else {
// otherwise insert more Cypress commands
// by calling the function after reload
cy.wait(500, { log: false })
reloadCount += 1
cy.log(`reload **${reloadCount} / ${reloadLimit}**`)
if (reloadCount > reloadLimit) {
throw new Error('Reload limit reached')
}
cy.reload({ log: false })
checkAndReload()
}
})
}
cy.visit('public/index.html')
checkAndReload()
})
it('until 7 appears using cypress-recurse', () => {
// see https://github.com/bahmutov/cypress-recurse
cy.visit('public/index.html')
recurse(
() => {
cy.reload()
return cy.get('#result', { log: false })
.invoke({ log: false }, 'text').then(parseInt)
},
(x) => x === 7,
{
timeout: 10000, // try up to 10 seconds
limit: 100, // try up to 100 times
}
)
})
it('still has window.Cypress after reload', () => {
cy.visit('public/index.html')
.should('have.property', 'appHasCypressPresent', true)
cy.reload()
.should('have.property', 'appHasCypressPresent', true)
// reload a couple of times, still should have window.Cypress
cy.reload().reload()
cy.window()
.then((win) => {
expect(win).to.have.property('appHasCypressPresent', true)
})
})
})