@@ -30,60 +30,19 @@ private static void log(String message) {
30
30
AkazeImageFinder .setupOpenCVEnv ();
31
31
}
32
32
33
- // TODO remove this and make private when a way is found to get the screen size for iOS without the appium driver
34
- // and then remove the screen size parameter from other methods
35
- public static Dimension getScreenSize (PlatformType platform , AppiumDriver <MobileElement > driver ) throws Exception {
36
- if (platform .equals (PlatformType .IOS )) {
37
- return driver .manage ().window ().getSize ();
38
- } else {
39
- return getAndroidScreenSize ();
40
- }
41
- }
42
-
43
- private static Dimension getAndroidScreenSize () throws IOException , InterruptedException {
44
- String adb = "adb" ;
45
- String [] adbCommand = {adb , "shell" , "dumpsys" , "window" };
46
- ProcessBuilder p = new ProcessBuilder (adbCommand );
47
- Process proc = p .start ();
48
- InputStream stdin = proc .getInputStream ();
49
- InputStreamReader isr = new InputStreamReader (stdin );
50
- BufferedReader br = new BufferedReader (isr );
51
- String line = null ;
52
- String [] size = null ;
53
- while ((line = br .readLine ()) != null ) {
54
- if (!line .contains ("OriginalmUnrestrictedScreen" )) { //we do this check for devices with android 5.x+ The adb command returns an extra line with the values 0x0 which must be filtered out.
55
- if (line .contains ("mUnrestrictedScreen" )) {
56
- proc .waitFor ();
57
- String [] tmp = line .split ("\\ ) " );
58
- size = tmp [1 ].split ("x" );
59
- }
60
- }
61
- }
62
- int width = Integer .parseInt (size [0 ]);
63
- int height = Integer .parseInt (size [1 ]);
64
- Dimension screenSize = new Dimension (width , height );
65
- return screenSize ;
66
- }
67
-
68
-
69
- public static ImageLocation findImage (String image , String scene , PlatformType platform , Dimension screenSize ) throws Exception {
33
+ public static ImageLocation findImage (String searchedImageFilePath , String sceneImageFilePath , PlatformType platform , Dimension screenSize ) throws Exception {
70
34
ImageRecognitionSettings setting = new ImageRecognitionSettings ();
71
- return findImage (image , scene , setting , platform , screenSize );
35
+ return findImage (searchedImageFilePath , sceneImageFilePath , setting , platform , screenSize );
72
36
}
73
37
74
-
75
- //This method calls on the Akaze scripts to find the coordinates of a given image in another image.
76
- //The "image" parameter is the image that you are searching for
77
- //The "scene" parameter is the image in which we are looking for "image"
78
- // "tolerance" sets the required accuracy for the image recognition algorithm.
79
- public static ImageLocation findImage (String image , String scene , ImageRecognitionSettings settings , PlatformType platform , Dimension screenSize ) throws Exception {
80
- log ("Searching for " + image );
81
- log ("Searching in " + scene );
82
- ImageLocation imgLocation = findImageUsingAkaze (image , scene , settings );
38
+ public static ImageLocation findImage (String searchedImageFilePath , String sceneImageFilePath , ImageRecognitionSettings settings , PlatformType platform , Dimension screenSize ) throws Exception {
39
+ log ("Searching for " + searchedImageFilePath );
40
+ log ("Searching in " + sceneImageFilePath );
41
+ ImageLocation imgLocation = imageFinder .findImage (searchedImageFilePath , sceneImageFilePath , settings .getTolerance ());
83
42
84
43
if (imgLocation != null ) {
85
44
if (platform .equals (PlatformType .IOS )) {
86
- imgLocation = scaleImageRectangleForIos (screenSize , imgLocation , scene );
45
+ imgLocation = scaleImageRectangleForIos (screenSize , imgLocation , sceneImageFilePath );
87
46
}
88
47
Point center = imgLocation .getCenter ();
89
48
if (!isPointInsideScreenBounds (center , screenSize )) {
@@ -95,19 +54,10 @@ public static ImageLocation findImage(String image, String scene, ImageRecogniti
95
54
return imgLocation ;
96
55
}
97
56
98
-
99
-
100
- private static ImageLocation findImageUsingAkaze (String image , String scene , ImageRecognitionSettings settings ) {
101
- ImageLocation location = imageFinder .findImage (image , scene , settings .getTolerance ());
102
- return location ;
103
- }
104
-
105
-
106
-
107
- private static ImageLocation scaleImageRectangleForIos (Dimension screenSize , ImageLocation imageLocation , String scene ) {
57
+ private static ImageLocation scaleImageRectangleForIos (Dimension screenSize , ImageLocation imageLocation , String sceneImageFilePath ) {
108
58
//for retina devices we need to recalculate coordinates
109
- double sceneHeight = imageFinder .getSceneHeight (scene );
110
- double sceneWidth = imageFinder .getSceneWidth (scene );
59
+ double sceneHeight = imageFinder .getSceneHeight (sceneImageFilePath );
60
+ double sceneWidth = imageFinder .getSceneWidth (sceneImageFilePath );
111
61
int screenHeight = screenSize .getHeight ();
112
62
int screenWidth = screenSize .getWidth ();
113
63
@@ -140,22 +90,20 @@ private static ImageLocation scaleImageRectangleForIos(Dimension screenSize, Ima
140
90
return imageLocation ;
141
91
}
142
92
143
-
144
-
145
93
private static boolean isPointInsideScreenBounds (Point center , Dimension screenSize ) {
146
94
return !((center .x >= screenSize .width ) || (center .x < 0 ) || (center .y >= screenSize .height ) || (center .y < 0 ));
147
95
}
148
96
149
97
150
- public static boolean hasImageDissappearedFromScreenBeforeTimeout (String imageFile ,
151
- String screenshotsFolder , Dimension screenSize , PlatformType platform ) throws Exception {
152
- log ("==> Trying to find image: " + imageFile );
98
+ public static boolean hasImageDissappearedFromScreenBeforeTimeout (String searchedImageFilePath ,
99
+ String screenshotBaseDirectory , Dimension screenSize , PlatformType platform ) throws Exception {
100
+ log ("==> Trying to find image: " + searchedImageFilePath );
153
101
int retry_counter =0 ;
154
102
long start = System .nanoTime ();
155
103
while (((System .nanoTime () - start ) / 1e6 / 1000 < 300 )) {
156
- String screenshotName = parseFileName ( imageFile ) + "_screenshot_" +retry_counter ;
157
- String screenShotFile = ImageRecognition .takeScreenshot (screenshotName , screenshotsFolder , platform );
158
- if ((findImage (imageFile , screenShotFile , platform , screenSize )) == null ) {
104
+ String screenshotName = FilenameUtils . getBaseName ( searchedImageFilePath ) + "_screenshot_" +retry_counter ;
105
+ String screenShotFile = ImageRecognition .takeScreenshot (screenshotName , screenshotBaseDirectory , platform );
106
+ if ((findImage (searchedImageFilePath , screenShotFile , platform , screenSize )) == null ) {
159
107
log ("Image has successfully disappeared from screen." );
160
108
return true ;
161
109
}
@@ -166,12 +114,6 @@ public static boolean hasImageDissappearedFromScreenBeforeTimeout(String imageFi
166
114
return false ;
167
115
}
168
116
169
- private static String parseFileName (String imageFile ){
170
- return FilenameUtils .getBaseName (imageFile );
171
- }
172
-
173
-
174
-
175
117
public static String getTextStringFromImage (String imageInput ) {
176
118
String [] tesseractCommand = {"tesseract" , imageInput , "stdout" };
177
119
String value = "" ;
@@ -195,40 +137,36 @@ public static String getTextStringFromImage(String imageInput) {
195
137
196
138
197
139
198
- public static ImageSearchResult findImageOnScreen (String imageFile , String screenshotsFolder , ImageRecognitionSettings settings , Dimension screenSize , PlatformType platform ) throws InterruptedException , IOException , Exception {
199
- ImageSearchResult foundImageDto = findImageLoop (imageFile , screenshotsFolder , settings , screenSize , platform );
200
- if (foundImageDto .isFound () && settings .isCrop ()) {
201
- cropImage (foundImageDto );
140
+ public static ImageSearchResult findImageOnScreen (String searchedImagePath , String screenshotBaseDirectory , ImageRecognitionSettings settings , Dimension screenSize , PlatformType platform ) throws InterruptedException , IOException , Exception {
141
+ ImageSearchResult imageSearchResult = findImageLoop (searchedImagePath , screenshotBaseDirectory , settings , screenSize , platform );
142
+ if (imageSearchResult .isFound () && settings .isCrop ()) {
143
+ log ("Cropping image.." );
144
+ imageFinder .cropImage (imageSearchResult );
145
+ log ("Cropping image.. Succeeded!" );
202
146
}
203
- return foundImageDto ;
147
+ return imageSearchResult ;
204
148
}
205
149
206
- private static ImageSearchResult findImageLoop (String imageFile , String screenshotsFolder , ImageRecognitionSettings settings , Dimension screenSize , PlatformType platform ) throws InterruptedException , IOException , Exception {
150
+ private static ImageSearchResult findImageLoop (String searchedImagePath , String screenshotBaseDirectory , ImageRecognitionSettings settings , Dimension screenSize , PlatformType platform ) throws InterruptedException , IOException , Exception {
207
151
long start_time = System .nanoTime ();
208
- ImageSearchResult foundImageDto = new ImageSearchResult ();
209
- String imageName = parseFileName ( imageFile );
152
+ ImageSearchResult imageSearchResult = new ImageSearchResult ();
153
+ String imageName = FilenameUtils . getBaseName ( searchedImagePath );
210
154
for (int i = 0 ; i < settings .getRetries (); i ++) {
211
155
String screenshotName = imageName + "_screenshot_" +i ;
212
- String screenshotFile = takeScreenshot (screenshotName ,screenshotsFolder , platform );
213
- ImageLocation imageLocation = ImageRecognition .findImage (imageFile , screenshotFile , settings , platform , screenSize );
156
+ String screenshotFile = takeScreenshot (screenshotName ,screenshotBaseDirectory , platform );
157
+ ImageLocation imageLocation = ImageRecognition .findImage (searchedImagePath , screenshotFile , settings , platform , screenSize );
214
158
if (imageLocation !=null ){
215
159
long end_time = System .nanoTime ();
216
160
int difference = (int ) ((end_time - start_time ) / 1e6 / 1000 );
217
161
log ("==> Find image took: " + difference + " secs." );
218
- foundImageDto .setImageLocation (imageLocation );
219
- foundImageDto .setScreenshotFile (screenshotFile );
220
- return foundImageDto ;
162
+ imageSearchResult .setImageLocation (imageLocation );
163
+ imageSearchResult .setScreenshotFile (screenshotFile );
164
+ return imageSearchResult ;
221
165
}
222
166
retryWait (settings );
223
167
}
224
168
log ("==> Image not found" );
225
- return foundImageDto ;
226
- }
227
-
228
- private static void cropImage (ImageSearchResult foundImage ) {
229
- log ("Cropping image.." );
230
- imageFinder .cropImage (foundImage );
231
- log ("Cropping image.. Succeeded!" );
169
+ return imageSearchResult ;
232
170
}
233
171
234
172
private static void retryWait (ImageRecognitionSettings settings ) throws InterruptedException {
@@ -238,31 +176,20 @@ private static void retryWait(ImageRecognitionSettings settings) throws Interrup
238
176
}
239
177
}
240
178
241
- //Stops the script for the given amount of seconds.
242
179
private static void sleep (int seconds ) throws InterruptedException {
243
180
Thread .sleep (seconds * 1000 );
244
181
}
245
182
246
-
247
-
248
-
249
- /* TODO
250
- * if (idevicescreenshotExists) {
251
- // Keep Appium session alive between multiple non-driver screenshots
252
- driver.manage().window().getSize();
253
- }
254
-
255
- */
256
- public static String takeScreenshot (String screenshotName , String screenshotsFolder , PlatformType platform ) throws Exception {
183
+ public static String takeScreenshot (String screenshotName , String screenshotBaseDirectory , PlatformType platform ) throws Exception {
257
184
long start_time = System .nanoTime ();
258
185
259
- String screenshotFile = screenshotsFolder + screenshotName + ".png" ;
260
- String fullFileName = System .getProperty ("user.dir" ) + "/" + screenshotFile ;
186
+ String screenshotFile = screenshotBaseDirectory + screenshotName + ".png" ;
187
+ String screenShotFilePath = System .getProperty ("user.dir" ) + "/" + screenshotFile ;
261
188
262
189
if (platform .equals (PlatformType .IOS )) {
263
- takeIDeviceScreenshot (fullFileName );
190
+ takeIDeviceScreenshot (screenShotFilePath );
264
191
} else if (platform .equals (PlatformType .ANDROID )) {
265
- takeAndroidScreenshot (fullFileName );
192
+ takeAndroidScreenshot (screenShotFilePath );
266
193
} else {
267
194
throw new Exception ("Invalid platformType: " +platform );
268
195
}
@@ -273,11 +200,10 @@ public static String takeScreenshot(String screenshotName, String screenshotsFol
273
200
return screenshotFile ;
274
201
}
275
202
276
-
277
- private static void takeAndroidScreenshot (String fullFileName ) throws IOException , InterruptedException {
203
+ private static void takeAndroidScreenshot (String screenShotFilePath ) throws IOException , InterruptedException {
278
204
log ("Taking android screenshot..." );
279
- log (fullFileName );
280
- String [] cmd = new String []{"screenshot2" , "-d" , fullFileName };
205
+ log (screenShotFilePath );
206
+ String [] cmd = new String []{"screenshot2" , "-d" , screenShotFilePath };
281
207
Process p = Runtime .getRuntime ().exec (cmd );
282
208
BufferedReader in = new BufferedReader (new InputStreamReader (p .getInputStream ()));
283
209
String line ;
@@ -290,15 +216,12 @@ private static void takeAndroidScreenshot(String fullFileName) throws IOExceptio
290
216
}
291
217
}
292
218
293
-
294
-
295
-
296
- private static void takeIDeviceScreenshot (String fullFileName ) throws Exception {
219
+ private static void takeIDeviceScreenshot (String screenShotFilePath ) throws Exception {
297
220
String udid = System .getenv ("UDID" );
298
221
if (udid ==null ){
299
222
throw new Exception ("$UDID was null, set UDID environment variable and try again" );
300
223
}
301
- String [] cmd = new String []{"idevicescreenshot" , "-u" , udid , fullFileName };
224
+ String [] cmd = new String []{"idevicescreenshot" , "-u" , udid , screenShotFilePath };
302
225
Process p = Runtime .getRuntime ().exec (cmd );
303
226
BufferedReader in = new BufferedReader (new InputStreamReader (p .getInputStream ()));
304
227
String line ;
@@ -309,15 +232,50 @@ private static void takeIDeviceScreenshot(String fullFileName) throws Exception
309
232
if (exitVal != 0 ) {
310
233
log ("idevicescreenshot process exited with value: " + exitVal );
311
234
}
312
- cmd = new String []{"sips" , "-s" , "format" , "png" , fullFileName , "--out" , fullFileName };
235
+ cmd = new String []{"sips" , "-s" , "format" , "png" , screenShotFilePath , "--out" , screenShotFilePath };
313
236
p = Runtime .getRuntime ().exec (cmd );
314
237
exitVal = p .waitFor ();
315
238
if (exitVal != 0 ) {
316
239
log ("sips process exited with value: " + exitVal );
317
240
}
318
241
}
242
+
243
+
319
244
245
+ // TODO remove this and make private when a way is found to get the screen size for iOS without the appium driver
246
+ // and then remove the screen size parameter from other methods
247
+ public static Dimension getScreenSize (PlatformType platform , AppiumDriver <MobileElement > driver ) throws Exception {
248
+ if (platform .equals (PlatformType .IOS )) {
249
+ return driver .manage ().window ().getSize ();
250
+ } else {
251
+ return getAndroidScreenSize ();
252
+ }
253
+ }
320
254
255
+ private static Dimension getAndroidScreenSize () throws IOException , InterruptedException {
256
+ String adb = "adb" ;
257
+ String [] adbCommand = {adb , "shell" , "dumpsys" , "window" };
258
+ ProcessBuilder p = new ProcessBuilder (adbCommand );
259
+ Process proc = p .start ();
260
+ InputStream stdin = proc .getInputStream ();
261
+ InputStreamReader isr = new InputStreamReader (stdin );
262
+ BufferedReader br = new BufferedReader (isr );
263
+ String line = null ;
264
+ String [] size = null ;
265
+ while ((line = br .readLine ()) != null ) {
266
+ if (!line .contains ("OriginalmUnrestrictedScreen" )) { //we do this check for devices with android 5.x+ The adb command returns an extra line with the values 0x0 which must be filtered out.
267
+ if (line .contains ("mUnrestrictedScreen" )) {
268
+ proc .waitFor ();
269
+ String [] tmp = line .split ("\\ ) " );
270
+ size = tmp [1 ].split ("x" );
271
+ }
272
+ }
273
+ }
274
+ int width = Integer .parseInt (size [0 ]);
275
+ int height = Integer .parseInt (size [1 ]);
276
+ Dimension screenSize = new Dimension (width , height );
277
+ return screenSize ;
278
+ }
321
279
322
280
323
281
}
0 commit comments