diff --git a/lib/assert.class.spec.ts b/lib/assert.class.spec.ts index 1fb6798c..8f3b1e0b 100644 --- a/lib/assert.class.spec.ts +++ b/lib/assert.class.spec.ts @@ -2,18 +2,24 @@ import {AssertClass} from "./assert.class"; import {Region} from "./region.class"; import {ScreenClass} from "./screen.class"; import providerRegistry from "./provider/provider-registry.class"; +import {Image} from "../index"; +import {mockPartial} from "sneer"; jest.mock('jimp', () => { }); jest.mock("./screen.class"); +const needleId = "needleId"; + describe("Assert", () => { it("isVisible should not throw if a match is found.", async () => { // GIVEN ScreenClass.prototype.find = jest.fn(() => Promise.resolve(new Region(0, 0, 100, 100))); const screenMock = new ScreenClass(providerRegistry); const SUT = new AssertClass(screenMock); - const needle = "foo"; + const needle = mockPartial({ + id: needleId + }); // WHEN @@ -26,12 +32,14 @@ describe("Assert", () => { ScreenClass.prototype.find = jest.fn(() => Promise.reject("foo")); const screenMock = new ScreenClass(providerRegistry); const SUT = new AssertClass(screenMock); - const needle = "foo"; + const needle = mockPartial({ + id: needleId + }); // WHEN // THEN - await expect(SUT.isVisible(needle)).rejects.toThrowError(`Element '${needle}' not found`); + await expect(SUT.isVisible(needle)).rejects.toThrowError(`Element '${needle.id}' not found`); }); it("isVisible should throw if a match is found.", async () => { @@ -40,14 +48,16 @@ describe("Assert", () => { const screenMock = new ScreenClass(providerRegistry); const SUT = new AssertClass(screenMock); const searchRegion = new Region(10, 10, 10, 10); - const needle = "foo"; + const needle = mockPartial({ + id: needleId + }); // WHEN // THEN await expect(SUT .isVisible(needle, searchRegion)) - .rejects.toThrowError(`Element '${needle}' not found in region ${searchRegion.toString()}` + .rejects.toThrowError(`Element '${needle.id}' not found in region ${searchRegion.toString()}` ); }); @@ -56,12 +66,14 @@ describe("Assert", () => { ScreenClass.prototype.find = jest.fn(() => Promise.resolve(new Region(0, 0, 100, 100))); const screenMock = new ScreenClass(providerRegistry); const SUT = new AssertClass(screenMock); - const needle = "foo"; + const needle = mockPartial({ + id: needleId + }); // WHEN // THEN - await expect(SUT.notVisible(needle)).rejects.toThrowError(`'${needle}' is visible`); + await expect(SUT.notVisible(needle)).rejects.toThrowError(`'${needle.id}' is visible`); }); it("isVisible should throw if a match is found.", async () => { @@ -69,7 +81,9 @@ describe("Assert", () => { ScreenClass.prototype.find = jest.fn(() => Promise.reject("foo")); const screenMock = new ScreenClass(providerRegistry); const SUT = new AssertClass(screenMock); - const needle = "foo"; + const needle = mockPartial({ + id: needleId + }); // WHEN diff --git a/lib/region.class.ts b/lib/region.class.ts index a3e22c71..1f428948 100644 --- a/lib/region.class.ts +++ b/lib/region.class.ts @@ -29,7 +29,6 @@ export class Region { } public toString() { - return `(${this.left}, ${this.top}, ${this.left + this.width}, ${this.top + - this.height})`; + return `(${this.left}, ${this.top}, ${this.width}, ${this.height})`; } } diff --git a/lib/screen.class.ts b/lib/screen.class.ts index cb45fab7..2034f2f0 100644 --- a/lib/screen.class.ts +++ b/lib/screen.class.ts @@ -8,7 +8,6 @@ import {Region} from "./region.class"; import {timeout} from "./util/timeout.function"; import {Image} from "./image.class"; import {ProviderRegistry} from "./provider/provider-registry.class"; -import {loadImageResource} from "./imageResources.function"; import {FirstArgumentType} from "./typings"; import {Point} from "./point.class"; @@ -93,11 +92,11 @@ export class ScreenClass { /** * {@link find} will search for a single occurrence of a template image on a systems main screen - * @param templateImage Filename of the template image, relative to {@link ScreenClass.config.resourceDirectory}, or an {@link Image} instance + * @param template Template {@link Image} instance * @param params {@link LocationParameters} which are used to fine tune search region and / or match confidence */ public async find( - templateImage: string | Image | Promise, + template: Image | Promise, params?: LocationParameters, ): Promise { const minMatch = (params && params.confidence) || this.config.confidence; @@ -105,12 +104,7 @@ export class ScreenClass { const searchRegion = (params && params.searchRegion) || screenSize; const searchMultipleScales = (params && params.searchMultipleScales) - let needle: Image; - if (typeof templateImage === "string") { - needle = await loadImageResource(this.providerRegistry, this.config.resourceDirectory, templateImage); - } else { - needle = await templateImage; - } + const needle = await template; const screenImage = await this.providerRegistry.getScreen().grabScreenRegion(searchRegion); @@ -150,11 +144,11 @@ export class ScreenClass { /** * {@link findAll} will search for every occurrences of a template image on a systems main screen - * @param templateImage Filename of the template image, relative to {@link ScreenClass.config.resourceDirectory}, or an {@link Image} instance + * @param template Template {@link Image} instance * @param params {@link LocationParameters} which are used to fine tune search region and / or match confidence */ public async findAll( - templateImage: string | Image | Promise, + template: FirstArgumentType, params?: LocationParameters, ): Promise { const minMatch = (params && params.confidence) || this.config.confidence; @@ -162,12 +156,7 @@ export class ScreenClass { const searchRegion = (params && params.searchRegion) || screenSize; const searchMultipleScales = (params && params.searchMultipleScales) - let needle: Image; - if (typeof templateImage === "string") { - needle = await loadImageResource(this.providerRegistry, this.config.resourceDirectory, templateImage); - } else { - needle = await templateImage; - } + const needle = await template; const screenImage = await this.providerRegistry.getScreen().grabScreenRegion(searchRegion);