import osName from 'os-name' ; import { IterationResult , TestResult , TestScriptResult , ScriptWithError , } from '@flood/element-report' ; import { ElementOptions } from './ElementOption' ; import { join } from 'path' ; import findRoot from 'find-root' ; export class ElementResult { private _result : TestResult ; constructor ( ) { const pkg = require ( join ( findRoot ( __dirname ) , 'package.json' ) ) ; this . _result = { testScripts : [ ] , executionInfo : { time : new Date ( ) . toLocaleString ( undefined , { day : '2-digit' , month : 'short' , year : 'numeric' , hour : '2-digit' , minute : '2-digit' , second : '2-digit' , } ) , mode : '' , os : osName ( ) , elementVersion : pkg . version , nodeVersion : process . version , } , scriptsWithError : [ ] , } ; } addExecutionInfo ( opt , isConfig ) { let modeName = opt . headless ? 'headless' : 'no-headless' ; modeName = isConfig ? `${ modeName } with config file` : modeName ; this . _result . executionInfo . mode = modeName ; } addTestScript ( file , iterationResults ) { let duration = 0 ; iterationResults . forEach ( ( item ) => ( duration += item . duration ?? 0 ) ) ; const testScript = { name : file , iterationResults , duration , } ; this . _result . testScripts . push ( testScript ) ; this . summarizeResult ( ) ; } addScriptWithError ( script ) { this . _result . scriptsWithError . push ( script ) ; } summarizeResult ( ) { let duration = 0 ; this . _result . testScripts . forEach ( ( item ) => ( duration += item . duration ?? 0 ) ) ; this . _result . executionInfo . duration = duration ; } getResult ( ) { return this . _result ; } }