Skip to content

Commit 9515ce4

Browse files
committed
(#278) Added grab and grabRegion methods to Screen
1 parent 079eca2 commit 9515ce4

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

lib/screen.class.e2e.spec.ts

+32
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {FileType} from "./file-type.enum";
44
import {ScreenClass} from "./screen.class";
55
import {sleep} from "./sleep.function";
66
import AbortController from "node-abort-controller";
7+
import {Region} from "./region.class";
78

89
jest.setTimeout(10000);
910

@@ -135,4 +136,35 @@ describe("Screen.", () => {
135136
});
136137
setTimeout(() => controller.abort(), abortAfterMs);
137138
});
139+
140+
it("should grab the whole screen content and return an Image", async () => {
141+
// GIVEN
142+
const visionAdapter = new VisionAdapter();
143+
const SUT = new ScreenClass(visionAdapter);
144+
const screenWidth = await SUT.width();
145+
const screenHeight = await SUT.height();
146+
147+
// WHEN
148+
const image = await SUT.grab();
149+
150+
// THEN
151+
expect(image.data).not.toBeUndefined();
152+
expect(image.width / image.pixelDensity.scaleX).toBe(screenWidth);
153+
expect(image.height / image.pixelDensity.scaleY).toBe(screenHeight);
154+
});
155+
156+
it("should grab a screen region and return an Image", async () => {
157+
// GIVEN
158+
const visionAdapter = new VisionAdapter();
159+
const SUT = new ScreenClass(visionAdapter);
160+
const regionToGrab = new Region(0, 0, 100, 100);
161+
162+
// WHEN
163+
const image = await SUT.grabRegion(regionToGrab);
164+
165+
// THEN
166+
expect(image.data).not.toBeUndefined();
167+
expect(image.width / image.pixelDensity.scaleX).toBe(regionToGrab.width);
168+
expect(image.height / image.pixelDensity.scaleY).toBe(regionToGrab.height);
169+
});
138170
});

lib/screen.class.ts

+14
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,13 @@ export class ScreenClass {
208208
fileNamePostfix);
209209
}
210210

211+
/**
212+
* {@link grab} grabs screen content of a systems main display
213+
*/
214+
public async grab(): Promise<Image> {
215+
return this.vision.grabScreen();
216+
}
217+
211218
/**
212219
* {@link captureRegion} captures a screenshot of a region on the systems main display
213220
* @param fileName Basename for the generated screenshot
@@ -234,6 +241,13 @@ export class ScreenClass {
234241
fileNamePostfix);
235242
}
236243

244+
/**
245+
* {@link grabRegion} grabs screen content of a region on the systems main display
246+
*/
247+
public async grabRegion(regionToGrab: Region): Promise<Image> {
248+
return this.vision.grabScreenRegion(regionToGrab);
249+
}
250+
237251
private async saveImage(
238252
image: Image,
239253
fileName: string,

0 commit comments

Comments
 (0)