@@ -3,23 +3,47 @@ import { TestMetadata } from '@bellatrix/core/test/props';
3
3
import { ServiceLocator } from '@bellatrix/core/utilities' ;
4
4
import { Image } from '@bellatrix/core/image' ;
5
5
import { App } from '@bellatrix/web/infrastructure' ;
6
- import * as fs from 'fs' ;
7
- import * as path from 'path' ;
6
+ import { existsSync , mkdirSync , writeFileSync } from 'fs' ;
7
+ import { dirname , extname , join } from 'path' ;
8
+ import { BellatrixSettings } from '@bellatrix/core/settings' ;
8
9
9
10
export class ScreenshotOnFailPlugin extends Plugin {
10
11
override async preAfterTest ( metadata : TestMetadata ) : Promise < void > {
12
+ const pluginSettings = BellatrixSettings . get ( ) . screenshotOnFailPluginSettings ;
13
+
14
+ if ( ! pluginSettings ?. isPluginEnabled ) {
15
+ return ;
16
+ }
17
+
11
18
if ( ! metadata . error ) {
12
19
return ;
13
20
}
14
21
15
22
const app = ServiceLocator . resolve ( App ) ;
16
23
const screenshotImage = await app . browser . getScreenshot ( ) ;
17
- // Save the screenshot as an image file
24
+
25
+ const outputPath = pluginSettings ?. outputPath ;
26
+
27
+ if ( ! outputPath ) {
28
+ console . error ( 'Output path for screenshots is not defined in the configuration.' ) ;
29
+ return ;
30
+ }
31
+
18
32
try {
19
- const savePath = this . saveImageToFile ( screenshotImage , '../reports/screenshots/' + metadata . testName ) ; // TODO: take from config
33
+ const projectRoot = process . env [ 'BELLATRIX_CONFIGURAITON_ROOT' ] ! ; // TODO: find a better way to get the project root
34
+ const pathArray = [ projectRoot , outputPath ] ;
35
+ if ( pluginSettings ?. shouldCreateFolderPerSuite ) {
36
+ pathArray . push ( metadata . suiteName ) ;
37
+ }
38
+ pathArray . push ( metadata . testName ) ;
39
+ const savePath = this . saveImageToFile ( screenshotImage , join ( ...pathArray ) ) ;
20
40
console . info ( '\n Screenshot for failed test ' + metadata . testName + ': ' + savePath + '\n' ) ;
21
41
} catch ( error ) {
22
- console . error ( 'Error saving screenshot:' , ( error as Error ) . message ) ;
42
+ if ( error instanceof Error ) {
43
+ console . error ( 'Error saving screenshot:' , error . message ) ;
44
+ } else {
45
+ console . error ( 'Error saving screenshot' ) ;
46
+ }
23
47
}
24
48
}
25
49
@@ -29,16 +53,28 @@ export class ScreenshotOnFailPlugin extends Plugin {
29
53
* @param outputPath - The path to save the image file
30
54
*/
31
55
private saveImageToFile ( image : Image , outputPath : string ) : string {
32
- const outputDir = path . dirname ( outputPath ) ;
33
- if ( ! fs . existsSync ( outputDir ) ) {
34
- fs . mkdirSync ( outputDir , { recursive : true } ) ;
56
+ const outputDir = dirname ( outputPath ) ;
57
+ if ( ! existsSync ( outputDir ) ) {
58
+ mkdirSync ( outputDir , { recursive : true } ) ;
35
59
}
36
60
37
- const outputFilePath = path . extname ( outputPath ) ? outputPath : `${ outputPath } .${ image . type } ` ;
61
+ const outputFilePath = extname ( outputPath ) ? outputPath : `${ outputPath } .${ image . type } ` ;
38
62
39
63
const binaryData = image . buffer ;
40
- const arrayBufferView : Uint8Array = new Uint8Array ( binaryData . buffer , binaryData . byteOffset , binaryData . length ) ;
41
- fs . writeFileSync ( outputFilePath , arrayBufferView ) ;
64
+ const arrayBufferView = new Uint8Array ( binaryData . buffer , binaryData . byteOffset , binaryData . length ) ;
65
+ writeFileSync ( outputFilePath , arrayBufferView ) ;
42
66
return outputFilePath ;
43
67
}
44
68
}
69
+
70
+ declare module '@bellatrix/core/types' {
71
+ interface BellatrixConfiguration {
72
+ screenshotOnFailPluginSettings ?: ScreenshotOnFailPluginSettings ;
73
+ }
74
+ }
75
+
76
+ interface ScreenshotOnFailPluginSettings {
77
+ isPluginEnabled : boolean ;
78
+ outputPath : string ,
79
+ shouldCreateFolderPerSuite ?: boolean ,
80
+ }
0 commit comments