/
event-manager.ts
903 lines (709 loc) · 27.9 KB
/
event-manager.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
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
import Bluebird from 'bluebird'
import { EventEmitter } from 'events'
import type { MobxRunnerStore } from '@packages/app/src/store/mobx-runner-store'
import type MobX from 'mobx'
import type { LocalBusEmitsMap, LocalBusEventMap, DriverToLocalBus, SocketToDriverMap } from './event-manager-types'
import type { RunState, CachedTestState, AutomationElementId, FileDetails, ReporterStartInfo, ReporterRunState } from '@packages/types'
import { logger } from './logger'
import type { Socket } from '@packages/socket/lib/browser'
import { automation, useRunnerUiStore } from '../store'
import { useScreenshotStore } from '../store/screenshot-store'
import { useStudioStore } from '../store/studio-store'
import { getAutIframeModel } from '.'
import { handlePausing } from './events/pausing'
export type CypressInCypressMochaEvent = Array<Array<string | Record<string, any>>>
// type is default export of '@packages/driver'
// cannot import because it's not type safe and tsc throw many type errors.
type $Cypress = any
const noop = () => {}
let crossOriginOnMessageRef = ({ data, source }: MessageEvent<{
data: any
source: Window
}>) => {
return undefined
}
let crossOriginLogs: {[key: string]: Cypress.Log} = {}
interface AddGlobalListenerOptions {
element: AutomationElementId
randomString: string
}
const driverToLocalAndReporterEvents = 'run:start run:end'.split(' ')
const driverToSocketEvents = 'backend:request automation:request mocha recorder:frame'.split(' ')
const driverTestEvents = 'test:before:run:async test:after:run'.split(' ')
const driverToLocalEvents = 'viewport:changed config stop url:changed page:loading visit:failed visit:blank cypress:in:cypress:runner:event'.split(' ')
const socketRerunEvents = 'runner:restart watched:file:changed'.split(' ')
const socketToDriverEvents = 'net:stubbing:event request:event script:error cross:origin:cookies'.split(' ')
const localToReporterEvents = 'reporter:log:add reporter:log:state:changed reporter:log:remove'.split(' ')
/**
* @type {Cypress.Cypress}
*/
let Cypress
export class EventManager {
reporterBus: EventEmitter = new EventEmitter()
localBus: EventEmitter = new EventEmitter()
Cypress?: $Cypress
selectorPlaygroundModel: any
cypressInCypressMochaEvents: CypressInCypressMochaEvent[] = []
// Used for testing the experimentalSingleTabRunMode experiment. Ensures AUT is correctly destroyed between specs.
ws: Socket
studioStore: ReturnType<typeof useStudioStore>
constructor (
// import '@packages/driver'
private $CypressDriver: any,
// import * as MobX
private Mobx: typeof MobX,
// selectorPlaygroundModel singleton
selectorPlaygroundModel: any,
ws: Socket,
) {
this.selectorPlaygroundModel = selectorPlaygroundModel
this.ws = ws
this.studioStore = useStudioStore()
}
getCypress () {
return Cypress
}
addGlobalListeners (state: MobxRunnerStore, options: AddGlobalListenerOptions) {
// Moving away from the runner turns off all websocket listeners. addGlobalListeners adds them back
// but connect is added when the websocket is created elsewhere so we need to add it back.
if (!this.ws.hasListeners('connect')) {
this.ws.on('connect', () => {
this.ws.emit('runner:connected')
})
}
const rerun = () => {
if (!this) {
// if the tests have been reloaded then there is nothing to rerun
return
}
return this.rerunSpec()
}
const connectionInfo: AddGlobalListenerOptions = {
element: options.element,
randomString: options.randomString,
}
const runnerUiStore = useRunnerUiStore()
this.ws.emit('is:automation:client:connected', connectionInfo, (isConnected: boolean) => {
const connected = isConnected ? automation.CONNECTED : automation.MISSING
// legacy MobX integration
// TODO: UNIFY-1318 - can we delete this, or does the driver depend on this somehow?
this.Mobx.runInAction(() => {
state.automation = connected
})
this.ws.on('automation:disconnected', () => {
this.Mobx.runInAction(() => {
state.automation = automation.DISCONNECTED
})
})
// unified integration
this.ws.on('automation:disconnected', () => {
runnerUiStore.setAutomationStatus('DISCONNECTED')
})
runnerUiStore.setAutomationStatus(connected)
})
this.ws.on('change:to:url', (url) => {
window.location.href = url
})
this.ws.on('automation:push:message', (msg, data = {}) => {
if (!Cypress) return
switch (msg) {
case 'change:cookie':
Cypress.Cookies.log(data.message, data.cookie, data.removed)
break
case 'create:download':
Cypress.downloads.start(data)
break
case 'complete:download':
Cypress.downloads.end(data)
break
default:
break
}
})
this.ws.on('watched:file:changed', () => {
this.studioStore.cancel()
rerun()
})
this.ws.on('dev-server:compile:success', ({ specFile }) => {
if (!specFile || specFile === state?.spec?.absolute) {
rerun()
}
})
socketRerunEvents.forEach((event) => {
this.ws.on(event, rerun)
})
socketToDriverEvents.forEach((event) => {
this.ws.on(event, (...args) => {
if (!Cypress) return
Cypress.emit(event, ...args)
})
})
localToReporterEvents.forEach((event) => {
this.localBus.on(event, (...args) => {
this.reporterBus.emit(event, ...args)
})
})
const logCommand = (testId, logId) => {
const consoleProps = Cypress.runner.getConsolePropsForLog(testId, logId)
logger.logFormatted(consoleProps)
}
this.reporterBus.on('runner:console:error', ({ err, testId, logId }) => {
if (!Cypress) return
if (logId || err) logger.clearLog()
if (logId) logCommand(testId, logId)
if (err) logger.logError(err.stack)
})
this.reporterBus.on('runner:console:log', (testId, logId) => {
if (!Cypress) return
logger.clearLog()
logCommand(testId, logId)
})
this.reporterBus.on('set:user:editor', (editor) => {
this.ws.emit('set:user:editor', editor)
})
this.reporterBus.on('runner:restart', rerun)
const sendEventIfSnapshotProps = (testId, logId, event) => {
if (!Cypress) return
const snapshotProps = Cypress.runner.getSnapshotPropsForLog(testId, logId)
if (snapshotProps) {
this.localBus.emit(event, snapshotProps)
}
}
this.reporterBus.on('runner:show:snapshot', (testId, logId) => {
sendEventIfSnapshotProps(testId, logId, 'show:snapshot')
})
this.reporterBus.on('runner:hide:snapshot', this._hideSnapshot.bind(this))
this.reporterBus.on('runner:pin:snapshot', (testId, logId) => {
sendEventIfSnapshotProps(testId, logId, 'pin:snapshot')
})
this.reporterBus.on('runner:unpin:snapshot', this._unpinSnapshot.bind(this))
this.reporterBus.on('runner:stop', () => {
if (!Cypress) return
Cypress.stop()
})
this.reporterBus.on('save:state', (state) => {
this.saveState(state)
})
this.reporterBus.on('clear:all:sessions', () => {
if (!Cypress) return
Cypress.backend('clear:sessions', true)
.then(rerun)
})
this.reporterBus.on('external:open', (url) => {
this.ws.emit('external:open', url)
})
this.reporterBus.on('get:user:editor', (cb) => {
this.ws.emit('get:user:editor', cb)
})
this.reporterBus.on('open:file:unified', (file: FileDetails) => {
this.emit('open:file', file)
})
this.reporterBus.on('open:file', (url) => {
this.ws.emit('open:file', url)
})
const studioInit = () => {
rerun()
}
this.reporterBus.on('studio:init:test', (testId) => {
this.studioStore.setTestId(testId)
studioInit()
})
this.reporterBus.on('studio:init:suite', (suiteId) => {
this.studioStore.setSuiteId(suiteId)
studioInit()
})
this.reporterBus.on('studio:cancel', () => {
this.studioStore.cancel()
rerun()
})
this.reporterBus.on('studio:remove:command', (commandId) => {
this.studioStore.removeLog(commandId)
})
this.reporterBus.on('studio:save', () => {
this.studioStore.startSave()
})
this.reporterBus.on('studio:copy:to:clipboard', (cb) => {
this._studioCopyToClipboard(cb)
})
this.localBus.on('studio:start', () => {
rerun()
})
this.localBus.on('studio:copy:to:clipboard', (cb) => {
this._studioCopyToClipboard(cb)
})
this.localBus.on('studio:save', (saveInfo) => {
this.ws.emit('studio:save', saveInfo, (err) => {
if (err) {
this.reporterBus.emit('test:set:state', this.studioStore.saveError(err), noop)
}
})
})
this.localBus.on('studio:cancel', () => {
this.studioStore.cancel()
rerun()
})
this.ws.on('aut:destroy:init', () => {
const autIframe = getAutIframeModel()
autIframe.destroy()
this.ws.emit('aut:destroy:complete')
})
// @ts-ignore
const $window = this.$CypressDriver.$(window)
// This is a test-only event. It's used to
// trigger a rerun for the driver rerun.cy.js spec.
$window.on('test:trigger:rerun', rerun)
// when we actually unload then
// nuke all of the cookies again
// so we clear out unload
$window.on('unload', () => {
this._clearAllCookies()
})
// when our window triggers beforeunload
// we know we've change the URL and we need
// to clear our cookies
// additionally we set unload to true so
// that Cypress knows not to set any more
// cookies
$window.on('beforeunload', () => {
this.reporterBus.emit('reporter:restart:test:run')
this._clearAllCookies()
this._setUnload()
})
}
start (config) {
if (config.socketId) {
this.ws.emit('app:connect', config.socketId)
}
}
setup (config) {
Cypress = this.Cypress = this.$CypressDriver.create(config)
// expose Cypress globally
// @ts-ignore
window.Cypress = Cypress
this._addListeners()
this.ws.emit('watch:test:file', config.spec)
if (config.isTextTerminal || config.experimentalInteractiveRunEvents) {
this.ws.emit('plugins:before:spec', config.spec)
}
}
isBrowser (browserName) {
if (!this.Cypress) return false
return this.Cypress.isBrowser(browserName)
}
initialize ($autIframe: JQuery<HTMLIFrameElement>, config: Record<string, any>) {
performance.mark('initialize-start')
return Cypress.initialize({
$autIframe,
onSpecReady: () => {
// get the current runnable states and cached test state
// in case we reran mid-test due to a visit to a new domain
this.ws.emit('get:cached:test:state', (runState: RunState = {}, testState: CachedTestState) => {
if (!Cypress.runner) {
// the tests have been reloaded
return
}
const hideCommandLog = window.__CYPRESS_CONFIG__.hideCommandLog
this.studioStore.initialize(config, runState)
const runnables = Cypress.runner.normalizeAll(runState.tests, hideCommandLog)
const run = () => {
performance.mark('initialize-end')
performance.measure('initialize', 'initialize-start', 'initialize-end')
this._runDriver(runState, testState)
}
if (!hideCommandLog) {
this.reporterBus.emit('runnables:ready', runnables)
}
if (runState?.numLogs) {
Cypress.runner.setNumLogs(runState.numLogs)
}
if (runState.startTime) {
Cypress.runner.setStartTime(runState.startTime)
}
if (config.isTextTerminal && !runState.currentId) {
// we are in run mode and it's the first load
// store runnables in backend and maybe send to dashboard
return this.ws.emit('set:runnables:and:maybe:record:tests', runnables, run)
}
if (runState.currentId) {
// if we have a currentId it means
// we need to tell the Cypress to skip
// ahead to that test
Cypress.runner.resumeAtTest(runState.currentId, runState.emissions)
}
return run()
})
},
})
}
_addListeners () {
Cypress.on('message', (msg, data, cb) => {
this.ws.emit('client:request', msg, data, cb)
})
driverToSocketEvents.forEach((event) => {
Cypress.on(event, (...args) => {
return this.ws.emit(event, ...args)
})
})
Cypress.on('collect:run:state', () => {
if (Cypress.env('NO_COMMAND_LOG')) {
return Bluebird.resolve()
}
return new Bluebird((resolve) => {
this.reporterBus.emit('reporter:collect:run:state', (reporterState: ReporterRunState) => {
resolve({
...reporterState,
studio: {
testId: this.studioStore.testId,
suiteId: this.studioStore.suiteId,
url: this.studioStore.url,
},
})
})
})
})
Cypress.on('log:added', (log) => {
// TODO: UNIFY-1318 - Race condition in unified runner - we should not need this null check
if (!Cypress.runner) {
return
}
const displayProps = Cypress.runner.getDisplayPropsForLog(log)
this._interceptStudio(displayProps)
this.reporterBus.emit('reporter:log:add', displayProps)
})
Cypress.on('log:changed', (log) => {
// TODO: UNIFY-1318 - Race condition in unified runner - we should not need this null check
if (!Cypress.runner) {
return
}
const displayProps = Cypress.runner.getDisplayPropsForLog(log)
this._interceptStudio(displayProps)
this.reporterBus.emit('reporter:log:state:changed', displayProps)
})
// TODO: MOVE BACK INTO useEventManager. Verify this works
const screenshotStore = useScreenshotStore()
const handleBeforeScreenshot = (config, cb) => {
if (config.appOnly) {
screenshotStore.setScreenshotting(true)
}
const beforeThenCb = () => {
this.localBus.emit('before:screenshot', config)
cb()
}
if (Cypress.env('NO_COMMAND_LOG')) {
return beforeThenCb()
}
const wait = !config.appOnly && config.waitForCommandSynchronization
if (!config.appOnly) {
const { id, isOpen } = config
this.reporterBus.emit('test:set:state', { id, isOpen }, wait ? beforeThenCb : undefined)
}
if (!wait) beforeThenCb()
}
Cypress.on('before:screenshot', handleBeforeScreenshot)
const handleAfterScreenshot = (config) => {
screenshotStore.setScreenshotting(false)
this.localBus.emit('after:screenshot', config)
}
Cypress.on('after:screenshot', handleAfterScreenshot)
driverTestEvents.forEach((event) => {
Cypress.on(event, (test, cb) => {
this.reporterBus.emit(event, test, cb)
})
})
driverToLocalAndReporterEvents.forEach((event) => {
Cypress.on(event, (...args) => {
this.localBus.emit(event, ...args)
this.reporterBus.emit(event, ...args)
})
})
driverToLocalEvents.forEach((event) => {
Cypress.on(event, (...args: unknown[]) => {
// special case for asserting the correct mocha events + payload
// is emitted from cypress/driver when running e2e tests using
// "cypress in cypress"
if (event === 'cypress:in:cypress:runner:event') {
this.cypressInCypressMochaEvents.push(args as CypressInCypressMochaEvent[])
if (args[0] === 'mocha' && args[1] === 'end') {
this.emit('cypress:in:cypress:run:complete', this.cypressInCypressMochaEvents)
// reset
this.cypressInCypressMochaEvents = []
}
return
}
// @ts-ignore
// TODO: UNIFY-1318 - strongly typed event emitter.
return this.emit(event, ...args)
})
})
Cypress.on('script:error', (err) => {
Cypress.stop()
this.localBus.emit('script:error', err)
})
Cypress.on('test:before:run:async', (_attr, test) => {
this.studioStore.interceptTest(test)
})
Cypress.on('test:after:run', (test) => {
if (this.studioStore.isOpen && test.state !== 'passed') {
this.studioStore.testFailed()
}
})
handlePausing(this.getCypress, this.reporterBus)
Cypress.on('test:before:run', (...args) => {
Cypress.primaryOriginCommunicator.toAllSpecBridges('test:before:run', ...args)
})
Cypress.on('test:before:run:async', (...args) => {
Cypress.primaryOriginCommunicator.toAllSpecBridges('test:before:run:async', ...args)
})
// Inform all spec bridges that the primary origin has begun to unload.
Cypress.on('window:before:unload', () => {
Cypress.primaryOriginCommunicator.toAllSpecBridges('before:unload', window.origin)
})
// Reflect back to the requesting origin the status of the 'duringUserTestExecution' state
Cypress.primaryOriginCommunicator.on('sync:during:user:test:execution', (_data, { origin, responseEvent }) => {
Cypress.primaryOriginCommunicator.toSpecBridge(origin, responseEvent, cy.state('duringUserTestExecution'))
})
Cypress.on('request:snapshot:from:spec:bridge', ({ log, name, options, specBridge, addSnapshot }: {
log: Cypress.Log
name?: string
options?: any
specBridge: string
addSnapshot: (snapshot: any, options: any, shouldRebindSnapshotFn: boolean) => Cypress.Log
}) => {
const eventID = log.get('id')
const requestSnapshot = () => {
return Cypress.primaryOriginCommunicator.toSpecBridgePromise({
origin: specBridge,
event: 'snapshot:generate:for:log',
data: {
name,
id: eventID,
},
}).then((crossOriginSnapshot) => {
const snapshot = crossOriginSnapshot.body ? crossOriginSnapshot : null
addSnapshot.apply(log, [snapshot, options, false])
})
}
requestSnapshot().catch(() => {
// If a spec bridge isn't present to respond this isn't an error and there is nothing to do.
})
})
Cypress.primaryOriginCommunicator.on('before:unload', (origin) => {
// In webkit the before:unload event could come in after the on load event has already happened.
// To prevent hanging we will only set the state to unstable if we are currently on the same origin as the unload event,
// otherwise we assume that the load event has already occurred and the event is no longer relevant.
if (Cypress.state('autLocation')?.origin === origin) {
// We specifically don't call 'cy.isStable' here because we don't want to inject another load event.
cy.state('isStable', false)
}
// Re-broadcast to any other specBridges.
Cypress.primaryOriginCommunicator.toAllSpecBridges('before:unload', origin)
})
Cypress.primaryOriginCommunicator.on('expect:origin', (origin) => {
this.localBus.emit('expect:origin', origin)
})
Cypress.primaryOriginCommunicator.on('viewport:changed', (viewport, { origin }) => {
const callback = () => {
Cypress.primaryOriginCommunicator.toSpecBridge(origin, 'viewport:changed:end')
}
Cypress.primaryOriginCommunicator.emit('sync:viewport', viewport)
this.localBus.emit('viewport:changed', viewport, callback)
})
Cypress.primaryOriginCommunicator.on('before:screenshot', (config, { origin }) => {
const callback = () => {
Cypress.primaryOriginCommunicator.toSpecBridge(origin, 'before:screenshot:end')
}
handleBeforeScreenshot(config, callback)
})
Cypress.primaryOriginCommunicator.on('url:changed', ({ url }) => {
this.localBus.emit('url:changed', url)
})
Cypress.primaryOriginCommunicator.on('after:screenshot', handleAfterScreenshot)
Cypress.primaryOriginCommunicator.on('log:added', (attrs) => {
// If the test is over and the user enters interactive snapshot mode, do not add cross origin logs to the test runner.
if (Cypress.state('test')?.final) return
// Create a new local log representation of the cross origin log.
// It will be attached to the current command.
// We also keep a reference to it to update it in the future.
crossOriginLogs[attrs.id] = Cypress.log(attrs)
})
Cypress.primaryOriginCommunicator.on('log:changed', (attrs) => {
// Retrieve the referenced log and update it.
const log = crossOriginLogs[attrs.id]
// this will trigger a log changed event for the log itself.
log?.set(attrs)
})
// This message comes from the AUT, not the spec bridge. This is called in
// the event that cookies are set via document.cookie in a cross origin
// AUT prior to attaching a spec bridge.
Cypress.primaryOriginCommunicator.on(
'aut:set:cookie',
(options: { cookie, url: string, sameSiteContext: string }) => {
// unlikely there will be errors, but ignore them in any case, since
// they're not user-actionable
Cypress.automation('set:cookie', options.cookie).catch(() => {})
Cypress.backend('cross:origin:set:cookie', options).catch(() => {})
},
)
/**
* Call a backend request for the requesting spec bridge since we cannot have websockets in the spec bridges.
* Return it's response.
*/
Cypress.primaryOriginCommunicator.on('backend:request', async ({ args }, { source, responseEvent }) => {
const response = await Cypress.backend(...args)
Cypress.primaryOriginCommunicator.toSource(source, responseEvent, response)
})
/**
* Call an automation request for the requesting spec bridge since we cannot have websockets in the spec bridges.
* Return it's response.
*/
Cypress.primaryOriginCommunicator.on('automation:request', async ({ args }, { source, responseEvent }) => {
const response = await Cypress.automation(...args)
Cypress.primaryOriginCommunicator.toSource(source, responseEvent, response)
})
// The window.top should not change between test reloads, and we only need to bind the message event when Cypress is recreated
// Forward all message events to the current instance of the multi-origin communicator
if (!window.top) throw new Error('missing window.top in event-manager')
/**
* NOTE: Be sure to remove the cross origin onMessage bus to make sure the communicator doesn't live on inside a closure and cause tied up events.
*
* This is applicable when a user navigates away from the runner and into the "specs" menu or otherwise,
* and the EventManager is recreated. This is the main reason this reference is scoped to the file and NOT the instance.
*
* This is also applicable when a user changes their spec file and hot reloads their spec, in which case we need to rebind onMessage
* with the newly creates Cypress.primaryOriginCommunicator
*/
window?.top?.removeEventListener('message', crossOriginOnMessageRef, false)
crossOriginOnMessageRef = ({ data, source }) => {
Cypress?.primaryOriginCommunicator.onMessage({ data, source })
return undefined
}
window.top.addEventListener('message', crossOriginOnMessageRef, false)
}
_runDriver (runState: RunState, testState: CachedTestState) {
performance.mark('run-s')
Cypress.run(testState, () => {
performance.mark('run-e')
performance.measure('run', 'run-s', 'run-e')
})
const hasRunnableId = !!this.studioStore.testId || !!this.studioStore.suiteId
this.reporterBus.emit('reporter:start', {
startTime: Cypress.runner.getStartTime(),
numPassed: runState.passed,
numFailed: runState.failed,
numPending: runState.pending,
autoScrollingEnabled: runState.autoScrollingEnabled,
isSpecsListOpen: runState.isSpecsListOpen,
scrollTop: runState.scrollTop,
studioActive: hasRunnableId,
} as ReporterStartInfo)
}
stop () {
this.localBus.removeAllListeners()
this.ws.off()
}
async teardown (state: MobxRunnerStore, isRerun = false) {
if (!Cypress) {
return
}
state.setIsLoading(true)
if (!isRerun) {
// only clear test state when a new spec is selected
Cypress.backend('reset:cached:test:state')
}
// when we are re-running we first need to stop cypress always
Cypress.stop()
// Clean up the primary communicator to prevent possible memory leaks / dangling references before the Cypress instance is destroyed.
Cypress.primaryOriginCommunicator.removeAllListeners()
// clean up the cross origin logs in memory to prevent dangling references as the log objects themselves at this point will no longer be needed.
crossOriginLogs = {}
this.studioStore.setInactive()
}
resetReporter () {
return new Bluebird((resolve) => {
this.reporterBus.once('reporter:restarted', resolve)
this.reporterBus.emit('reporter:restart:test:run')
})
}
async rerunSpec () {
if (!this || !Cypress) {
// if the tests have been reloaded then there is nothing to rerun
return
}
await this.resetReporter()
// this probably isn't 100% necessary since Cypress will fall out of scope
// but we want to be aggressive here and force GC early and often
Cypress.removeAllListeners()
this.localBus.emit('restart')
}
_interceptStudio (displayProps) {
if (this.studioStore.isActive) {
displayProps.hookId = this.studioStore.hookId
if (displayProps.name === 'visit' && displayProps.state === 'failed') {
this.studioStore.testFailed()
this.reporterBus.emit('test:set:state', this.studioStore.testError, noop)
}
}
return displayProps
}
_studioCopyToClipboard (cb) {
this.ws.emit('studio:get:commands:text', this.studioStore.logs, (commandsText) => {
this.studioStore.copyToClipboard(commandsText)
.then(cb)
})
}
emit<K extends Extract<keyof LocalBusEmitsMap, string>>(k: K, v: LocalBusEmitsMap[K]): void
emit<K extends Extract<keyof DriverToLocalBus, string>>(k: K, v: DriverToLocalBus[K]): void
emit<K extends Extract<keyof SocketToDriverMap, string>>(k: K, v: SocketToDriverMap[K]): void
emit (event: string, ...args: any[]) {
this.localBus.emit(event, ...args)
}
on<K extends Extract<keyof LocalBusEventMap, string>>(k: K, f: (v: LocalBusEventMap[K]) => void): void
on<K extends Extract<keyof DriverToLocalBus, string>>(k: K, f: (v: DriverToLocalBus[K]) => void): void
on<K extends Extract<keyof SocketToDriverMap, string>>(k: K, f: (v: SocketToDriverMap[K]) => void): void
on (event: string, listener: (...args: any[]) => void): void
on (event: string, listener: (...args: any[]) => void) {
this.localBus.on(event, listener)
}
off (event: string, listener: (...args: any[]) => void) {
this.localBus.off(event, listener)
}
notifyRunningSpec (specFile) {
this.ws.emit('spec:changed', specFile)
}
notifyCrossOriginBridgeReady (origin) {
// Any multi-origin event appends the origin as the third parameter and we do the same here for this short circuit
Cypress.primaryOriginCommunicator.emit('bridge:ready', undefined, { origin })
}
snapshotUnpinned () {
this._unpinSnapshot()
this._hideSnapshot()
this.reporterBus.emit('reporter:snapshot:unpinned')
}
_unpinSnapshot () {
this.localBus.emit('unpin:snapshot')
}
_hideSnapshot () {
this.localBus.emit('hide:snapshot')
}
launchBrowser (browser) {
this.ws.emit('reload:browser', window.location.toString(), browser && browser.name)
}
// clear all the cypress specific cookies
// whenever our app starts
// and additional when we stop running our tests
_clearAllCookies () {
if (!Cypress) return
Cypress.Cookies.clearCypressCookies()
}
_setUnload () {
if (!Cypress) return
Cypress.Cookies.setCy('unload', true)
}
saveState (state) {
this.localBus.emit('save:app:state', state)
}
// useful for testing
_testingOnlySetCypress (cypress: any) {
Cypress = cypress
}
}