Debian Codegraph @

tests/selenium/wdio-mediawiki/index.js

raw symbols SCIP
1 'use strict';
2
3 const fs = require( 'fs' );
4
5 /**
6 * @since 1.1.0
7 * @return {string} File name friendly version of ISO 8601 date and time
8 */
9 function makeFilenameDate() {
10 return new Date().toISOString().replace( /[:.]/g, '-' );
11 }
12
13 /**
14 * @since 1.1.0
15 * @param {string} title Test title
16 * @return {string} File name friendly version of the test title
17 */
18 function testTitle( title ) {
19 return encodeURIComponent( title.replace( /\s+/g, '-' ) );
20 }
21
22 /**
23 * @since 1.1.0
24 * @param {string} title Test title
25 * @param {string} extension png for screenshots, mp4 for videos
26 * @return {string} Full path of screenshot/video file
27 */
28 function filePath( title, extension ) {
29 return `${ browser.config.screenshotPath }/${ testTitle( title ) }-${ makeFilenameDate() }.${ extension }`;
30 }
31
32 /**
33 * Based on <https://github.com/webdriverio/webdriverio/issues/269#issuecomment-306342170>
34 *
35 * @since 1.0.0
36 * @param {string} title Description (will be sanitised and used as file name)
37 * @return {string} File path
38 */
39 async function saveScreenshot( title ) {
40 // Create sensible file name for current test title
41 const path = filePath( title, 'png' );
42 // Ensure directory exists, based on WebDriverIO#saveScreenshotSync()
43 try {
44 // eslint-disable-next-line security/detect-non-literal-fs-filename
45 fs.statSync( browser.config.screenshotPath );
46 } catch ( err ) {
47 // eslint-disable-next-line security/detect-non-literal-fs-filename
48 fs.mkdirSync( browser.config.screenshotPath );
49 }
50 // Create and save screenshot
51 await browser.saveScreenshot( path );
52 return path;
53 }
54
55 /**
56 * @since 1.1.0
57 * @param {Object} ffmpeg
58 * @param {string} title Test title
59 * @return {Object} ffmpeg object is returned so it could be used in stopVideo()
60 */
61 function startVideo( ffmpeg, title ) {
62 if ( process.env.DISPLAY && process.env.DISPLAY.startsWith( ':' ) ) {
63 const videoPath = filePath( title, 'mp4' );
64 const { spawn } = require( 'child_process' );
65 ffmpeg = spawn( 'ffmpeg', [
66 '-f', 'x11grab', // grab the X11 display
67 '-video_size', '1280x1024', // video size
68 '-i', process.env.DISPLAY, // input file url
69 '-loglevel', 'error', // log only errors
70 '-y', // overwrite output files without asking
71 '-pix_fmt', 'yuv420p', // QuickTime Player support, "Use -pix_fmt yuv420p for compatibility with outdated media players"
72 videoPath // output file
73 ] );
74 const logBuffer = function ( buffer, prefix ) {
75 const lines = buffer.toString().trim().split( '\n' );
76 lines.forEach( ( line ) => {
77 console.log( prefix + line );
78 } );
79 };
80 ffmpeg.stdout.on( 'data', ( data ) => {
81 logBuffer( data, 'ffmpeg stdout: ' );
82 } );
83 ffmpeg.stderr.on( 'data', ( data ) => {
84 logBuffer( data, 'ffmpeg stderr: ' );
85 } );
86 }
87 return ffmpeg;
88 }
89
90 /**
91 * @since 1.1.0
92 * @param {Object} ffmpeg
93 */
94 function stopVideo( ffmpeg ) {
95 if ( ffmpeg ) {
96 ffmpeg.kill( 'SIGINT' );
97 }
98 }
99
100 module.exports = {
101 makeFilenameDate,
102 saveScreenshot,
103 startVideo,
104 stopVideo
105 };