/
index.js
224 lines (204 loc) · 6.54 KB
/
index.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
const debug = require('debug')('cypress-if')
function skipRestOfTheChain(cmd, chainerId) {
while (
cmd &&
cmd.attributes.chainerId === chainerId &&
cmd.attributes.name !== 'finally'
) {
debug('skipping "%s"', cmd.attributes.name)
cmd.attributes.skip = true
cmd = cmd.attributes.next
}
}
function findMyIfSubject(elseCommandAttributes) {
if (!elseCommandAttributes) {
return
}
if (elseCommandAttributes.name === 'if') {
return elseCommandAttributes.ifSubject
}
if (
!elseCommandAttributes.skip &&
!Cypress._.isNil(elseCommandAttributes.subject)
) {
return elseCommandAttributes.subject
}
if (elseCommandAttributes.prev) {
return findMyIfSubject(elseCommandAttributes.prev.attributes)
}
}
function getCypressCurrentSubject() {
if (typeof cy.currentSubject === 'function') {
return cy.currentSubject()
}
// fallback for Cypress v9 and some early v10 versions
return cy.state('subject')
}
Cypress.Commands.add(
'if',
{ prevSubject: true },
function (subject, assertion, assertionValue) {
const cmd = cy.state('current')
debug('if', cmd.attributes, 'subject', subject, 'assertion?', assertion)
debug('next command', cmd.next)
debug('if() current subject', getCypressCurrentSubject())
// console.log('subjects', cy.state('subjects'))
// keep the subject, if there is an "else" branch
// it can look it up to use
cmd.attributes.ifSubject = subject
const hasSubject = Boolean(subject)
let assertionsPassed = true
if (hasSubject && assertion) {
try {
if (assertion.startsWith('not') || assertion.startsWith('have')) {
const parts = assertion.split('.')
let assertionReduced = expect(subject).to
parts.forEach((assertionPart, k) => {
if (
k === parts.length - 1 &&
typeof assertionValue !== 'undefined'
) {
assertionReduced = assertionReduced[assertionPart](assertionValue)
} else {
assertionReduced = assertionReduced[assertionPart]
}
})
} else {
if (typeof assertionValue !== 'undefined') {
expect(subject).to.be[assertion](assertionValue)
} else {
expect(subject).to.be[assertion]
}
}
} catch (e) {
console.error(e)
assertionsPassed = false
}
}
const chainerId = cmd.attributes.chainerId
if (!chainerId) {
throw new Error('Command is missing chainer id')
}
if (!hasSubject || !assertionsPassed) {
let nextCommand = cmd.attributes.next
while (nextCommand && nextCommand.attributes.chainerId === chainerId) {
debug(
'skipping the next "%s" command "%s"',
nextCommand.attributes.type,
nextCommand.attributes.name,
)
// cy.log(`**skipping ${cmd.attributes.next.attributes.name}**`)
if (nextCommand.attributes.name === 'else') {
debug('else branch starts right away')
nextCommand = null
} else {
debug('am skipping "%s"', nextCommand.attributes.name)
debug(nextCommand.attributes)
nextCommand.attributes.skip = true
nextCommand = nextCommand.attributes.next
if (nextCommand && nextCommand.attributes.name === 'else') {
debug('stop skipping command on "else" command')
nextCommand = null
}
}
}
if (subject) {
debug('wrapping subject', subject)
cy.wrap(subject, { log: false })
}
return
} else {
// skip possible "else" branch
debug('skipping a possible "else" branch')
let nextCommand = cmd.attributes.next
while (nextCommand && nextCommand.attributes.chainerId === chainerId) {
debug(
'next command "%s" type "%s"',
nextCommand.attributes.name,
nextCommand.attributes.type,
nextCommand.attributes,
)
if (nextCommand.attributes.name === 'else') {
// found the "else" command, start skipping
debug('found the "else" branch command start')
skipRestOfTheChain(nextCommand, chainerId)
nextCommand = null
} else {
nextCommand = nextCommand.attributes.next
}
}
}
return subject
},
)
Cypress.Commands.add('else', { prevSubject: true }, (subject) => {
debug('else command, subject', subject)
if (typeof subject === 'undefined') {
// find the subject from the "if()" before
subject = findMyIfSubject(cy.state('current').attributes)
}
if (subject) {
cy.wrap(subject, { log: false })
}
})
Cypress.Commands.add('finally', { prevSubject: true }, (subject) => {
debug('finally with the subject', subject)
// notice: cy.log yields "null" 🤯
// https://github.com/cypress-io/cypress/issues/23400
if (typeof subject === 'undefined' || subject === null) {
// find the subject from the "if()" before
const currentCommand = cy.state('current').attributes
debug('current command is finally', currentCommand)
subject = findMyIfSubject(currentCommand)
debug('found subject', subject)
}
if (subject) {
cy.wrap(subject, { log: false })
}
})
Cypress.Commands.overwrite('get', function (get, selector, options) {
// can we see the next command already?
const cmd = cy.state('current')
debug(cmd)
const next = cmd.attributes.next
if (next && next.attributes.name === 'if') {
// disable the built-in assertion
return get(selector, options).then(
(getResult) => {
debug('internal get result', getResult)
return getResult
},
(noResult) => {
debug('no get result', noResult)
},
)
}
return get(selector, options)
})
Cypress.Commands.overwrite(
'contains',
function (contains, prevSubject, selector, text, options) {
debug('contains arguments number', arguments.length)
if (arguments.length === 3) {
text = selector
selector = undefined
}
debug('contains args', { prevSubject, selector, text, options })
const cmd = cy.state('current')
debug(cmd)
const next = cmd.attributes.next
if (next && next.attributes.name === 'if') {
// disable the built-in assertion
return contains(prevSubject, selector, text, options).then(
(getResult) => {
debug('internal contains result', getResult)
return getResult
},
(noResult) => {
debug('no contains result', noResult)
},
)
}
return contains(prevSubject, selector, text, options)
},
)