11import { existsSync } from "fs" ;
2- import { VisionAdapter } from "./adapter/vision.adapter.class" ;
3- import { FileType } from "./file-type.enum" ;
4- import { ScreenClass } from "./screen.class" ;
5- import { sleep } from "./sleep.function" ;
2+ import { screen , sleep , FileType , Region } from "@nut-tree/nut-js" ;
63import AbortController from "node-abort-controller" ;
7- import { Region } from "./region.class" ;
8- import providerRegistry from "./provider/provider-registry.class ";
4+
5+ import "@nut-tree/template-matcher ";
96
107jest . setTimeout ( 10000 ) ;
118
129describe ( "Screen." , ( ) => {
1310 it ( "should capture the screen" , ( ) => {
1411 // GIVEN
15- const visionAdapter = new VisionAdapter ( providerRegistry ) ;
16- const SUT = new ScreenClass ( visionAdapter ) ;
1712
1813 // WHEN
19- SUT . capture ( "asdf" , FileType . PNG ) . then ( filename => {
14+ screen . capture ( "asdf" , FileType . PNG ) . then ( filename => {
2015 // THEN
2116 expect ( filename ) . not . toBeNull ( ) ;
2217 sleep ( 1000 ) . then ( ( ) => {
@@ -27,11 +22,9 @@ describe("Screen.", () => {
2722
2823 it ( "should capture the screen and save to JPG" , ( ) => {
2924 // GIVEN
30- const visionAdapter = new VisionAdapter ( providerRegistry ) ;
31- const SUT = new ScreenClass ( visionAdapter ) ;
3225
3326 // WHEN
34- SUT . capture ( "asdf" , FileType . JPG ) . then ( filename => {
27+ screen . capture ( "asdf" , FileType . JPG ) . then ( filename => {
3528 // THEN
3629 expect ( filename ) . not . toBeNull ( ) ;
3730 sleep ( 1000 ) . then ( ( ) => {
@@ -42,12 +35,10 @@ describe("Screen.", () => {
4235
4336 it ( "should capture the screen and save file with prefix" , ( ) => {
4437 // GIVEN
45- const visionAdapter = new VisionAdapter ( providerRegistry ) ;
46- const SUT = new ScreenClass ( visionAdapter ) ;
4738 const prefix = "foo_" ;
4839
4940 // WHEN
50- SUT . capture ( "asdf" , FileType . JPG , "./" , prefix ) . then ( filename => {
41+ screen . capture ( "asdf" , FileType . JPG , "./" , prefix ) . then ( filename => {
5142 // THEN
5243 expect ( filename . includes ( prefix ) ) . toBeTruthy ( ) ;
5344 expect ( filename ) . not . toBeNull ( ) ;
@@ -59,12 +50,10 @@ describe("Screen.", () => {
5950
6051 it ( "should capture the screen and save file with postfix" , ( ) => {
6152 // GIVEN
62- const visionAdapter = new VisionAdapter ( providerRegistry ) ;
63- const SUT = new ScreenClass ( visionAdapter ) ;
6453 const postfix = "_bar" ;
6554
6655 // WHEN
67- SUT . capture ( "asdf" , FileType . JPG , "./" , "" , postfix ) . then ( filename => {
56+ screen . capture ( "asdf" , FileType . JPG , "./" , "" , postfix ) . then ( filename => {
6857 // THEN
6958 expect ( filename . includes ( postfix ) ) . toBeTruthy ( ) ;
7059 expect ( filename ) . not . toBeNull ( ) ;
@@ -76,14 +65,12 @@ describe("Screen.", () => {
7665
7766 it ( "should capture the screen and save file with pre- and postfix" , ( ) => {
7867 // GIVEN
79- const visionAdapter = new VisionAdapter ( providerRegistry ) ;
80- const SUT = new ScreenClass ( visionAdapter ) ;
8168 const filename = "asdf" ;
8269 const prefix = "foo_" ;
8370 const postfix = "_bar" ;
8471
8572 // WHEN
86- SUT . capture ( "asdf" , FileType . JPG , "./" , prefix , postfix ) . then ( output => {
73+ screen . capture ( "asdf" , FileType . JPG , "./" , prefix , postfix ) . then ( output => {
8774 // THEN
8875 expect ( output . includes ( `${ prefix } ${ filename } ${ postfix } ` ) ) . toBeTruthy ( ) ;
8976 expect ( output ) . not . toBeNull ( ) ;
@@ -96,14 +83,12 @@ describe("Screen.", () => {
9683 it ( "should reject after timeout" , async ( ) => {
9784 // GIVEN
9885 const timeout = 5000 ;
99- const visionAdapter = new VisionAdapter ( providerRegistry ) ;
100- const SUT = new ScreenClass ( visionAdapter ) ;
101- SUT . config . resourceDirectory = "./e2e/assets" ;
86+ screen . config . resourceDirectory = "./e2e/assets" ;
10287
10388 // WHEN
10489 const start = Date . now ( ) ;
10590 try {
106- await SUT . waitFor ( "calculator.png" , timeout ) ;
91+ await screen . waitFor ( "calculator.png" , timeout ) ;
10792 } catch ( e ) {
10893 // THEN
10994 expect ( e ) . toBe ( `Action timed out after ${ timeout } ms` ) ;
@@ -120,13 +105,11 @@ describe("Screen.", () => {
120105 const abortAfterMs = 1000 ;
121106 const controller = new AbortController ( ) ;
122107 const signal = controller . signal ;
123- const visionAdapter = new VisionAdapter ( providerRegistry ) ;
124- const SUT = new ScreenClass ( visionAdapter ) ;
125- SUT . config . resourceDirectory = "./e2e/assets" ;
108+ screen . config . resourceDirectory = "./e2e/assets" ;
126109
127110 // WHEN
128111 const start = Date . now ( ) ;
129- SUT . waitFor ( "calculator.png" , timeout , { abort : signal } ) . catch ( e => {
112+ screen . waitFor ( "calculator.png" , timeout , { abort : signal } ) . catch ( e => {
130113 const end = Date . now ( ) ;
131114
132115 // THEN
@@ -140,13 +123,11 @@ describe("Screen.", () => {
140123
141124 it ( "should grab the whole screen content and return an Image" , async ( ) => {
142125 // GIVEN
143- const visionAdapter = new VisionAdapter ( providerRegistry ) ;
144- const SUT = new ScreenClass ( visionAdapter ) ;
145- const screenWidth = await SUT . width ( ) ;
146- const screenHeight = await SUT . height ( ) ;
126+ const screenWidth = await screen . width ( ) ;
127+ const screenHeight = await screen . height ( ) ;
147128
148129 // WHEN
149- const image = await SUT . grab ( ) ;
130+ const image = await screen . grab ( ) ;
150131
151132 // THEN
152133 expect ( image . data ) . not . toBeUndefined ( ) ;
@@ -156,12 +137,10 @@ describe("Screen.", () => {
156137
157138 it ( "should grab a screen region and return an Image" , async ( ) => {
158139 // GIVEN
159- const visionAdapter = new VisionAdapter ( providerRegistry ) ;
160- const SUT = new ScreenClass ( visionAdapter ) ;
161140 const regionToGrab = new Region ( 0 , 0 , 100 , 100 ) ;
162141
163142 // WHEN
164- const image = await SUT . grabRegion ( regionToGrab ) ;
143+ const image = await screen . grabRegion ( regionToGrab ) ;
165144
166145 // THEN
167146 expect ( image . data ) . not . toBeUndefined ( ) ;
0 commit comments