import * as fs from 'fs' ; import * as mkdirp from 'mkdirp' ; import * as path from 'path' ; import { SpectronApplication } from '../spectron/application' ; import { sanitize } from './utilities' ; const TEST_TIME = new Date ( ) . toISOString ( ) ; export class Screenshot { private counter = 0 ; private screenshotsDirPath : string ; constructor ( private spectron , private testName , testRetry ) { this . screenshotsDirPath = path . join ( 'test_data' , 'screenshots' , sanitize ( TEST_TIME ) ) ; } public async capture ( name ? ) { const screenshotPath = path . join ( this . screenshotsDirPath , sanitize ( this . testName ) , name !== undefined ? `${ this . counter ++ }-${ sanitize ( name ) }.png` : `${ this . counter ++ }.png` ) ; const image = await this . spectron . app . browserWindow . capturePage ( ) . catch ( err => { console . log ( 'Error creating test snapshot' ) ; } ) ; await new Promise ( ( c , e ) => mkdirp ( path . dirname ( screenshotPath ) , err => ( err ? e ( err ) : c ( ) ) ) ) ; await new Promise ( ( c , e ) => fs . writeFile ( screenshotPath , image , err => ( err ? e ( err ) : c ( ) ) ) ) ; } }