-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
Copy pathquerying.ts
397 lines (319 loc) · 11.9 KB
/
querying.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
import _ from 'lodash'
import $dom from '../../../dom'
import $elements from '../../../dom/elements'
import $errUtils from '../../../cypress/error_utils'
import $utils from '../../../cypress/utils'
import type { Log } from '../../../cypress/log'
import { resolveShadowDomInclusion } from '../../../cypress/shadow_dom_utils'
import { getAliasedRequests, isDynamicAliasingPossible } from '../../net-stubbing/aliasing'
import { aliasRe, aliasIndexRe } from '../../aliases'
type GetOptions = Partial<Cypress.Loggable & Cypress.Timeoutable & Cypress.Withinable & Cypress.Shadow & {
_log?: Log
}>
type ContainsOptions = Partial<Cypress.Loggable & Cypress.Timeoutable & Cypress.CaseMatchable & Cypress.Shadow>
type ShadowOptions = Partial<Cypress.Loggable & Cypress.Timeoutable>
function getAlias (selector, log, cy) {
const alias = selector.slice(1)
return () => {
let toSelect
// Aliases come in two types: raw names, or names followed by an index.
// For example, "@foo.bar" or "@foo.bar.1" or "@foo.bar.all", where 1 or all are the index.
if ((cy.state('aliases') || {})[alias]) {
toSelect = selector
} else {
// If the name isn't in our state, then this is probably a dynamic alias -
// which is to say, it includes an index.
toSelect = selector.replace(/\.(\d+|all)$/, '')
}
let aliasObj
try {
aliasObj = cy.getAlias(toSelect)
} catch (err) {
// possibly this is a dynamic alias, check to see if there is a request
const requests = getAliasedRequests(alias, cy.state)
if (!isDynamicAliasingPossible(cy.state) || !requests.length) {
err.retry = false
throw err
}
aliasObj = {
alias,
command: cy.state('routes')[requests[0].routeId].command,
}
}
if (!aliasObj) {
return
}
const { command } = aliasObj
log && cy.state('current') === this && log.set('referencesAlias', { name: alias })
/*
* There are two cases for aliases, each explained in more detail below:
* 1. Intercept aliases
* 2. Subject aliases (either DOM elements or primitives).
*/
if (command.get('name') === 'intercept') {
// Intercept aliases are fairly similar, but `getAliasedRequests` does *not* handle indexes
// and we have to do it ourselves here.
const requests = getAliasedRequests(aliasObj.alias, cy.state)
// If the user provides an index ("@foo.1" or "@foo.all"), use that. Otherwise, return the most recent request.
const match = selector.match(aliasIndexRe)
if (match && match[1] === '0') {
$errUtils.throwErrByPath('get.alias_zero', {
args: { alias: aliasObj.alias },
})
}
const index = match ? match[1] : requests.length
const returnValue = index === 'all' ? requests : (requests[parseInt(index, 10) - 1] || null)
log && cy.state('current') === this && log.set({
aliasType: 'intercept',
consoleProps: () => {
return {
Alias: selector,
Yielded: returnValue,
}
},
})
return returnValue
}
// If we've fallen through to here, then this is a subject alias - the result of one or more previous
// cypress commands. We replay their subject chain (including possibly re-quering the DOM)
// and use this as the result of the alias.
// If we have a test similar to
// cy.get('li').as('alias').then(li => li.remove())
// cy.get('@alias').should('not.exist')
// then Cypress can be very confused: the original 'get' command was not followed by 'should not exist'
// but when reinvoked, it is! We therefore set a special piece of state,
// which the 'should exist' assertion can read to determine if the *current* command is followed by a 'should not
// exist' assertion.
cy.state('aliasCurrentCommand', this)
const subject = cy.getSubjectFromChain(aliasObj.subjectChain)
cy.state('aliasCurrentCommand', undefined)
if ($dom.isElement(subject)) {
log && cy.state('current') === this && log.set({
aliasType: 'dom',
consoleProps: () => {
return {
Alias: selector,
Yielded: $dom.getElements(subject),
Elements: (subject as JQuery<HTMLElement>).length,
}
},
})
} else {
log && cy.state('current') === this && log.set({
aliasType: 'primitive',
consoleProps: () => {
return {
Alias: selector,
Yielded: subject,
}
},
})
}
return subject
}
}
export default (Commands, Cypress, cy, state) => {
Commands.addQuery('get', function get (selector, userOptions: GetOptions = {}) {
if ((userOptions === null) || _.isArray(userOptions) || !_.isPlainObject(userOptions)) {
$errUtils.throwErrByPath('get.invalid_options', {
args: { options: userOptions },
})
}
const log = userOptions.log !== false && (userOptions._log || Cypress.log({
message: selector,
type: 'parent',
timeout: userOptions.timeout,
consoleProps: () => ({}),
}))
this.set('timeout', userOptions.timeout)
this.set('_log', log)
if (aliasRe.test(selector)) {
return getAlias.call(this, selector, log, cy)
}
const withinSubject = cy.state('withinSubjectChain')
const includeShadowDom = resolveShadowDomInclusion(Cypress, userOptions.includeShadowDom)
return () => {
Cypress.ensure.commandCanCommunicateWithAUT(cy)
let $el
try {
let scope = userOptions.withinSubject !== undefined ? userOptions.withinSubject : cy.getSubjectFromChain(withinSubject)
if (includeShadowDom) {
const root = scope?.get(0) || cy.state('document')
const elementsWithShadow = $dom.findAllShadowRoots(root)
scope = elementsWithShadow.concat(root)
}
$el = cy.$$(selector, scope)
// jQuery v3 has removed its deprecated properties like ".selector"
// https://jquery.com/upgrade-guide/3.0/breaking-change-deprecated-context-and-selector-properties-removed
// but our error messages use this property to actually show the missing element
// so let's put it back
if ($el.selector == null) {
$el.selector = selector
}
} catch (err: any) {
if (err.message.startsWith('Syntax error')) {
err.retry = false
}
// this is usually a sizzle error (invalid selector)
if (log) {
err.onFail = () => log.error(err)
}
throw err
}
log && cy.state('current') === this && log.set({
$el,
consoleProps: () => {
return {
Selector: selector,
Yielded: $dom.getElements($el),
Elements: $el.length,
}
},
})
return $el
}
})
Commands.addQuery('contains', function contains (filter, text, userOptions: ContainsOptions = {}) {
if (_.isRegExp(text)) {
// .contains(filter, text)
// Do nothing
} else if (_.isObject(text)) {
// .contains(text, userOptions)
userOptions = text
text = filter
filter = ''
} else if (_.isUndefined(text)) {
// .contains(text)
text = filter
filter = ''
}
// https://github.com/cypress-io/cypress/issues/1119
if (text === 0) {
// text can be 0 but should not be falsy
text = '0'
}
if (userOptions.matchCase === true && _.isRegExp(text) && text.flags.includes('i')) {
$errUtils.throwErrByPath('contains.regex_conflict')
}
if (!(_.isString(text) || _.isFinite(text) || _.isRegExp(text))) {
$errUtils.throwErrByPath('contains.invalid_argument')
}
if (_.isBlank(text)) {
$errUtils.throwErrByPath('contains.empty_string')
}
// find elements by the :cy-contains psuedo selector
// and any submit inputs with the attributeContainsWord selector
const selector = $dom.getContainsSelector(text, filter, { matchCase: true, ...userOptions })
const log = userOptions.log !== false && Cypress.log({
message: $utils.stringify(_.compact([filter, text])),
type: this.hasPreviouslyLinkedCommand ? 'child' : 'parent',
timeout: userOptions.timeout,
consoleProps: () => ({}),
})
const getOptions = _.extend({ _log: log }, userOptions) as GetOptions
const getFn = cy.now('get', selector, getOptions)
const getPhrase = () => {
if (filter && !(cy.$$(getOptions.withinSubject) as JQuery<HTMLElement>).is('body')) {
const node = $dom.stringify(getOptions.withinSubject, 'short')
return `within the element: ${node} and with the selector: '${filter}' `
}
if (filter) {
return `within the selector: '${filter}' `
}
if (!(cy.$$(getOptions.withinSubject) as JQuery<HTMLElement>).is('body')) {
const node = $dom.stringify(getOptions.withinSubject, 'short')
return `within the element: ${node} `
}
return ''
}
this.set('timeout', userOptions.timeout)
this.set('onFail', (err) => {
switch (err.type) {
case 'length':
if (err.expected > 1) {
const { message, docsUrl } = $errUtils.cypressErrByPath('contains.length_option')
err.message = message
err.docsUrl = docsUrl
err.retry = false
}
break
case 'existence':
if (err.negated) {
err.message = `Expected not to find content: '${text}' ${getPhrase()}but continuously found it.`
} else {
err.message = `Expected to find content: '${text}' ${getPhrase()}but never did.`
}
break
default:
break
}
})
const withinSubject = cy.state('withinSubjectChain')
return (subject) => {
Cypress.ensure.isType(subject, ['optional', 'element', 'window', 'document'], this.get('name'), cy)
if (!subject || (!$dom.isElement(subject) && !$elements.isShadowRoot(subject[0]))) {
subject = cy.getSubjectFromChain(withinSubject || [cy.$$('body')])
}
let $el = cy.$$()
subject.each((index, element) => {
getOptions.withinSubject = cy.$$(element)
$el = $el.add(getFn())
})
if (!$el.length) {
// .get() looks for elements *inside* the current subject, while contains() wants to also match the current
// subject itself if no child matches.
$el = (subject as JQuery).filter(selector)
}
$el = $dom.getFirstDeepestElement($el)
log && cy.state('current') === this && log.set({
$el,
consoleProps: () => {
return {
Content: text,
'Applied To': $dom.getElements(subject),
Yielded: $el.get(0),
Elements: $el.length,
}
},
})
return $el
}
})
Commands.addQuery('shadow', function contains (userOptions: ShadowOptions = {}) {
const log = userOptions.log !== false && Cypress.log({
timeout: userOptions.timeout,
consoleProps: () => ({}),
})
this.set('timeout', userOptions.timeout)
this.set('onFail', (err) => {
switch (err.type) {
case 'existence': {
const { message, docsUrl } = $errUtils.cypressErrByPath('shadow.no_shadow_root')
err.message = message
err.docsUrl = docsUrl
break
}
default:
break
}
})
return (subject) => {
Cypress.ensure.isType(subject, 'element', 'shadow', cy)
// find all shadow roots of the subject(s), if any exist
const $el = subject
.map((i, node) => node.shadowRoot)
.filter((i, node) => node !== undefined && node !== null)
log && cy.state('current') === this && log.set({
$el,
consoleProps: () => {
return {
'Applied To': $dom.getElements(subject),
Yielded: $dom.getElements($el),
Elements: $el?.length,
}
},
})
return $el
}
})
}