From 9515ce477492b48c4aa02913eb23130dd046dbe1 Mon Sep 17 00:00:00 2001 From: Simon Hofmann Date: Sat, 18 Sep 2021 21:41:05 +0200 Subject: [PATCH] (#278) Added grab and grabRegion methods to Screen --- lib/screen.class.e2e.spec.ts | 32 ++++++++++++++++++++++++++++++++ lib/screen.class.ts | 14 ++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/lib/screen.class.e2e.spec.ts b/lib/screen.class.e2e.spec.ts index f9ef532a..7f3f1405 100644 --- a/lib/screen.class.e2e.spec.ts +++ b/lib/screen.class.e2e.spec.ts @@ -4,6 +4,7 @@ import {FileType} from "./file-type.enum"; import {ScreenClass} from "./screen.class"; import {sleep} from "./sleep.function"; import AbortController from "node-abort-controller"; +import {Region} from "./region.class"; jest.setTimeout(10000); @@ -135,4 +136,35 @@ describe("Screen.", () => { }); setTimeout(() => controller.abort(), abortAfterMs); }); + + it("should grab the whole screen content and return an Image", async () => { + // GIVEN + const visionAdapter = new VisionAdapter(); + const SUT = new ScreenClass(visionAdapter); + const screenWidth = await SUT.width(); + const screenHeight = await SUT.height(); + + // WHEN + const image = await SUT.grab(); + + // THEN + expect(image.data).not.toBeUndefined(); + expect(image.width / image.pixelDensity.scaleX).toBe(screenWidth); + expect(image.height / image.pixelDensity.scaleY).toBe(screenHeight); + }); + + it("should grab a screen region and return an Image", async () => { + // GIVEN + const visionAdapter = new VisionAdapter(); + const SUT = new ScreenClass(visionAdapter); + const regionToGrab = new Region(0, 0, 100, 100); + + // WHEN + const image = await SUT.grabRegion(regionToGrab); + + // THEN + expect(image.data).not.toBeUndefined(); + expect(image.width / image.pixelDensity.scaleX).toBe(regionToGrab.width); + expect(image.height / image.pixelDensity.scaleY).toBe(regionToGrab.height); + }); }); diff --git a/lib/screen.class.ts b/lib/screen.class.ts index c27fefc0..0ee03b77 100644 --- a/lib/screen.class.ts +++ b/lib/screen.class.ts @@ -208,6 +208,13 @@ export class ScreenClass { fileNamePostfix); } + /** + * {@link grab} grabs screen content of a systems main display + */ + public async grab(): Promise { + return this.vision.grabScreen(); + } + /** * {@link captureRegion} captures a screenshot of a region on the systems main display * @param fileName Basename for the generated screenshot @@ -234,6 +241,13 @@ export class ScreenClass { fileNamePostfix); } + /** + * {@link grabRegion} grabs screen content of a region on the systems main display + */ + public async grabRegion(regionToGrab: Region): Promise { + return this.vision.grabScreenRegion(regionToGrab); + } + private async saveImage( image: Image, fileName: string,