Skip to content

Commit 70c4535

Browse files
committed
Merge branch 'develop'
2 parents 145c09f + 233b778 commit 70c4535

File tree

7 files changed

+64
-18
lines changed

7 files changed

+64
-18
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
All notable changes to this project will be documented in this file.
44

5+
## 0.1.0-beta.3
6+
7+
- Improved error handling on image search
8+
9+
## 0.1.0-beta.2
10+
11+
- Changed default `screen.config.resourceDirectory` to use `process.cwd()`
512

613
## 0.1.0-beta.1
714

index.e2e.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ const close = async () => {
3333
await mouse.leftClick();
3434
};
3535

36+
describe("E2E screen test", () => {
37+
it("should throw on invalid images", async () => {
38+
jest.setTimeout(30000);
39+
await expect(screen.find("mouse.png")).rejects.toContain("Failed to load image");
40+
});
41+
});
42+
3643
describe("E2E demo", () => {
3744
it("should run without throwing", async () => {
3845
jest.setTimeout(30000);

lib/adapter/vision.adapter.class.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,14 @@ export class VisionAdapter {
5757
public async findOnScreenRegion(
5858
matchRequest: MatchRequest,
5959
): Promise<MatchResult> {
60-
const matchResult = await this.finder.findMatch(matchRequest);
61-
return Promise.resolve(matchResult);
60+
return new Promise<MatchResult>(async (resolve, reject) => {
61+
try {
62+
const matchResult = await this.finder.findMatch(matchRequest);
63+
resolve(matchResult);
64+
} catch (e) {
65+
reject(e);
66+
}
67+
});
6268
}
6369

6470
/**

lib/provider/opencv/template-matching-finder.class.spec.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ describe("Template-matching finder", () => {
6060
const result = SUT.findMatch(matchRequest);
6161

6262
// THEN
63-
expect(result).rejects.toEqual(`Failed to load image from '${pathToHaystack}'`);
63+
await expect(result)
64+
.rejects
65+
.toThrowError(`Failed to load ${pathToHaystack}. Reason: 'Failed to load image from '${pathToHaystack}''.`);
6466
});
6567
});

lib/provider/opencv/template-matching-finder.class.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ export class TemplateMatchingFinder implements FinderInterface {
124124
needle = await this.loadNeedle(needleInput);
125125
} catch (e) {
126126
throw new Error(
127-
`Failed to load ${matchRequest.pathToNeedle}. Reason: '${e.message}'.`,
127+
`Failed to load ${matchRequest.pathToNeedle}. Reason: '${e}'.`,
128128
);
129129
}
130130
if (!needle || needle.empty) {
@@ -190,12 +190,16 @@ export class TemplateMatchingFinder implements FinderInterface {
190190
}
191191

192192
public async findMatch(matchRequest: MatchRequest, debug: boolean = false): Promise<MatchResult> {
193-
const matches = await this.findMatches(matchRequest, debug);
194-
return new Promise<MatchResult>((resolve, reject) => {
195-
if (matches.length === 0) {
196-
reject(`Unable to locate ${matchRequest.pathToNeedle}, no match!`);
193+
return new Promise<MatchResult>(async (resolve, reject) => {
194+
try {
195+
const matches = await this.findMatches(matchRequest, debug);
196+
if (matches.length === 0) {
197+
reject(`Unable to locate ${matchRequest.pathToNeedle}, no match!`);
198+
}
199+
resolve(matches[0]);
200+
} catch (e) {
201+
reject(e);
197202
}
198-
resolve(matches[0]);
199203
});
200204
}
201205

lib/screen.class.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,21 @@ describe("Screen.", () => {
6161
.toEqual(`No match for ${imagePath}. Required: ${SUT.config.confidence}, given: ${matchResult.confidence}`);
6262
});
6363

64+
it("should reject when search fails.", async () => {
65+
const rejectionReason = "Search failed.";
66+
VisionAdapter.prototype.findOnScreenRegion = jest.fn(() => {
67+
return Promise.reject(rejectionReason);
68+
});
69+
70+
const visionAdapterMock = new VisionAdapter();
71+
72+
const SUT = new Screen(visionAdapterMock);
73+
const imagePath = "test/path/to/image.png";
74+
await expect(SUT.find(imagePath))
75+
.rejects
76+
.toEqual(`Searching for ${imagePath} failed. Reason: '${rejectionReason}'`);
77+
});
78+
6479
it("should override default confidence value with parameter.", async () => {
6580
const minMatch = 0.8;
6681
const matchResult = new MatchResult(minMatch, searchRegion);

lib/screen.class.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,21 @@ export class Screen {
4444
minMatch,
4545
);
4646

47-
const matchResult = await this.vision.findOnScreenRegion(matchRequest);
48-
49-
return new Promise<Region>((resolve, reject) => {
50-
if (matchResult.confidence >= minMatch) {
51-
resolve(matchResult.location);
52-
} else {
47+
return new Promise<Region>(async (resolve, reject) => {
48+
try {
49+
const matchResult = await this.vision.findOnScreenRegion(matchRequest);
50+
if (matchResult.confidence >= minMatch) {
51+
resolve(matchResult.location);
52+
} else {
53+
reject(
54+
`No match for ${pathToNeedle}. Required: ${minMatch}, given: ${
55+
matchResult.confidence
56+
}`,
57+
);
58+
}
59+
} catch (e) {
5360
reject(
54-
`No match for ${pathToNeedle}. Required: ${minMatch}, given: ${
55-
matchResult.confidence
56-
}`,
61+
`Searching for ${pathToNeedle} failed. Reason: '${e}'`,
5762
);
5863
}
5964
});

0 commit comments

Comments
 (0)