diff --git a/src/video-recorder/index.js b/src/video-recorder/index.js index 76b39d51756..928f0aa2bb5 100644 --- a/src/video-recorder/index.js +++ b/src/video-recorder/index.js @@ -58,7 +58,12 @@ export default class VideoRecorder { } _assignEventHandlers (browserJob) { - browserJob.once('start', this._createSafeListener(this._onBrowserJobStart)); + browserJob.once('start', this._createSafeListener(() => { + this.tempDirectoryInitializedPromise = this._onBrowserJobStart(); + + return this.tempDirectoryInitializedPromise; + })); + browserJob.once('done', this._createSafeListener(this._onBrowserJobDone)); browserJob.on('test-run-create', this._createSafeListener(this._onTestRunCreate)); browserJob.on('test-run-ready', this._createSafeListener(this._onTestRunReady)); @@ -92,13 +97,15 @@ export default class VideoRecorder { return join(this.basePath, pathPattern.getPath()); } - _generateTempNames (id) { + async _generateTempNames (id) { const tempFileNames = { tempVideoPath: `${TEMP_VIDEO_FILE_PREFIX}-${id}.${VIDEO_EXTENSION}`, tempMergeConfigPath: `${TEMP_MERGE_CONFIG_FILE_PREFIX}-${id}.${TEMP_MERGE_CONFIG_FILE_EXTENSION}`, tmpMergeName: `${TEMP_MERGE_FILE_PREFIX}-${id}.${VIDEO_EXTENSION}` }; + await this.tempDirectoryInitializedPromise; + for (const [tempFile, tempName] of Object.entries(tempFileNames)) tempFileNames[tempFile] = join(this.tempDirectory.path, tempName); @@ -146,8 +153,7 @@ export default class VideoRecorder { this.testRunInfo[index] = testRunInfo; - testRunInfo.tempFiles = this._generateTempNames(connection.id); - + testRunInfo.tempFiles = await this._generateTempNames(connection.id); testRunInfo.videoRecorder = new VideoRecorderProcess(testRunInfo.tempFiles.tempVideoPath, this.ffmpegPath, connection, this.encodingOptions); diff --git a/test/server/video-recorder-test.js b/test/server/video-recorder-test.js index c32116801ce..73f3f928ede 100644 --- a/test/server/video-recorder-test.js +++ b/test/server/video-recorder-test.js @@ -5,6 +5,41 @@ const WarningLog = require('../../lib/notifications/warning-log'); const VIDEOS_BASE_PATH = '__videos__'; +class VideoRecorderMock extends VideoRecorder { + constructor (basePath, ffmpegPath, connection, customOptions) { + super(basePath, ffmpegPath, connection, customOptions); + + this.log = []; + } + + _generateTempNames (id) { + return super._generateTempNames(id) + .then(result => { + this.log.push('generate-names'); + + return result; + }); + } + + _onBrowserJobStart () { + this.log.push('job-start'); + + return super._onBrowserJobStart() + .then(() => { + this.log.push('temp-dir-initialized'); + }); + } + + _onTestRunCreate (options) { + this.log.push('test-created'); + + return super._onTestRunCreate(options) + .then(() => { + this.log.push('video-recorder-initialized'); + }); + } +} + describe('Video Recorder', () => { it('Should not start video recording for legacy tests', () => { const browserJobMock = new AsyncEmitter(); @@ -46,4 +81,40 @@ describe('Video Recorder', () => { 'The placeholders were replaced with an empty string.' ]); }); + + it('Should wait for Temp directory is initialized', () => { + const browserJobMock = new AsyncEmitter(); + const warningLog = new WarningLog(); + const videoRecorder = new VideoRecorderMock(browserJobMock, VIDEOS_BASE_PATH, {}, {}, warningLog); + + const testRunMock = { + testRun: { + browserConnection: { + id: 'connectionId', + provider: { + hasCustomActionForBrowser: () => { + return { + hasGetVideoFrameData: true + }; + } + } + } + } + }; + + browserJobMock.emit('start'); + + const testRunCreatePromise = browserJobMock.emit('test-run-create', testRunMock); + + browserJobMock.emit('done'); + + return testRunCreatePromise.then(() => { + expect(videoRecorder.log).eql([ + 'job-start', + 'test-created', + 'temp-dir-initialized', + 'generate-names' + ]); + }); + }); });