-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
querying.ts
574 lines (455 loc) · 16.1 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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
import _ from 'lodash'
import Promise from 'bluebird'
import $dom from '../../../dom'
import $elements from '../../../dom/elements'
import $errUtils from '../../../cypress/error_utils'
import { resolveShadowDomInclusion } from '../../../cypress/shadow_dom_utils'
import { getAliasedRequests, isDynamicAliasingPossible } from '../../net-stubbing/aliasing'
interface InternalGetOptions extends Partial<Cypress.Loggable & Cypress.Timeoutable & Cypress.Withinable & Cypress.Shadow> {
_log?: any
_retries?: number
filter?: any
onRetry?: Function
verify?: boolean
}
interface InternalContainsOptions extends Partial<Cypress.Loggable & Cypress.Timeoutable & Cypress.CaseMatchable & Cypress.Shadow> {
_log?: any
}
export default (Commands, Cypress, cy, state) => {
Commands.addAll({
get (selector, userOptions: Partial<Cypress.Loggable & Cypress.Timeoutable & Cypress.Withinable & Cypress.Shadow> = {}) {
const ctx = this
if ((userOptions === null) || _.isArray(userOptions) || !_.isPlainObject(userOptions)) {
return $errUtils.throwErrByPath('get.invalid_options', {
args: { options: userOptions },
})
}
const options: InternalGetOptions = _.defaults({}, userOptions, {
retry: true,
withinSubject: state('withinSubject'),
log: true,
command: null,
verify: true,
})
options.includeShadowDom = resolveShadowDomInclusion(Cypress, options.includeShadowDom)
let aliasObj
const consoleProps: Record<string, any> = {}
const start = (aliasType) => {
if (options.log === false) {
return
}
if (options._log == null) {
options._log = Cypress.log({
message: selector,
referencesAlias: (aliasObj != null && aliasObj.alias) ? { name: aliasObj.alias } : undefined,
aliasType,
timeout: options.timeout,
consoleProps: () => {
return consoleProps
},
})
}
}
const log = (value, aliasType = 'dom') => {
if (options.log === false) {
return
}
if (!_.isObject(options._log)) {
start(aliasType)
}
const obj: any = {}
if (aliasType === 'dom') {
_.extend(obj, {
$el: value,
numRetries: options._retries,
})
}
obj.consoleProps = () => {
const key = aliasObj ? 'Alias' : 'Selector'
consoleProps[key] = selector
switch (aliasType) {
case 'dom':
_.extend(consoleProps, {
Yielded: $dom.getElements(value),
Elements: (value != null ? value.length : undefined),
})
break
case 'primitive':
_.extend(consoleProps, {
Yielded: value,
})
break
case 'route':
_.extend(consoleProps, {
Yielded: value,
})
break
default:
break
}
return consoleProps
}
options._log.set(obj)
}
let allParts
let toSelect
// We want to strip everything after the last '.'
// only when it is potentially a number or 'all'
if ((_.indexOf(selector, '.') === -1) ||
(_.keys(state('aliases')).includes(selector.slice(1)))) {
toSelect = selector
} else {
allParts = _.split(selector, '.')
toSelect = _.join(_.dropRight(allParts, 1), '.')
}
try {
aliasObj = cy.getAlias(toSelect)
} catch (err) {
// possibly this is a dynamic alias, check to see if there is a request
const alias = toSelect.slice(1)
const [request] = getAliasedRequests(alias, state)
if (!isDynamicAliasingPossible(state) || !request) {
throw err
}
aliasObj = {
alias,
command: state('routes')[request.routeId].command,
}
}
if (!aliasObj && isDynamicAliasingPossible(state)) {
const requests = getAliasedRequests(toSelect, state)
if (requests.length) {
aliasObj = {
alias: toSelect,
command: state('routes')[requests[0].routeId].command,
}
}
}
if (aliasObj) {
let { subject, alias, command } = aliasObj
const resolveAlias = () => {
// if this is a DOM element
if ($dom.isElement(subject)) {
let replayFrom = false
const replay = () => {
cy.replayCommandsFrom(command)
// its important to return undefined
// here else we trick cypress into thinking
// we have a promise violation
return undefined
}
// if we're missing any element
// within our subject then filter out
// anything not currently in the DOM
if ($dom.isDetached(subject)) {
subject = (subject as any).filter((index, el) => $dom.isAttached(el))
// if we have nothing left
// just go replay the commands
if (!subject.length) {
return replay()
}
}
log(subject)
return cy.verifyUpcomingAssertions(subject, options, {
onFail (err) {
// if we are failing because our aliased elements
// are less than what is expected then we know we
// need to requery for them and can thus replay
// the commands leading up to the alias
if ((err.type === 'length') && (err.actual < err.expected)) {
return replayFrom = true
}
return false
},
onRetry () {
if (replayFrom) {
return replay()
}
return resolveAlias()
},
})
}
// if this is a route command
if (command.get('name') === 'route') {
if (!((_.indexOf(selector, '.') === -1) ||
(_.keys(state('aliases')).includes(selector.slice(1))))
) {
allParts = _.split(selector, '.')
const index = _.last(allParts)
alias = _.join([alias, index], '.')
}
const requests = cy.getRequestsByAlias(alias) || null
log(requests, 'route')
return requests
}
if (command.get('name') === 'intercept') {
const requests = getAliasedRequests(alias, state)
// detect alias.all and alias.index
const specifier = /\.(all|[\d]+)$/.exec(selector)
if (specifier) {
const [, index] = specifier
if (index === 'all') {
return requests
}
return requests[Number(index)] || null
}
log(requests, command.get('name'))
// by default return the latest match
return _.last(requests) || null
}
// log as primitive
log(subject, 'primitive')
const verifyAssertions = () => {
return cy.verifyUpcomingAssertions(subject, options, {
ensureExistenceFor: false,
onRetry: verifyAssertions,
})
}
return verifyAssertions()
}
return resolveAlias()
}
start('dom')
const setEl = ($el) => {
if (options.log === false) {
return
}
consoleProps.Yielded = $dom.getElements($el)
consoleProps.Elements = $el != null ? $el.length : undefined
options._log.set({ $el })
}
const getElements = () => {
let $el
try {
let scope: (typeof options.withinSubject) | Node[] = options.withinSubject
if (options.includeShadowDom) {
const root = options.withinSubject ? options.withinSubject[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) {
// this is usually a sizzle error (invalid selector)
err.onFail = () => {
if (options.log === false) {
return err
}
options._log.error(err)
}
throw err
}
// if that didnt find anything and we have a within subject
// and we have been explictly told to filter
// then just attempt to filter out elements from our within subject
if (!$el.length && options.withinSubject && options.filter) {
const filtered = (options.withinSubject as JQuery).filter(selector)
// reset $el if this found anything
if (filtered.length) {
$el = filtered
}
}
// store the $el now in case we fail
setEl($el)
// allow retry to be a function which we ensure
// returns truthy before returning its
if (_.isFunction(options.onRetry)) {
const ret = options.onRetry.call(ctx, $el)
if (ret) {
log($el)
return ret
}
} else {
log($el)
return $el
}
}
const resolveElements = () => {
return Promise.try(getElements).then(($el) => {
if (options.verify === false) {
return $el
}
return cy.verifyUpcomingAssertions($el, options, {
onRetry: resolveElements,
})
})
}
return resolveElements()
},
})
Commands.addAll({ prevSubject: ['optional', 'window', 'document', 'element'] }, {
contains (subject, filter, text, userOptions: Partial<Cypress.Loggable & Cypress.Timeoutable & Cypress.CaseMatchable & Cypress.Shadow> = {}) {
// nuke our subject if its present but not an element.
// in these cases its either window or document but
// we dont care.
// we'll null out the subject so it will show up as a parent
// command since its behavior is identical to using it
// as a parent command: cy.contains()
// don't nuke if subject is a shadow root, is a document not an element
if (subject && !$dom.isElement(subject) && !$elements.isShadowRoot(subject[0])) {
subject = null
}
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')
}
const options: InternalContainsOptions = _.defaults({}, userOptions, { log: true, matchCase: true })
if (!(_.isString(text) || _.isFinite(text) || _.isRegExp(text))) {
$errUtils.throwErrByPath('contains.invalid_argument')
}
if (_.isBlank(text)) {
$errUtils.throwErrByPath('contains.empty_string')
}
const getPhrase = () => {
if (filter && subject) {
const node = $dom.stringify(subject, 'short')
return `within the element: ${node} and with the selector: '${filter}' `
}
if (filter) {
return `within the selector: '${filter}' `
}
if (subject) {
const node = $dom.stringify(subject, 'short')
return `within the element: ${node} `
}
return ''
}
const getErr = (err) => {
const { type, negated } = err
if (type === 'existence') {
if (negated) {
return `Expected not to find content: '${text}' ${getPhrase()}but continuously found it.`
}
return `Expected to find content: '${text}' ${getPhrase()}but never did.`
}
return null
}
let consoleProps
if (options.log !== false) {
consoleProps = {
Content: text,
'Applied To': $dom.getElements(subject || state('withinSubject')),
}
options._log = Cypress.log({
message: _.compact([filter, text]),
type: subject ? 'child' : 'parent',
timeout: options.timeout,
consoleProps: () => {
return consoleProps
},
})
}
const setEl = ($el) => {
if (options.log === false) {
return
}
consoleProps.Yielded = $dom.getElements($el)
consoleProps.Elements = $el != null ? $el.length : undefined
options._log.set({ $el })
}
// find elements by the :cy-contains psuedo selector
// and any submit inputs with the attributeContainsWord selector
const selector = $dom.getContainsSelector(text, filter, options)
const resolveElements = () => {
const getOptions = _.extend({}, options, {
// error: getErr(text, phrase)
withinSubject: subject || state('withinSubject') || cy.$$('body'),
filter: true,
log: false,
// retry: false ## dont retry because we perform our own element validation
verify: false, // dont verify upcoming assertions, we do that ourselves
})
return cy.now('get', selector, getOptions).then(($el) => {
if ($el && $el.length) {
$el = $dom.getFirstDeepestElement($el)
}
setEl($el)
return cy.verifyUpcomingAssertions($el, options, {
onRetry: resolveElements,
onFail (err) {
switch (err.type) {
case 'length':
if (err.expected > 1) {
return $errUtils.throwErrByPath('contains.length_option', { onFail: options._log })
}
break
case 'existence':
return err.message = getErr(err)
default:
break
}
return null
},
})
})
}
return Promise
.try(resolveElements)
},
})
Commands.add('shadow', { prevSubject: 'element' }, (subject, options) => {
const userOptions = options || {}
options = _.defaults({}, userOptions, { log: true })
const consoleProps: Record<string, any> = {
'Applied To': $dom.getElements(subject),
}
if (options.log !== false) {
options._log = Cypress.log({
timeout: options.timeout,
consoleProps () {
return consoleProps
},
})
}
const setEl = ($el) => {
if (options.log === false) {
return
}
consoleProps.Yielded = $dom.getElements($el)
consoleProps.Elements = $el?.length
return options._log.set({ $el })
}
const getShadowRoots = () => {
// 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)
setEl($el)
return cy.verifyUpcomingAssertions($el, options, {
onRetry: getShadowRoots,
onFail (err) {
if (err.type !== 'existence') {
return
}
const { message, docsUrl } = $errUtils.cypressErrByPath('shadow.no_shadow_root')
err.message = message
err.docsUrl = docsUrl
},
})
}
return getShadowRoots()
})
}