Skip to content

Commit b36c64e

Browse files
committed
(#49) waitFor implementation
1 parent 0d5a07a commit b36c64e

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

lib/screen.class.e2e.spec.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,25 @@ describe("Screen.", () => {
8787
});
8888
});
8989
});
90+
91+
it("should run without throwing", async () => {
92+
// GIVEN
93+
const timeout = 5000;
94+
const visionAdapter = new VisionAdapter();
95+
const SUT = new Screen(visionAdapter);
96+
SUT.config.resourceDirectory = "./e2e/assets";
97+
98+
// WHEN
99+
const start = Date.now();
100+
try {
101+
await SUT.waitFor("calculator.png", timeout);
102+
} catch (e) {
103+
// THEN
104+
expect(e).toBe(`Action timed out after ${timeout} ms`);
105+
}
106+
const end = Date.now();
107+
108+
// THEN
109+
expect(end - start).toBeGreaterThanOrEqual(timeout);
110+
});
90111
});

lib/screen.class.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { generateOutputPath } from "./generate-output-path.function";
66
import { LocationParameters } from "./locationparameters.class";
77
import { MatchRequest } from "./match-request.class";
88
import { Region } from "./region.class";
9+
import { timeout } from "./util/poll-action.function";
910

1011
export class Screen {
1112
public config = {
@@ -64,6 +65,14 @@ export class Screen {
6465
});
6566
}
6667

68+
public async waitFor(
69+
pathToNeedle: string,
70+
timeoutMs: number = 5000,
71+
params?: LocationParameters,
72+
): Promise<Region> {
73+
return timeout(500, timeoutMs, () => this.find(pathToNeedle, params));
74+
}
75+
6776
public async capture(
6877
fileName: string,
6978
fileFormat: FileType = FileType.PNG,

0 commit comments

Comments
 (0)