-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
run.ts
1212 lines (966 loc) · 37.9 KB
/
run.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
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* eslint-disable no-console, @cypress/dev/arrow-body-multiline-braces */
import _ from 'lodash'
import pkg from '@packages/root'
import path from 'path'
import chalk from 'chalk'
import Debug from 'debug'
import Bluebird from 'bluebird'
import assert from 'assert'
import recordMode from './record'
import * as errors from '../errors'
import Reporter from '../reporter'
import browserUtils from '../browsers'
import { openProject } from '../open_project'
import * as videoCapture from '../video_capture'
import { fs } from '../util/fs'
import runEvents from '../plugins/run_events'
import env from '../util/env'
import trash from '../util/trash'
import random from '../util/random'
import system from '../util/system'
import chromePolicyCheck from '../util/chrome_policy_check'
import type { SpecWithRelativeRoot, SpecFile, TestingType, OpenProjectLaunchOpts, FoundBrowser, BrowserVideoController, VideoRecording, ProcessOptions } from '@packages/types'
import type { Cfg, ProjectBase } from '../project-base'
import type { Browser } from '../browsers/types'
import * as printResults from '../util/print-run'
import type { ProtocolManager } from '../cloud/protocol'
import { telemetry } from '@packages/telemetry'
import { CypressRunResult, createPublicBrowser, createPublicConfig, createPublicRunResults, createPublicSpec, createPublicSpecResults } from './results'
import { EarlyExitTerminator } from '../util/graceful_crash_handling'
import { CDPFailedToStartFirefox } from '../browsers/firefox'
import type { CypressError } from '@packages/errors'
type SetScreenshotMetadata = (data: TakeScreenshotProps) => void
type ScreenshotMetadata = ReturnType<typeof screenshotMetadata>
type TakeScreenshotProps = any
type RunEachSpec = any
type BeforeSpecRun = any
type AfterSpecRun = any
type Project = NonNullable<ReturnType<typeof openProject['getProject']>>
let currentSetScreenshotMetadata: SetScreenshotMetadata
let isRunCancelled = false
const debug = Debug('cypress:server:run')
const DELAY_TO_LET_VIDEO_FINISH_MS = 1000
let earlyExitTerminator = new EarlyExitTerminator()
const relativeSpecPattern = (projectRoot, pattern) => {
if (typeof pattern === 'string') {
return pattern.replace(`${projectRoot}/`, '')
}
return pattern.map((x) => x.replace(`${projectRoot}/`, ''))
}
const iterateThroughSpecs = function (options: { specs: SpecFile[], runEachSpec: RunEachSpec, beforeSpecRun?: BeforeSpecRun, afterSpecRun?: AfterSpecRun, config: Cfg }) {
const { specs, runEachSpec, beforeSpecRun, afterSpecRun, config } = options
const serial = () => {
return Bluebird.mapSeries(specs, runEachSpec)
}
const ranSpecs: SpecFile[] = []
async function parallelAndSerialWithRecord (runs) {
const { spec, claimedInstances, totalInstances, estimated, instanceId } = await beforeSpecRun()
// no more specs to run? then we're done!
if (!spec) return runs
// find the actual spec object amongst
// our specs array since the API sends us
// the relative name
const specObject = _.find(specs, { relative: spec })
if (!specObject) throw new Error(`Unable to find specObject for spec '${spec}'`)
ranSpecs.push(specObject)
const results = await runEachSpec(
specObject,
claimedInstances - 1,
totalInstances,
estimated,
instanceId,
)
runs.push(results)
await afterSpecRun(specObject, results, config)
// recurse
return parallelAndSerialWithRecord(runs)
}
if (beforeSpecRun) {
// if we are running in parallel
// then ask the server for the next spec
return parallelAndSerialWithRecord([])
}
// else iterate in serial
return serial()
}
async function getProjectId (project, id) {
if (id == null) {
id = env.get('CYPRESS_PROJECT_ID')
}
// if we have an ID just use it
if (id) return id
try {
return project.getProjectId()
} catch (err) {
// no id no problem
return null
}
}
const sumByProp = (runs, prop) => {
return _.sumBy(runs, prop) || 0
}
const getRun = (run, prop) => {
return _.get(run, prop)
}
async function writeOutput (outputPath, results) {
if (!outputPath) {
return
}
debug('saving output results %o', { outputPath })
return fs.outputJson(outputPath, results)
}
const onWarning = (err) => {
console.log(chalk.yellow(err.message))
}
const openProjectCreate = (projectRoot, socketId, args) => {
// now open the project to boot the server
// putting our web client app in headless mode
// - NO display server logs (via morgan)
// - YES display reporter results (via mocha reporter)
const options = {
socketId,
morgan: false,
report: true,
isTextTerminal: args.isTextTerminal,
// pass the list of browsers we have detected when opening a project
// to give user's plugins file a chance to change it
browsers: args.browsers,
onWarning,
spec: args.spec,
onError: args.onError,
}
return openProject.create(projectRoot, args, options)
}
async function checkAccess (folderPath) {
return fs.access(folderPath, fs.constants.W_OK).catch((err) => {
if (['EACCES', 'EPERM', 'EROFS'].includes(err.code)) {
// we cannot write due to folder permissions, or read-only filesystem
return errors.warning('FOLDER_NOT_WRITABLE', folderPath)
}
throw err
})
}
const createAndOpenProject = async (options) => {
const { projectRoot, projectId, socketId } = options
await checkAccess(projectRoot)
const open_project = await openProjectCreate(projectRoot, socketId, options)
const project = open_project.getProject()
if (!project) throw new Error('Missing project after openProjectCreate!')
const [config, _projectId] = await Promise.all([
project.getConfig(),
getProjectId(project, projectId),
])
return {
project,
config,
projectId: _projectId,
// Lazy require'd here, so as to not execute until we're in the electron process
configFile: require('@packages/data-context').getCtx().lifecycleManager.configFile,
}
}
const removeOldProfiles = (browser) => {
return browserUtils.removeOldProfiles(browser)
.catch((err) => {
// dont make removing old browsers profiles break the build
return errors.warning('CANNOT_REMOVE_OLD_BROWSER_PROFILES', err)
})
}
async function trashAssets (config: Cfg) {
if (config.trashAssetsBeforeRuns !== true) {
return
}
try {
await Promise.all([
trash.folder(config.videosFolder),
trash.folder(config.screenshotsFolder),
trash.folder(config.downloadsFolder),
])
} catch (err) {
// dont make trashing assets fail the build
errors.warning('CANNOT_TRASH_ASSETS', err)
}
}
async function startVideoRecording (options: { previous?: VideoRecording, project: Project, spec: SpecWithRelativeRoot, videosFolder: string }): Promise<VideoRecording> {
if (!options.videosFolder) throw new Error('Missing videoFolder for recording')
function videoPath (suffix: string) {
return path.join(options.videosFolder, options.spec.relativeToCommonRoot + suffix)
}
const videoName = videoPath('.mp4')
const compressedVideoName = videoPath('-compressed.mp4')
const outputDir = path.dirname(videoName)
const onError = _.once((err) => {
// catch video recording failures and log them out
// but don't let this affect the run at all
errors.warning('VIDEO_RECORDING_FAILED', err)
return undefined
})
try {
await fs.ensureDir(outputDir)
} catch (err) {
onError(err)
}
if (options.previous) {
debug('in single-tab mode, re-using previous videoController')
Object.assign(options.previous.api, {
videoName,
compressedVideoName,
onError,
})
await options.previous.controller?.restart().catch(onError)
return options.previous
}
let ffmpegController: BrowserVideoController
let _ffmpegOpts: Pick<videoCapture.StartOptions, 'webmInput'>
const videoRecording: VideoRecording = {
api: {
onError,
videoName,
compressedVideoName,
async useFfmpegVideoController (ffmpegOpts) {
_ffmpegOpts = ffmpegOpts || _ffmpegOpts
ffmpegController = await videoCapture.start({ ...videoRecording.api, ..._ffmpegOpts })
// This wrapper enables re-binding writeVideoFrame to a new video stream when running in single-tab mode.
const controllerWrap: BrowserVideoController = {
...ffmpegController,
writeVideoFrame: function writeVideoFrameWrap (data) {
if (!ffmpegController) throw new Error('missing ffmpegController in writeVideoFrameWrap')
ffmpegController.writeVideoFrame(data)
},
async restart () {
await videoRecording.api.useFfmpegVideoController(_ffmpegOpts)
},
}
videoRecording.api.useVideoController(controllerWrap)
return controllerWrap
},
useVideoController (videoController) {
debug('setting videoController for videoRecording %o', videoRecording)
videoRecording.controller = videoController
},
onProjectCaptureVideoFrames (fn) {
options.project.on('capture:video:frames', fn)
},
},
controller: undefined,
}
options.project.videoRecording = videoRecording
debug('created videoRecording %o', { videoRecording })
return videoRecording
}
const warnVideoCaptureFailed = (err) => {
// log that capturing video was attempted
// but failed and don't let this change the run exit code
errors.warning('VIDEO_CAPTURE_FAILED', err)
}
const warnVideoCompressionFailed = (err) => {
// log that compression was attempted
// but failed and don't let this change the run exit code
errors.warning('VIDEO_COMPRESSION_FAILED', err)
}
async function compressRecording (options: { quiet: boolean, videoCompression: number | boolean, processOptions: Omit<ProcessOptions, 'videoCompression'> }) {
debug('ending the video recording %o', options)
// once this ended promises resolves
// then begin compressing the file
// don't compress anything if videoCompress is off
// or we've been told not to upload the video
if (options.videoCompression === false || options.videoCompression === 0) {
debug('skipping compression')
return
}
// if a user passes in videoCompression='true' into their config, coerce the value
// to the default CRF value which is 32
if (options.videoCompression === true) {
debug('coercing compression to 32 CRF')
options.videoCompression = 32
}
const processOptions: ProcessOptions = {
...options.processOptions,
videoCompression: Number(options.videoCompression),
}
function continueWithCompression (onProgress?: (progress: number) => void) {
return videoCapture.compress({ ...processOptions, onProgress })
}
if (options.quiet) {
return continueWithCompression()
}
const { onProgress } = printResults.displayVideoCompressionProgress(processOptions)
return continueWithCompression(onProgress)
}
function launchBrowser (options: { browser: Browser, spec: SpecWithRelativeRoot, setScreenshotMetadata: SetScreenshotMetadata, screenshots: ScreenshotMetadata[], projectRoot: string, shouldLaunchNewTab: boolean, onError: (err: Error) => void, videoRecording?: VideoRecording, protocolManager?: ProtocolManager }) {
const { browser, spec, setScreenshotMetadata, screenshots, projectRoot, shouldLaunchNewTab, onError, protocolManager } = options
const warnings = {}
const browserOpts: OpenProjectLaunchOpts = {
protocolManager,
projectRoot,
shouldLaunchNewTab,
onError,
videoApi: options.videoRecording?.api,
automationMiddleware: {
onBeforeRequest (message, data) {
if (message === 'take:screenshot') {
return setScreenshotMetadata(data)
}
},
onAfterResponse: (message, data, resp) => {
if (message === 'take:screenshot' && resp) {
const existingScreenshot = _.findIndex(screenshots, { path: resp.path })
if (existingScreenshot !== -1) {
// NOTE: saving screenshots to the same path will overwrite the previous one
// so we shouldn't report more screenshots than exist on disk.
// this happens when cy.screenshot is used in a retried test
screenshots.splice(existingScreenshot, 1, screenshotMetadata(data, resp))
} else {
screenshots.push(screenshotMetadata(data, resp))
}
}
return resp
},
},
onWarning: (err) => {
const { message } = err
// if this warning has already been
// seen for this browser launch then
// suppress it
if (warnings[message]) return
warnings[message] = err
},
}
return openProject.launch(browser, spec, browserOpts)
}
async function listenForProjectEnd (project: ProjectBase, exit: boolean): Promise<any> {
if (globalThis.CY_TEST_MOCK?.listenForProjectEnd) return Bluebird.resolve(globalThis.CY_TEST_MOCK.listenForProjectEnd)
// if exit is false, we need to intercept the resolution of tests - whether
// an early exit with intermediate results, or a full run.
return new Promise((resolve, reject) => {
Promise.race([
new Promise((res) => {
project.once('end', (results) => {
debug('project ended with results %O', results)
// If the project ends and the spec is skipped, treat the run as cancelled
// as we do not want to update the dev server unnecessarily for experimentalJustInTimeCompile.
if (results?.skippedSpec) {
isRunCancelled = true
}
res(results)
})
}),
earlyExitTerminator.waitForEarlyExit(project, exit),
]).then((results) => {
if (exit === false) {
// eslint-disable-next-line no-console
console.log('not exiting due to options.exit being false')
} else {
resolve(results)
}
}).catch((err) => {
reject(err)
})
})
}
async function waitForBrowserToConnect (options: { project: Project, socketId: string, onError: (err: Error) => void, spec: SpecWithRelativeRoot, isFirstSpecInBrowser: boolean, testingType: string, experimentalSingleTabRunMode: boolean, browser: Browser, screenshots: ScreenshotMetadata[], projectRoot: string, shouldLaunchNewTab: boolean, webSecurity: boolean, videoRecording?: VideoRecording, protocolManager?: ProtocolManager }) {
if (globalThis.CY_TEST_MOCK?.waitForBrowserToConnect) return Promise.resolve()
const { project, socketId, onError, spec, browser, protocolManager } = options
const browserTimeout = Number(process.env.CYPRESS_INTERNAL_BROWSER_CONNECT_TIMEOUT || 60000)
let browserLaunchAttempt = 1
// without this the run mode is only setting new spec
// path for next spec in launch browser.
// we need it to run on every spec even in single browser mode
currentSetScreenshotMetadata = (data) => {
data.specName = spec.relativeToCommonRoot
return data
}
// TODO: remove the need to extend options and avoid the type error
// @ts-expect-error
options.setScreenshotMetadata = (data) => {
return currentSetScreenshotMetadata(data)
}
if (options.experimentalSingleTabRunMode && options.testingType === 'component' && !options.isFirstSpecInBrowser) {
// reset browser state to match default behavior when opening/closing a new tab
await openProject.resetBrowserState()
// Send the new telemetry context to the browser to set the parent/child relationship appropriately for tests
if (telemetry.isEnabled()) {
openProject.updateTelemetryContext(JSON.stringify(telemetry.getActiveContextObject()))
}
// since we aren't going to be opening a new tab,
// we need to tell the protocol manager to reconnect to the existing browser
if (protocolManager) {
await openProject.connectProtocolToBrowser({ browser, foundBrowsers: project.options.browsers, protocolManager })
}
// since we aren't re-launching the browser, we have to navigate to the next spec instead
debug('navigating to next spec %s', createPublicSpec(spec))
return openProject.changeUrlToSpec(spec)
}
const wait = async () => {
telemetry.startSpan({ name: `waitForBrowserToConnect:attempt:${browserLaunchAttempt}` })
debug('waiting for socket to connect and browser to launch...')
const coreData = require('@packages/data-context').getCtx().coreData
if (coreData.didBrowserPreviouslyHaveUnexpectedExit) {
debug(`browser previously exited. Setting shouldLaunchNewTab=false to recreate the correct browser automation clients.`)
options.shouldLaunchNewTab = false
coreData.didBrowserPreviouslyHaveUnexpectedExit = false
}
async function retryOnError (err: CypressError) {
telemetry.getSpan(`waitForBrowserToConnect:attempt:${browserLaunchAttempt}`)?.end()
console.log('')
// always first close the open browsers
// before retrying or dieing
await openProject.closeBrowser()
if (browserLaunchAttempt === 1 || browserLaunchAttempt === 2) {
// try again up to 3 attempts
const word = browserLaunchAttempt === 1 ? 'Retrying...' : 'Retrying again...'
if (CDPFailedToStartFirefox.isCDPFailedToStartFirefoxError(err?.originalError)) {
errors.warning('FIREFOX_CDP_FAILED_TO_CONNECT', word)
} else {
errors.warning('TESTS_DID_NOT_START_RETRYING', word)
}
browserLaunchAttempt += 1
return await wait()
}
err = errors.get('TESTS_DID_NOT_START_FAILED')
errors.log(err)
onError(err)
}
return Bluebird.all([
waitForSocketConnection(project, socketId),
// TODO: remove the need to extend options and coerce this type
launchBrowser(options as typeof options & { setScreenshotMetadata: SetScreenshotMetadata }),
]).catch((e: CypressError) => {
// if the error wrapped is a CDPFailedToStartFirefox, try to relaunch the browser
if (CDPFailedToStartFirefox.isCDPFailedToStartFirefoxError(e?.originalError)) {
// if CDP fails to connect, which is ultimately out of our control and in the hands of webdriver
// we retry launching the browser in the hopes the session is spawned correctly
debug(`Caught in launchBrowser: ${e.details}`)
return retryOnError(e)
}
// otherwise, fail
throw e
})
.timeout(browserTimeout)
.then(() => {
telemetry.getSpan(`waitForBrowserToConnect:attempt:${browserLaunchAttempt}`)?.end()
})
.catch(Bluebird.TimeoutError, async (err) => {
debug('Catch on waitForBrowserToConnect')
return retryOnError(err as CypressError)
})
}
return wait()
}
function waitForSocketConnection (project: Project, id: string) {
if (globalThis.CY_TEST_MOCK?.waitForSocketConnection) return
debug('waiting for socket connection... %o', { id })
return new Promise<void>((resolve, reject) => {
const fn = function (socketId) {
debug('got socket connection %o', { id: socketId })
if (socketId === id) {
// remove the event listener if we've connected
project.removeListener('socket:connected', fn)
debug('socket connected', { socketId })
// resolve the promise
return resolve()
}
}
// when a socket connects verify this
// is the one that matches our id!
return project.on('socket:connected', fn)
})
}
async function waitForTestsToFinishRunning (options: { project: Project, screenshots: ScreenshotMetadata[], videoCompression: number | boolean, exit: boolean, spec: SpecWithRelativeRoot, estimated: number, quiet: boolean, config: Cfg, shouldKeepTabOpen: boolean, isLastSpec: boolean, testingType: TestingType, videoRecording?: VideoRecording, protocolManager?: ProtocolManager }) {
if (globalThis.CY_TEST_MOCK?.waitForTestsToFinishRunning) return Promise.resolve(globalThis.CY_TEST_MOCK.waitForTestsToFinishRunning)
const { project, screenshots, videoRecording, videoCompression, exit, spec, estimated, quiet, config, shouldKeepTabOpen, isLastSpec, testingType, protocolManager } = options
const results = await listenForProjectEnd(project, exit)
debug('received project end')
_.defaults(results, {
error: null,
hooks: null,
tests: null,
video: null,
screenshots: null,
reporterStats: null,
})
// Cypress Cloud told us to skip this spec
const skippedSpec = results.skippedSpec
// https://github.com/cypress-io/cypress/issues/2370
// delay 1 second if we're recording a video to give
// the browser padding to render the final frames
// to avoid chopping off the end of the video
const videoController = videoRecording?.controller
debug('received videoController %o', { videoController })
if (videoController && !skippedSpec) {
const span = telemetry.startSpan({ name: 'video:capture:delayToLetFinish' })
debug('delaying to extend video %o', { DELAY_TO_LET_VIDEO_FINISH_MS })
await Bluebird.delay(DELAY_TO_LET_VIDEO_FINISH_MS)
span?.end()
}
if (screenshots) {
results.screenshots = screenshots
}
results.spec = spec
const { tests } = results
const attempts = _.flatMap(tests, (test) => test.attempts)
let videoCaptureFailed = false
// if we have a video recording
if (videoController) {
results.video = videoRecording!.api.videoName
if (tests && tests.length) {
// always set the video timestamp on tests
Reporter.setVideoTimestamp(videoController.startedVideoCapture, attempts)
}
try {
await videoController.endVideoCapture(!skippedSpec)
debug('ended video capture')
} catch (err) {
videoCaptureFailed = true
warnVideoCaptureFailed(err)
}
telemetry.getSpan('video:capture')?.setAttributes({ videoCaptureFailed })?.end()
}
const afterSpecSpan = telemetry.startSpan({ name: 'lifecycle:after:spec' })
const [publicSpec, publicResults] = createPublicSpecResults(spec, results)
debug('spec results: %o', publicResults)
debug('execute after:spec')
await runEvents.execute('after:spec', publicSpec, publicResults)
afterSpecSpan?.end()
await protocolManager?.afterSpec()
const videoName = videoRecording?.api.videoName
const videoExists = videoName && await fs.pathExists(videoName)
if (!videoExists) {
// the video file no longer exists at the path where we expect it,
// possibly because the user deleted it in the after:spec event
debug(`No video found after spec ran - skipping compression. Video path: ${videoName}`)
results.video = null
}
if (!quiet && !skippedSpec) {
printResults.displayResults(results, estimated)
}
// @ts-expect-error experimentalSingleTabRunMode only exists on the CT-specific config type
const usingExperimentalSingleTabMode = testingType === 'component' && config.experimentalSingleTabRunMode
if (usingExperimentalSingleTabMode && !isLastSpec) {
await project.server.destroyAut()
}
// we do not support experimentalSingleTabRunMode for e2e. We always want to close the tab on the last spec to ensure that things get cleaned up properly at the end of the run
if (!usingExperimentalSingleTabMode || isLastSpec) {
debug('attempting to close the browser tab')
await openProject.resetBrowserTabsForNextSpec(shouldKeepTabOpen)
debug('resetting server state')
project.server.reset()
}
let videoCompressionFailed = false
if (videoExists && !skippedSpec && !videoCaptureFailed) {
const span = telemetry.startSpan({ name: 'video:compression' })
const chaptersConfig = videoCapture.generateFfmpegChaptersConfig(results.tests)
printResults.printVideoHeader()
try {
debug('compressing recording')
span?.setAttributes({
videoName,
videoCompressionString: videoCompression.toString(),
compressedVideoName: videoRecording.api.compressedVideoName,
})
await compressRecording({
quiet,
videoCompression,
processOptions: {
compressedVideoName: videoRecording.api.compressedVideoName,
videoName,
chaptersConfig,
...(videoRecording.controller!.postProcessFfmpegOptions || {}),
},
})
} catch (err) {
videoCompressionFailed = true
warnVideoCompressionFailed(err)
}
span?.end()
}
// only fail to print the video if capturing the video fails.
// otherwise, print the video path to the console if it exists regardless of whether compression fails or not
if (!videoCaptureFailed && videoExists) {
printResults.printVideoPath(videoName)
}
if (videoCaptureFailed || videoCompressionFailed) {
results.video = null
}
// the early exit terminator persists between specs,
// so if this spec crashed, the next one will report as
// a crash too unless it is reset. Would like to not rely
// on closure, but threading through fn props via options is also not
// great.
earlyExitTerminator = new EarlyExitTerminator()
return results
}
function screenshotMetadata (data, resp) {
return {
screenshotId: random.id(),
name: data.name || null,
testId: data.testId,
testAttemptIndex: data.testAttemptIndex,
takenAt: resp.takenAt,
path: resp.path,
height: resp.dimensions.height,
width: resp.dimensions.width,
pathname: undefined as string | undefined,
}
}
async function runSpecs (options: { config: Cfg, browser: Browser, sys: any, headed: boolean, outputPath: string, specs: SpecWithRelativeRoot[], specPattern: string | RegExp | string[], beforeSpecRun?: BeforeSpecRun, afterSpecRun?: AfterSpecRun, runUrl?: string, parallel?: boolean, group?: string, tag?: string, autoCancelAfterFailures?: number | false, testingType: TestingType, quiet: boolean, project: Project, onError: (err: Error) => void, exit: boolean, socketId: string, webSecurity: boolean, projectRoot: string, protocolManager?: ProtocolManager } & Pick<Cfg, 'video' | 'videoCompression' | 'videosFolder'>) {
if (globalThis.CY_TEST_MOCK?.runSpecs) return globalThis.CY_TEST_MOCK.runSpecs
const { config, browser, sys, headed, outputPath, specs, specPattern, beforeSpecRun, afterSpecRun, runUrl, parallel, group, tag, autoCancelAfterFailures, protocolManager } = options
const isHeadless = !headed
browser.isHeadless = isHeadless
browser.isHeaded = !isHeadless
if (!options.quiet) {
printResults.displayRunStarting({
config,
specs,
group,
tag,
runUrl,
browser,
parallel,
specPattern,
autoCancelAfterFailures,
})
}
let isFirstSpecInBrowser = true
async function runEachSpec (spec: SpecWithRelativeRoot, index: number, length: number, estimated: number, instanceId: string) {
const span = telemetry.startSpan({
name: 'run:spec',
active: true,
})
span?.setAttributes({
specName: spec.name,
type: spec.specType,
firstSpec: isFirstSpecInBrowser,
})
await protocolManager?.beforeSpec({
...spec,
instanceId,
})
if (!options.quiet) {
printResults.displaySpecHeader(spec.relativeToCommonRoot, index + 1, length, estimated)
}
const isExperimentalJustInTimeCompile = options.testingType === 'component' && config.experimentalJustInTimeCompile
// Only update the dev server if the run is not cancelled
if (isExperimentalJustInTimeCompile) {
if (isRunCancelled) {
// TODO: this logic to skip updating the dev-server on cancel needs a system-test before the feature goes generally available.
debug(`isExperimentalJustInTimeCompile=true and run is cancelled. Not updating dev server with spec ${spec.absolute}.`)
} else {
const ctx = require('@packages/data-context').getCtx()
// If in run mode, we need to update the dev server with our spec.
// in open mode, this happens in the browser through the web socket, but we do it here in run mode
// to try and have it happen as early as possible to make the test run as fast as possible
await ctx._apis.projectApi.getDevServer().updateSpecs([spec])
}
}
const { results } = await runSpec(config, spec, options, estimated, isFirstSpecInBrowser, index === length - 1)
if (results?.error?.includes('We detected that the Chrome process just crashed with code')) {
// If the browser has crashed, make sure isFirstSpecInBrowser is set to true as the browser will be relaunching
isFirstSpecInBrowser = true
} else {
isFirstSpecInBrowser = false
}
span?.end()
return results
}
const beforeRunDetails = {
browser,
config,
cypressVersion: pkg.version,
group,
parallel,
runUrl,
specs,
specPattern,
system: _.pick(sys, 'osName', 'osVersion'),
tag,
autoCancelAfterFailures,
}
const runSpan = telemetry.startSpan({ name: 'run' })
runSpan?.setAttributes({
recordEnabled: !!runUrl,
...(runUrl && {
recordOpts: JSON.stringify({
runUrl,
parallel,
group,
tag,
autoCancelAfterFailures,
}),
}),
})
const beforeRunSpan = telemetry.startSpan({ name: 'lifecycle:before:run' })
await runEvents.execute('before:run', beforeRunDetails)
beforeRunSpan?.end()
const runs = await iterateThroughSpecs({
specs,
config,
runEachSpec,
afterSpecRun,
beforeSpecRun,
})
const results: CypressRunResult = {
status: 'finished',
startedTestsAt: getRun(_.first(runs), 'stats.wallClockStartedAt'),
endedTestsAt: getRun(_.last(runs), 'stats.wallClockEndedAt'),
totalDuration: sumByProp(runs, 'stats.wallClockDuration'),
totalSuites: sumByProp(runs, 'stats.suites'),
totalTests: sumByProp(runs, 'stats.tests'),
totalPassed: sumByProp(runs, 'stats.passes'),
totalPending: sumByProp(runs, 'stats.pending'),
totalFailed: sumByProp(runs, 'stats.failures'),
totalSkipped: sumByProp(runs, 'stats.skipped'),
runs,
browserPath: browser.path,
browserName: browser.name,
browserVersion: browser.version,
osName: sys.osName,
osVersion: sys.osVersion,
cypressVersion: pkg.version,
runUrl,
config,
}
const afterRunSpan = telemetry.startSpan({ name: 'lifecycle:after:run' })
const publicResults = createPublicRunResults(results)
debug('final results of all runs: %o', publicResults)
await runEvents.execute('after:run', publicResults)
afterRunSpan?.end()
await writeOutput(outputPath, publicResults)
runSpan?.end()
return results
}
async function runSpec (config, spec: SpecWithRelativeRoot, options: { project: Project, browser: Browser, onError: (err: Error) => void, config: Cfg, quiet: boolean, exit: boolean, testingType: TestingType, socketId: string, webSecurity: boolean, projectRoot: string, protocolManager?: ProtocolManager } & Pick<Cfg, 'video' | 'videosFolder' | 'videoCompression'>, estimated, isFirstSpecInBrowser, isLastSpec) {
const { project, browser, onError } = options
const { isHeadless } = browser
debug('about to run spec %o', {
spec: createPublicSpec(spec),
isHeadless,
browser: createPublicBrowser(browser),
})
if (browser.family !== 'chromium' && !options.config.chromeWebSecurity) {
console.log('')
errors.warning('CHROME_WEB_SECURITY_NOT_SUPPORTED', browser.family)
}
const screenshots = []
async function getVideoRecording () {
if (!options.video) return undefined
const opts = { project, spec, videosFolder: options.videosFolder }
telemetry.startSpan({ name: 'video:capture' })
if (config.experimentalSingleTabRunMode && !isFirstSpecInBrowser && project.videoRecording) {
// in single-tab mode, only the first spec needs to create a videoRecording object
// which is then re-used between specs
return await startVideoRecording({ ...opts, previous: project.videoRecording })
}
return await startVideoRecording(opts)
}
const videoRecording = await getVideoRecording()
// we know we're done running headlessly
// when the renderer has connected and
// finishes running all of the tests.
const [results] = await Promise.all([
waitForTestsToFinishRunning({
spec,
config,
project,
estimated,
screenshots,
videoRecording,
exit: options.exit,
testingType: options.testingType,
videoCompression: options.videoCompression,
quiet: options.quiet,
shouldKeepTabOpen: !isLastSpec,
isLastSpec,
protocolManager: options.protocolManager,
}),
waitForBrowserToConnect({
spec,
project,
browser,
screenshots,
onError: (...args) => {
debug('onError from runSpec')
onError(...args)
},
videoRecording,
socketId: options.socketId,
webSecurity: options.webSecurity,
projectRoot: options.projectRoot,
testingType: options.testingType,
isFirstSpecInBrowser,
experimentalSingleTabRunMode: config.experimentalSingleTabRunMode,
shouldLaunchNewTab: !isFirstSpecInBrowser,
protocolManager: options.protocolManager,