|
| 1 | +import { Plugin } from '@bellatrix/core/infrastructure'; |
| 2 | +import { TestMetadata } from '@bellatrix/core/test/props'; |
| 3 | +import { ServiceLocator } from '@bellatrix/core/utilities'; |
| 4 | +import { Image } from '@bellatrix/core/image'; |
| 5 | +import { App } from '@bellatrix/web/infrastructure'; |
| 6 | +import { existsSync, mkdirSync, writeFileSync } from 'fs'; |
| 7 | +import { dirname, extname, join } from 'path'; |
| 8 | +import { BellatrixSettings } from '@bellatrix/core/settings'; |
| 9 | + |
| 10 | +export class ScreenshotOnFailPlugin extends Plugin { |
| 11 | + override async preAfterTest(metadata: TestMetadata): Promise<void> { |
| 12 | + const pluginSettings = BellatrixSettings.get().screenshotOnFailPluginSettings; |
| 13 | + |
| 14 | + if (!pluginSettings?.isPluginEnabled) { |
| 15 | + return; |
| 16 | + } |
| 17 | + |
| 18 | + if (!metadata.error) { |
| 19 | + return; |
| 20 | + } |
| 21 | + |
| 22 | + const app = ServiceLocator.resolve(App); |
| 23 | + const screenshotImage = await app.browser.takeScreenshot(); |
| 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 | + |
| 32 | + try { |
| 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)); |
| 40 | + console.info('\n Screenshot for failed test ' + metadata.testName + ': ' + savePath + '\n'); |
| 41 | + } catch (error) { |
| 42 | + if (error instanceof Error) { |
| 43 | + console.error('Error saving screenshot:', error.message); |
| 44 | + } else { |
| 45 | + console.error('Error saving screenshot'); |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Save an Image class instance as a file |
| 52 | + * @param image - The Image instance to be saved |
| 53 | + * @param outputPath - The path to save the image file |
| 54 | + */ |
| 55 | + private saveImageToFile(image: Image, outputPath: string): string { |
| 56 | + const outputDir = dirname(outputPath); |
| 57 | + if (!existsSync(outputDir)) { |
| 58 | + mkdirSync(outputDir, { recursive: true }); |
| 59 | + } |
| 60 | + |
| 61 | + const outputFilePath = extname(outputPath) ? outputPath : `${outputPath}.${image.type}`; |
| 62 | + |
| 63 | + const binaryData = image.buffer; |
| 64 | + const arrayBufferView = new Uint8Array(binaryData.buffer, binaryData.byteOffset, binaryData.length); |
| 65 | + writeFileSync(outputFilePath, arrayBufferView); |
| 66 | + return outputFilePath; |
| 67 | + } |
| 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 | + shouldCaptureFullPage?: boolean, |
| 81 | +} |
0 commit comments