Skip to content

(#278) Added grab and grabRegion methods to Screen #280

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions lib/screen.class.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);
});
});
14 changes: 14 additions & 0 deletions lib/screen.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,13 @@ export class ScreenClass {
fileNamePostfix);
}

/**
* {@link grab} grabs screen content of a systems main display
*/
public async grab(): Promise<Image> {
return this.vision.grabScreen();
}

/**
* {@link captureRegion} captures a screenshot of a region on the systems main display
* @param fileName Basename for the generated screenshot
Expand All @@ -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<Image> {
return this.vision.grabScreenRegion(regionToGrab);
}

private async saveImage(
image: Image,
fileName: string,
Expand Down