forked from webdriverio-community/wdio-video-reporter
-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
204 lines (175 loc) · 7.04 KB
/
index.js
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
import WdioReporter from '@wdio/reporter';
import allureReporter from '@wdio/allure-reporter';
import mkdirp from 'mkdirp';
import fs from 'fs-extra';
import path from 'path';
import { spawn } from 'child_process';
import { path as ffmpegPath} from '@ffmpeg-installer/ffmpeg';
import helpers from './helpers.js';
import config from './config.js';
import notAvailableImage from './assets/not-available.png';
export default class Video extends WdioReporter {
/**
* Set reporter options
*/
constructor (options) {
super(options);
// User options
// Wdio doesn't pass outputDir, but logFile which includes outputDir
config.outputDir = options.logFile ? path.dirname(options.logFile) : config.outputDir;
if(config.outputDir.length > 1) {
config.outputDir = config.outputDir.replace(/[\/|\\]$/, '');
}
config.saveAllVideos = options.saveAllVideos || config.saveAllVideos;
config.videoSlowdownMultiplier = options.videoSlowdownMultiplier || config.videoSlowdownMultiplier;
config.videoRenderTimeout = options.videoRenderTimeout || config.videoRenderTimeout;
// Debug
config.excludedActions.push(...(options.addExcludedActions || []));
config.jsonWireActions.push(...(options.addJsonWireActions || []));
this.videos = [];
this.ffmpegCommands = [];
this.testnameStructure = [];
this.testname = '';
this.frameNr = 0;
this.videos = [];
this.config = config;
helpers.setLogger(msg => this.write(msg));
}
/**
* Set wdio config options
*/
onRunnerStart (browser) {
const allureConfig = browser.config.reporters.filter(r => r === 'allure' || r[0] === 'allure').pop();
if (allureConfig && allureConfig[1] && allureConfig[1].outputDir) {
config.allureOutputDir = path.resolve(allureConfig[1].outputDir);
}
config.usingAllure = !!allureConfig;
const logLevel = browser.config.logLevel;
config.debugMode = logLevel.toLowerCase() === 'trace' || logLevel.toLowerCase() === 'debug';
this.write('Using reporter config:' + JSON.stringify(browser.config.reporters, undefined, 2) + '\n\n');
this.write('Using config:' + JSON.stringify(config, undefined, 2) + '\n\n\n');
}
/**
* Save screenshot or add not available image movie stills
*/
onAfterCommand (jsonWireMsg) {
const command = jsonWireMsg.endpoint.match(/[^\/]+$/);
const commandName = command ? command[0] : 'undefined';
helpers.debugLog('Incomming command: ' + jsonWireMsg.endpoint + ' => [' + commandName + ']\n');
// Filter out non-action commands and keep only last action command
if (config.excludedActions.includes(commandName) || !config.jsonWireActions.includes(commandName) || !this.recordingPath) {
return;
}
const filename = this.frameNr.toString().padStart(4, '0') + '.png';
const filePath = path.resolve(this.recordingPath, filename);
try {
browser.saveScreenshot(filePath);
helpers.debugLog('- Screenshot!!\n');
} catch (e) {
fs.writeFile(filePath, notAvailableImage, 'base64');
helpers.debugLog('- Screenshot not available...\n');
}
this.frameNr++;
}
/**
* Add suite name to naming structure
*/
onSuiteStart (suite) {
helpers.debugLog(`\n\n\n--- New suite: ${suite.title} ---\n`);
this.testnameStructure.push(suite.title.replace(/ /g, '-'));
}
/**
* Cleare suite name from naming structure
*/
onSuiteEnd () {
this.testnameStructure.pop();
}
/**
* Setup filename based on test name and prepare storage directory
*/
onTestStart (test) {
helpers.debugLog(`\n\n--- New test: ${test.title} ---\n`);
this.testnameStructure.push(test.title.replace(/ /g, '-'));
const fullname = this.testnameStructure.slice(1).reduce((cur,acc) => cur + '--' + acc, this.testnameStructure[0]);
let browserName = browser.capabilities.browserName.toUpperCase();
if (browser.capabilities.deviceType) {
browserName += `-${browser.capabilities.deviceType.replace(/ /g, '-')}`;
}
this.testname = helpers.generateFilename(browserName, fullname);
this.frameNr = 0;
this.recordingPath = path.resolve(config.outputDir, config.rawPath, this.testname);
mkdirp.sync(this.recordingPath);
}
/**
* Remove empty directories
*/
onTestSkip () {
if(this.recordingPath !== undefined) {
fs.removeSync(this.recordingPath);
}
}
/**
* Add attachment to Allue if applicable and start to generate the video
*/
onTestEnd (test) {
this.testnameStructure.pop();
if(config.usingAllure) {
if (browser.capabilities.deviceType) {
allureReporter.addArgument('deviceType', browser.capabilities.deviceType);
}
if (browser.capabilities.browserVersion) {
allureReporter.addArgument('browserVersion', browser.capabilities.browserVersion);
}
}
if (test.state === 'failed' || (test.state === 'passed' && config.saveAllVideos)) {
const filePath = path.resolve(this.recordingPath, this.frameNr.toString().padStart(4, '0') + '.png');
try {
browser.saveScreenshot(filePath);
helpers.debugLog('- Screenshot!!\n');
} catch (e) {
fs.writeFile(filePath, notAvailableImage, 'base64');
helpers.debugLog('- Screenshot not available...\n');
}
const videoPath = path.resolve(config.outputDir, this.testname + '.mp4');
this.videos.push(videoPath);
if (config.usingAllure) {
allureReporter.addAttachment('Execution video', videoPath, 'video/mp4');
}
const command = `"${ffmpegPath}" -y -r 10 -i "${this.recordingPath}/%04d.png" -vcodec libx264` +
` -crf 32 -pix_fmt yuv420p -vf "scale=1200:trunc(ow/a/2)*2","setpts=${config.videoSlowdownMultiplier}.0*PTS"` +
` "${path.resolve(config.outputDir, this.testname)}.mp4"`;
helpers.debugLog(`ffmpeg command: ${command}\n`);
this.ffmpegCommands.push(command);
}
}
/**
* Finalize report if using allure and clean up
*/
onRunnerEnd () {
try {
helpers.debugLog(`\n\n--- Awaiting videos ---\n`);
this.ffmpegCommands.forEach((cmd) => spawn(cmd, { stdio: 'ignore', shell: true}));
this.videos = helpers.waitForVideos(this.videos);
helpers.debugLog(`\n--- Videos are done ---\n\n`);
this.write('\nGenerated:' + JSON.stringify(this.videos, undefined, 2) + '\n\n');
if (config.usingAllure) {
helpers.debugLog(`--- Patching allure report ---\n`);
fs
.readdirSync(config.allureOutputDir)
.filter(line => line.includes('.mp4'))
.map(filename => path.resolve(config.allureOutputDir, filename))
.filter(allureFile => this.videos.includes(fs.readFileSync(allureFile).toString())) // Dont parse other browsers videos since they may not be ready
.forEach((filepath) => {
const videoFilePath = fs.readFileSync(filepath).toString();// The contents of the placeholder file is the video path
fs.copySync(videoFilePath, filepath);
});
}
this.write(`\n\nDone!\n`);
}
catch(e) {
this.write('Error during onRunnerEnd:');
this.write(e.message);
this.write(e.stack);
}
}
}