| 1 |
'use strict'; |
| 2 |
|
| 3 |
const fs = require( 'fs' ); |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
function makeFilenameDate() { |
| 10 |
return new Date().toISOString().replace( /[:.]/g, '-' ); |
| 11 |
} |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
function testTitle( title ) { |
| 19 |
return encodeURIComponent( title.replace( /\s+/g, '-' ) ); |
| 20 |
} |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
function filePath( title, extension ) { |
| 29 |
return `${ browser.config.screenshotPath }/${ testTitle( title ) }-${ makeFilenameDate() }.${ extension }`; |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
async function saveScreenshot( title ) { |
| 40 |
|
| 41 |
const path = filePath( title, 'png' ); |
| 42 |
|
| 43 |
try { |
| 44 |
|
| 45 |
fs.statSync( browser.config.screenshotPath ); |
| 46 |
} catch ( err ) { |
| 47 |
|
| 48 |
fs.mkdirSync( browser.config.screenshotPath ); |
| 49 |
} |
| 50 |
|
| 51 |
await browser.saveScreenshot( path ); |
| 52 |
return path; |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 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', |
| 67 |
'-video_size', '1280x1024', |
| 68 |
'-i', process.env.DISPLAY, |
| 69 |
'-loglevel', 'error', |
| 70 |
'-y', |
| 71 |
'-pix_fmt', 'yuv420p', |
| 72 |
videoPath |
| 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 |
|
| 92 |
|
| 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 |
}; |