Skip to content

Commit 66737b9

Browse files
committed
updated screenshot on fail update
1 parent ef3e1bd commit 66737b9

File tree

2 files changed

+52
-11
lines changed

2 files changed

+52
-11
lines changed

@bellatrix/extras/src/plugins/ScreenshotOnFailPlugin.ts

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,47 @@ import { TestMetadata } from '@bellatrix/core/test/props';
33
import { ServiceLocator } from '@bellatrix/core/utilities';
44
import { Image } from '@bellatrix/core/image';
55
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';
89

910
export class ScreenshotOnFailPlugin extends Plugin {
1011
override async preAfterTest(metadata: TestMetadata): Promise<void> {
12+
const pluginSettings = BellatrixSettings.get().screenshotOnFailPluginSettings;
13+
14+
if (!pluginSettings?.isPluginEnabled) {
15+
return;
16+
}
17+
1118
if (!metadata.error) {
1219
return;
1320
}
1421

1522
const app = ServiceLocator.resolve(App);
1623
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+
1832
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));
2040
console.info('\n Screenshot for failed test ' + metadata.testName + ': ' + savePath + '\n');
2141
} 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+
}
2347
}
2448
}
2549

@@ -29,16 +53,28 @@ export class ScreenshotOnFailPlugin extends Plugin {
2953
* @param outputPath - The path to save the image file
3054
*/
3155
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 });
3559
}
3660

37-
const outputFilePath = path.extname(outputPath) ? outputPath : `${outputPath}.${image.type}`;
61+
const outputFilePath = extname(outputPath) ? outputPath : `${outputPath}.${image.type}`;
3862

3963
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);
4266
return outputFilePath;
4367
}
4468
}
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+
}

example/bellatrix.config.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ const config: BellatrixConfigurationOverride = {
4343
browserName: 'chrome'
4444
}
4545
}
46+
},
47+
screenshotOnFailPluginSettings: {
48+
isPluginEnabled: true,
49+
outputPath: `./reports/screenshots${Date.now()}`,
50+
shouldCreateFolderPerSuite: true,
4651
}
4752
};
4853

0 commit comments

Comments
 (0)