diff --git a/package.json b/package.json index cb339f1..85cc841 100644 --- a/package.json +++ b/package.json @@ -37,7 +37,6 @@ "uuid": "^7.0.1" }, "devDependencies": { - "@types/fs-extra": "^8.0.1", "@types/jest": "^24.9.1", "@types/klaw-sync": "^6.0.0", "@types/uuid": "^7.0.0", diff --git a/src/screenshot/Screenshot.ts b/src/screenshot/Screenshot.ts index 36ce0ec..3dd3a9f 100644 --- a/src/screenshot/Screenshot.ts +++ b/src/screenshot/Screenshot.ts @@ -10,14 +10,13 @@ export class Screenshot { public static async capture(): Promise { try { - if (this.customScreenshotWriter != null) { if (Util.isAsync(this.customScreenshotWriter)) { return await this.customScreenshotWriter(); } else { return Promise.resolve(this.customScreenshotWriter()); } - } else if (this.customScreenshotWriter != null) { + } else if (this.customScreenGrabber != null) { let data: Uint8Array; if (Util.isAsync(this.customScreenshotWriter)) { data = await this.customScreenGrabber(); diff --git a/tests/processors/ExecutionEndingProcessorTests.ts b/tests/processors/ExecutionEndingProcessorTests.ts index 1cc40c1..c6094c3 100644 --- a/tests/processors/ExecutionEndingProcessorTests.ts +++ b/tests/processors/ExecutionEndingProcessorTests.ts @@ -71,7 +71,7 @@ describe('ExecutionEndingProcessor', () => { let pRes = res.executionResult as gauge.messages.ProtoExecutionResult; expect(pRes.failed).toBe(true); expect(pRes.errorMessage).toBe("failed"); - expect(pRes.screenShot.length).toBe(10); + expect(pRes.failureScreenshotFile).toBeTruthy(); }) }) diff --git a/tests/processors/StepExecutionProcessorTests.ts b/tests/processors/StepExecutionProcessorTests.ts index 3f7cdc7..d382948 100644 --- a/tests/processors/StepExecutionProcessorTests.ts +++ b/tests/processors/StepExecutionProcessorTests.ts @@ -16,7 +16,7 @@ describe('StepExecutionProcessor', () => { }) describe('.process', () => { - it('should process stepex exection request when step is unimplemeted', async () => { + it('should process step execution request when step is unimplemented', async () => { let message = new gauge.messages.Message({ messageId: 0, messageType: gauge.messages.Message.MessageType.ExecuteStep, @@ -32,7 +32,7 @@ describe('StepExecutionProcessor', () => { expect(result.errorMessage).toBe('Step Implementation not found'); }) - it('should process stepex exection request when there is param lenght mismatch', async () => { + it('should process step execution request when there is param lenght mismatch', async () => { let capture = jest.spyOn(Screenshot, "capture"); registry.isImplemented = jest.fn().mockReturnValue(true); registry.get = jest.fn().mockReturnValue(new StepRegistryEntry('hello', 'hello', 'StepImpl.ts', (a: any) => { })) @@ -48,11 +48,11 @@ describe('StepExecutionProcessor', () => { let response = resMess.executionStatusResponse as gauge.messages.ExecutionStatusResponse; let result = response.executionResult as gauge.messages.ProtoExecutionResult; expect(result.failed).toBe(true); - expect(result.errorMessage).toBe('Argument length mismatch for `hello`. Actual Count: [1], Exepected Count: [0]'); + expect(result.errorMessage).toBe('Argument length mismatch for `hello`. Actual Count: [1], Expected Count: [0]'); expect(capture).toBeCalled(); }) - it('should process stepex exection request', async () => { + it('should process step execution request', async () => { registry.isImplemented = jest.fn().mockReturnValue(true); registry.get = jest.fn().mockReturnValue(new StepRegistryEntry('hello to ', 'hello {} to {}', @@ -85,7 +85,7 @@ describe('StepExecutionProcessor', () => { expect(result.errorMessage).toBe(''); }) - it('should process stepex exection request when step is recoverable', async () => { + it('should process step execution request when step is recoverable', async () => { let capture = jest.spyOn(Screenshot, "capture"); process.env.screenshot_on_failure = 'false'; diff --git a/tests/screenshot/ScreenshotTest.ts b/tests/screenshot/ScreenshotTest.ts index c89918f..1e41d2b 100644 --- a/tests/screenshot/ScreenshotTest.ts +++ b/tests/screenshot/ScreenshotTest.ts @@ -1,33 +1,47 @@ +import { existsSync, unlinkSync } from 'fs'; import { Screenshot } from '../../src/screenshot/Screenshot'; import { Util } from '../../src/utils/Util'; describe('Screenshot', () => { + + let screenshotFile: string; + describe('.capture', () => { + + afterEach(() => { + if (existsSync(screenshotFile)) { + unlinkSync(screenshotFile); + } + }) + it('should capture screen shot and return the buffer', async () => { + process.env.gauge_screenshots_dir = ""; Util.readFileBuffer = jest.fn().mockReturnValue(new ArrayBuffer(10)); Util.spawn = jest.fn(); - let data = await Screenshot.capture(); - expect(data.length).toBe(10); + screenshotFile = await Screenshot.capture(); + expect(screenshotFile.endsWith('.png')).toBe(true); }) it('should return empty buffer if fails to capture screenshot', async () => { Util.readFileBuffer = jest.fn().mockReturnValue(new ArrayBuffer(10)); - Util.spawn = jest.fn().mockImplementation(() => { throw new Error('faild to spawn') }); + Util.spawn = jest.fn().mockImplementation(() => { throw new Error('failed to spawn') }); console.log = jest.fn(); - let data = await Screenshot.capture(); - expect(data.length).toBe(0); + let file = await Screenshot.capture(); + expect(file).toBe(""); }) - it('should capture screenshot using customscreengrabber', async () => { - Screenshot.setCustomScreenGrabber(() => { return new Uint8Array(new ArrayBuffer(10))}) - let data = await Screenshot.capture(); - expect(data.length).toBe(10); + it('should capture screenshot using custom screen grabber and write data to file', async () => { + process.env.gauge_screenshots_dir = process.cwd(); + Screenshot.setCustomScreenGrabber(() => { return new Uint8Array(new ArrayBuffer(10)) }) + screenshotFile = await Screenshot.capture(); + expect(screenshotFile.endsWith('png')).toBeTruthy(); }) - it('should capture screenshot using async customscreengrabber', async () => { - Screenshot.setCustomScreenGrabber(async () => { return new Uint8Array(new ArrayBuffer(5))}) - let data = await Screenshot.capture(); - expect(data.length).toBe(5); + it('should capture screenshot using async custom screen grabber and write data to file', async () => { + process.env.gauge_screenshots_dir = ""; + Screenshot.setCustomScreenGrabber(async () => { return new Uint8Array(new ArrayBuffer(5)) }) + screenshotFile = await Screenshot.capture(); + expect(screenshotFile.endsWith('png')).toBeTruthy(); }) }) }) \ No newline at end of file