-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
log.ts
653 lines (514 loc) · 15.4 KB
/
log.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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
import _ from 'lodash'
import $ from 'jquery'
import clone from 'clone'
import { HIGHLIGHT_ATTR } from '../cy/snapshots'
import { extend as extendEvents } from './events'
import $dom from '../dom'
import $utils from './utils'
import $errUtils from './error_utils'
// adds class methods for command, route, and agent logging
// including the intermediate $Log interface
const groupsOrTableRe = /^(groups|table)$/
const parentOrChildRe = /parent|child|system/
const SNAPSHOT_PROPS = 'id snapshots $el url coords highlightAttr scrollBy viewportWidth viewportHeight'.split(' ')
const DISPLAY_PROPS = 'id alias aliasType callCount displayName end err event functionName hookId instrument isStubbed group message method name numElements showError numResponses referencesAlias renderProps state testId timeout type url visible wallClockStartedAt testCurrentRetry'.split(' ')
const BLACKLIST_PROPS = 'snapshots'.split(' ')
let counter = 0
export const LogUtils = {
// mutate attrs by nulling out
// object properties
reduceMemory: (attrs) => {
return _.each(attrs, (value, key) => {
if (_.isObject(value)) {
attrs[key] = null
}
})
},
toSerializedJSON (attrs) {
const { isDom } = $dom
const stringify = function (value, key) {
if (BLACKLIST_PROPS.includes(key)) {
return null
}
if (_.isArray(value)) {
return _.map(value, stringify)
}
if (isDom(value)) {
return $dom.stringify(value, 'short')
}
if (_.isFunction(value) && groupsOrTableRe.test(key)) {
return value()
}
if (_.isFunction(value) || _.isSymbol(value)) {
return value.toString()
}
if (_.isObject(value)) {
// clone to nuke circular references
// and blow away anything that throws
try {
return _.mapValues(clone(value), stringify)
} catch (err) {
return null
}
}
return value
}
return _.mapValues(attrs, stringify)
},
getDisplayProps: (attrs) => {
return {
..._.pick(attrs, DISPLAY_PROPS),
hasSnapshot: !!attrs.snapshots,
hasConsoleProps: !!attrs.consoleProps,
}
},
getConsoleProps: (attrs) => {
return attrs.consoleProps
},
getSnapshotProps: (attrs) => {
return _.pick(attrs, SNAPSHOT_PROPS)
},
countLogsByTests (tests: Record<string, any> = {}) {
if (_.isEmpty(tests)) {
return 0
}
return _
.chain(tests)
.flatMap((test) => test.prevAttempts ? [test, ...test.prevAttempts] : [test])
.flatMap<{id: string}>((tests) => [].concat(tests.agents, tests.routes, tests.commands))
.compact()
.union([{ id: '0' }])
// id is a string in the form of 'log-domain-#', grab the number off the end.
.map(({ id }) => parseInt((id.match(/\d*$/) || ['0'])[0]))
.max()
.value()
},
// TODO: fix this
setCounter: (num) => {
return counter = num
},
getCounter: () => {
return counter
},
}
const defaults = function (state: Cypress.State, config, obj) {
const instrument = obj.instrument != null ? obj.instrument : 'command'
// dont set any defaults if this
// is an agent or route because we
// may not even be inside of a command
if (instrument === 'command') {
const current = state('current')
// we are logging a command instrument by default
_.defaults(obj, current != null ? current.pick('name', 'type') : undefined)
// force duals to become either parents or childs
// normally this would be handled by the command itself
// but in cases where the command purposely does not log
// then it could still be logged during a failure, which
// is why we normalize its type value
if (typeof obj.type === 'string' && !parentOrChildRe.test(obj.type)) {
// does this command have a previously linked command
// by chainer id
obj.type = (current != null ? current.hasPreviouslyLinkedCommand() : undefined) ? 'child' : 'parent'
}
_.defaults(obj, {
timeout: config('defaultCommandTimeout'),
event: false,
renderProps () {
return {}
},
consoleProps () {
// if we don't have a current command just bail
if (!current) {
return {}
}
const ret = $dom.isElement(current.get('subject')) ?
$dom.getElements(current.get('subject'))
:
current.get('subject')
return { Yielded: ret }
},
})
// if obj.isCurrent
// stringify the obj.message (if it exists) or current.get("args")
obj.message = $utils.stringify(obj.message != null ? obj.message : (current != null ? current.get('args') : undefined))
// allow type to by a dynamic function
// so it can conditionally return either
// parent or child (useful in assertions)
if (_.isFunction(obj.type)) {
obj.type = obj.type(current, state('subject'))
}
}
const runnable = state('runnable')
const getTestAttemptFromRunnable = (runnable) => {
if (!runnable) {
return
}
const t = $utils.getTestFromRunnable(runnable)
return t._currentRetry || 0
}
counter++
_.defaults(obj, {
id: `log-${window.location.origin}-${counter}`,
state: 'pending',
instrument: 'command',
url: state('url'),
hookId: state('hookId'),
testId: runnable ? runnable.id : undefined,
testCurrentRetry: getTestAttemptFromRunnable(state('runnable')),
viewportWidth: state('viewportWidth'),
viewportHeight: state('viewportHeight'),
referencesAlias: undefined,
alias: undefined,
aliasType: undefined,
message: undefined,
timeout: undefined,
wallClockStartedAt: new Date().toJSON(),
renderProps () {
return {}
},
consoleProps () {
return {}
},
})
const logGroupIds = state('logGroupIds') || []
if (logGroupIds.length) {
obj.group = _.last(logGroupIds)
}
if (obj.groupEnd) {
state('logGroupIds', _.slice(logGroupIds, 0, -1))
}
if (obj.groupStart) {
state('logGroupIds', (logGroupIds).concat(obj.id))
}
return obj
}
class Log {
cy: any
state: Cypress.State
config: any
fireChangeEvent: ((log) => (void | undefined))
obj: any
private attributes: Record<string, any> = {}
constructor (cy, state, config, fireChangeEvent, obj) {
this.cy = cy
this.state = state
this.config = config
// only fire the log:state:changed event as fast as every 4ms
this.fireChangeEvent = _.debounce(fireChangeEvent, 4)
this.obj = defaults(state, config, obj)
extendEvents(this)
}
get (attr) {
if (attr) {
return this.attributes[attr]
}
return this.attributes
}
unset (key) {
return this.set(key, undefined)
}
invoke (key) {
const invoke = () => {
// ensure this is a callable function
// and set its default to empty object literal
const fn = this.get(key)
if (_.isFunction(fn)) {
return fn()
}
return fn
}
return invoke() || {}
}
toJSON () {
return _
.chain(this.attributes)
.omit('error')
.omitBy(_.isFunction)
.extend({
err: $errUtils.wrapErr(this.get('error')),
consoleProps: this.invoke('consoleProps'),
renderProps: this.invoke('renderProps'),
})
.value()
}
set (key, val?) {
if (_.isString(key)) {
this.obj = {}
this.obj[key] = val
} else {
this.obj = key
}
if ('url' in this.obj) {
// always stringify the url property
this.obj.url = (this.obj.url != null ? this.obj.url : '').toString()
}
// convert onConsole to consoleProps
// for backwards compatibility
if (this.obj.onConsole) {
this.obj.consoleProps = this.obj.onConsole
}
// if we have an alias automatically
// figure out what type of alias it is
if (this.obj.alias) {
_.defaults(this.obj, { aliasType: this.obj.$el ? 'dom' : 'primitive' })
}
// dont ever allow existing id's to be mutated
if (this.attributes.id) {
delete this.obj.id
}
_.extend(this.attributes, this.obj)
// if we have an consoleProps function
// then re-wrap it
if (this.obj && _.isFunction(this.obj.consoleProps)) {
this.wrapConsoleProps()
}
if (this.obj && this.obj.$el) {
this.setElAttrs()
}
this.fireChangeEvent(this)
return this
}
pick (...args) {
return _.pick(this.attributes, args)
}
snapshot (name?, options: any = {}) {
// bail early and don't snapshot if we're in headless mode
// or we're not storing tests
if (!this.config('isInteractive') || (this.config('numTestsKeptInMemory') === 0)) {
return this
}
_.defaults(options, {
at: null,
next: null,
})
const snapshot = this.cy.createSnapshot(name, this.get('$el'))
const snapshots = this.get('snapshots') || []
// don't add snapshot if we couldn't create one, which can happen
// if the snapshotting process errors
// https://github.com/cypress-io/cypress/issues/15816
if (snapshot) {
// insert at index 'at' or whatever is the next position
snapshots[options.at || snapshots.length] = snapshot
}
this.set('snapshots', snapshots)
if (options.next) {
const fn = this.snapshot
this.snapshot = function () {
// restore the fn
this.snapshot = fn
// call orig fn with next as name
return fn.call(this, options.next)
}
}
return this
}
error (err) {
const logGroupIds = this.state('logGroupIds') || []
// current log was responsible to creating the current log group so end the current group
if (_.last(logGroupIds) === this.attributes.id) {
this.endGroup()
}
this.set({
ended: true,
error: err,
state: 'failed',
})
return this
}
end () {
// dont set back to passed if we've already ended
if (this.get('ended')) {
// we do need to trigger the change event since
// xhr onLoad and proxy-logging updateRequestWithResponse can sometimes
// happen in a different order and the log data in each is different
this.fireChangeEvent(this)
return
}
this.set({
ended: true,
state: 'passed',
})
return this
}
endGroup () {
this.state('logGroupIds', _.slice(this.state('logGroupIds'), 0, -1))
}
getError (err) {
return err.stack || err.message
}
setElAttrs () {
const $el = this.get('$el')
if (!$el) {
return
}
if (_.isElement($el)) {
// wrap the element in jquery
// if its just a plain element
return this.set('$el', $($el))
}
// if we've passed something like
// <window> or <document> here or
// a primitive then unset $el
if (!$dom.isJquery($el)) {
return this.unset('$el')
}
// make sure all $el elements are visible!
this.obj = {
highlightAttr: HIGHLIGHT_ATTR,
numElements: $el.length,
visible: $el.length === $el.filter(':visible').length,
}
return this.set(this.obj, { silent: true })
}
merge (log) {
// merges another logs attributes into
// ours by also removing / adding any properties
// on the original
// 1. calculate which properties to unset
const unsets = _.chain(this.attributes).keys().without(..._.keys(log.get())).value()
_.each(unsets, (unset) => {
return this.unset(unset)
})
// 2. merge in any other properties
return this.set(log.get())
}
_shouldAutoEnd () {
// must be autoEnd
// and not already ended
// and not an event
// and a command
return (this.get('autoEnd') !== false) &&
(this.get('ended') !== true) &&
(this.get('event') === false) &&
(this.get('instrument') === 'command')
}
finish () {
// end our command since our subject
// has been resolved at this point
// unless its already been 'ended'
// or has been specifically told not to auto resolve
if (this._shouldAutoEnd()) {
if (this.get('snapshot') !== false) {
this.snapshot()
}
return this.end()
}
return
}
wrapConsoleProps () {
const _this = this
const { consoleProps } = this.attributes
this.attributes.consoleProps = function (...args) {
const key = _this.get('event') ? 'Event' : 'Command'
const consoleObj: Record<string, any> = {}
consoleObj[key] = _this.get('name')
// merge in the other properties from consoleProps
_.extend(consoleObj, consoleProps.apply(this, args))
// TODO: right here we need to automatically
// merge in "Yielded + Element" if there is an $el
// and finally add error if one exists
if (_this.get('error')) {
_.defaults(consoleObj, {
Error: _this.getError(_this.get('error')),
})
}
// add note if no snapshot exists on command instruments
if ((_this.get('instrument') === 'command') && !_this.get('snapshots')) {
consoleObj.Snapshot = 'The snapshot is missing. Displaying current state of the DOM.'
} else {
delete consoleObj.Snapshot
}
return consoleObj
}
}
}
class LogManager {
logs: Record<string, any> = {}
constructor () {
this.fireChangeEvent = this.fireChangeEvent.bind(this)
}
trigger (log, event) {
// bail if we never fired our initial log event
if (!log._hasInitiallyLogged) {
return
}
// bail if we've reset the logs due to a Cypress.abort
if (!this.logs[log.get('id')]) {
return
}
const attrs = log.toJSON()
// only trigger this event if our last stored
// emitted attrs do not match the current toJSON
if (!_.isEqual(log._emittedAttrs, attrs)) {
log._emittedAttrs = attrs
log.emit(event, attrs)
return Cypress.action(event, attrs, log)
}
}
triggerLog (log) {
log._hasInitiallyLogged = true
return this.trigger(log, 'command:log:added')
}
addToLogs (log) {
const id = log.get('id')
this.logs[id] = true
}
fireChangeEvent (log) {
return this.trigger(log, 'command:log:changed')
}
createLogFn (cy, state, config) {
return (options: any = {}) => {
if (!_.isObject(options)) {
$errUtils.throwErrByPath('log.invalid_argument', { args: { arg: options } })
}
const log = new Log(cy, state, config, this.fireChangeEvent, options)
log.set(options)
// if snapshot was passed
// in, go ahead and snapshot
if (log.get('snapshot')) {
log.snapshot()
}
// if end was passed in
// go ahead and end
if (log.get('end')) {
log.end()
}
if (log.get('error')) {
log.error(log.get('error'))
}
log.wrapConsoleProps()
const onBeforeLog = state('onBeforeLog')
// dont trigger log if this function
// explicitly returns false
if (_.isFunction(onBeforeLog)) {
if (onBeforeLog.call(cy, log) === false) {
return
}
}
// set the log on the command
const current = state('current')
if (current) {
current.log(log)
}
this.addToLogs(log)
if (options.sessionInfo) {
Cypress.emit('session:add', log.toJSON())
}
if (options.emitOnly) {
return
}
this.triggerLog(log)
// if not current state then the log is being run
// with no command reference, so just end the log
if (!current) {
log.end()
}
return log
}
}
}
export function create (Cypress, cy, state, config) {
counter = 0
const logManager = new LogManager()
return logManager.createLogFn(cy, state, config)
}