4
4
import java .io .IOException ;
5
5
import java .io .InputStream ;
6
6
import java .io .InputStreamReader ;
7
+ import java .util .Properties ;
7
8
8
9
import org .apache .commons .io .FilenameUtils ;
9
10
import org .opencv .core .Point ;
10
11
import org .openqa .selenium .Dimension ;
11
12
import org .slf4j .Logger ;
12
13
import org .slf4j .LoggerFactory ;
13
14
14
- import io .appium .java_client .AppiumDriver ;
15
- import io .appium .java_client .MobileElement ;
16
15
import objects .ImageLocation ;
17
16
import objects .ImageRecognitionSettings ;
18
17
import objects .ImageSearchResult ;
@@ -30,17 +29,27 @@ private static void log(String message) {
30
29
AkazeImageFinder .setupOpenCVEnv ();
31
30
}
32
31
33
- public static ImageLocation findImage (String searchedImageFilePath , String sceneImageFilePath , PlatformType platform , Dimension screenSize ) throws Exception {
32
+ public static ImageLocation findImage (String searchedImageFilePath , String sceneImageFilePath , PlatformType platform ) throws Exception {
34
33
ImageRecognitionSettings setting = new ImageRecognitionSettings ();
35
- return findImage (searchedImageFilePath , sceneImageFilePath , setting , platform , screenSize );
34
+ return findImage (searchedImageFilePath , sceneImageFilePath , setting , platform );
36
35
}
37
-
38
- public static ImageLocation findImage (String searchedImageFilePath , String sceneImageFilePath , ImageRecognitionSettings settings , PlatformType platform , Dimension screenSize ) throws Exception {
36
+
37
+ public static ImageLocation findImage (String searchedImageFilePath , String sceneImageFilePath , ImageRecognitionSettings settings , PlatformType platform ) throws Exception {
39
38
log ("Searching for " + searchedImageFilePath );
40
39
log ("Searching in " + sceneImageFilePath );
41
40
ImageLocation imgLocation = imageFinder .findImage (searchedImageFilePath , sceneImageFilePath , settings .getTolerance ());
42
41
43
42
if (imgLocation != null ) {
43
+ Dimension screenSize = getScreenSize (platform );
44
+ double sceneHeight = imageFinder .getSceneHeight (sceneImageFilePath );
45
+ double sceneWidth = imageFinder .getSceneWidth (sceneImageFilePath );
46
+ int screenHeight = screenSize .getHeight ();
47
+ int screenWidth = screenSize .getWidth ();
48
+
49
+ System .out .println ("TESTAS" );
50
+ System .out .println (sceneHeight +" vs " +screenHeight );
51
+ System .out .println (sceneWidth +" vs " +screenWidth );
52
+
44
53
if (platform .equals (PlatformType .IOS )) {
45
54
imgLocation = scaleImageRectangleForIos (screenSize , imgLocation , sceneImageFilePath );
46
55
}
@@ -96,14 +105,14 @@ private static boolean isPointInsideScreenBounds(Point center, Dimension screenS
96
105
97
106
98
107
public static boolean hasImageDissappearedFromScreenBeforeTimeout (String searchedImageFilePath ,
99
- String screenshotBaseDirectory , Dimension screenSize , PlatformType platform ) throws Exception {
108
+ String screenshotBaseDirectory , PlatformType platform ) throws Exception {
100
109
log ("==> Trying to find image: " + searchedImageFilePath );
101
110
int retry_counter =0 ;
102
111
long start = System .nanoTime ();
103
112
while (((System .nanoTime () - start ) / 1e6 / 1000 < 300 )) {
104
113
String screenshotName = FilenameUtils .getBaseName (searchedImageFilePath ) + "_screenshot_" +retry_counter ;
105
114
String screenShotFile = ImageRecognition .takeScreenshot (screenshotName , screenshotBaseDirectory , platform );
106
- if ((findImage (searchedImageFilePath , screenShotFile , platform , screenSize )) == null ) {
115
+ if ((findImage (searchedImageFilePath , screenShotFile , platform )) == null ) {
107
116
log ("Image has successfully disappeared from screen." );
108
117
return true ;
109
118
}
@@ -137,8 +146,8 @@ public static String getTextStringFromImage(String imageInput) {
137
146
138
147
139
148
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 );
149
+ public static ImageSearchResult findImageOnScreen (String searchedImagePath , String screenshotBaseDirectory , ImageRecognitionSettings settings , PlatformType platform ) throws InterruptedException , IOException , Exception {
150
+ ImageSearchResult imageSearchResult = findImageLoop (searchedImagePath , screenshotBaseDirectory , settings , platform );
142
151
if (imageSearchResult .isFound () && settings .isCrop ()) {
143
152
log ("Cropping image.." );
144
153
imageFinder .cropImage (imageSearchResult );
@@ -147,14 +156,14 @@ public static ImageSearchResult findImageOnScreen(String searchedImagePath, Stri
147
156
return imageSearchResult ;
148
157
}
149
158
150
- private static ImageSearchResult findImageLoop (String searchedImagePath , String screenshotBaseDirectory , ImageRecognitionSettings settings , Dimension screenSize , PlatformType platform ) throws InterruptedException , IOException , Exception {
159
+ private static ImageSearchResult findImageLoop (String searchedImagePath , String screenshotBaseDirectory , ImageRecognitionSettings settings , PlatformType platform ) throws InterruptedException , IOException , Exception {
151
160
long start_time = System .nanoTime ();
152
161
ImageSearchResult imageSearchResult = new ImageSearchResult ();
153
162
String imageName = FilenameUtils .getBaseName (searchedImagePath );
154
163
for (int i = 0 ; i < settings .getRetries (); i ++) {
155
164
String screenshotName = imageName + "_screenshot_" +i ;
156
165
String screenshotFile = takeScreenshot (screenshotName ,screenshotBaseDirectory , platform );
157
- ImageLocation imageLocation = ImageRecognition .findImage (searchedImagePath , screenshotFile , settings , platform , screenSize );
166
+ ImageLocation imageLocation = ImageRecognition .findImage (searchedImagePath , screenshotFile , settings , platform );
158
167
if (imageLocation !=null ){
159
168
long end_time = System .nanoTime ();
160
169
int difference = (int ) ((end_time - start_time ) / 1e6 / 1000 );
@@ -240,18 +249,69 @@ private static void takeIDeviceScreenshot(String screenShotFilePath) throws Exce
240
249
}
241
250
}
242
251
243
-
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 {
252
+ private static Dimension getScreenSize (PlatformType platform ) throws Exception {
248
253
if (platform .equals (PlatformType .IOS )) {
249
- return driver . manage (). window (). getSize ();
254
+ return getIosScreenSize ();
250
255
} else {
251
256
return getAndroidScreenSize ();
252
257
}
253
258
}
254
259
260
+ private static Dimension getIosScreenSize () throws Exception {
261
+ String udid = System .getenv ("UDID" );
262
+ if (udid ==null ){
263
+ throw new Exception ("$UDID was null, set UDID environment variable and try again" );
264
+ }
265
+ String [] cmd = new String []{"ideviceinfo" , "-u" , udid , "--key" , "ProductType" };
266
+ Process p = Runtime .getRuntime ().exec (cmd );
267
+ BufferedReader in = new BufferedReader (new InputStreamReader (p .getInputStream ()));
268
+
269
+ int exitVal = p .waitFor ();
270
+ if (exitVal != 0 ) {
271
+ throw new Exception ("ideviceinfo process exited with value: " + exitVal );
272
+ }
273
+ String productType = in .readLine ();
274
+
275
+ Properties screenSizeProperties = fetchProperties ();
276
+ String screenDimensionString = (String ) screenSizeProperties .get (productType );
277
+ if (screenDimensionString == null ){
278
+ throw new Exception ("ios-screen-size.properties is missing entry for: " + productType );
279
+ }
280
+ String screenDimensions [] = screenDimensionString .split ("x" );
281
+ if (screenDimensions .length !=2 ){
282
+ throw new Exception ("Invalid ios-screen-size.properties file syntax for line: " + productType );
283
+ }
284
+ int height = Integer .parseInt (screenDimensions [0 ]);
285
+ int width = Integer .parseInt (screenDimensions [1 ]);
286
+ return new Dimension (width , height );
287
+ }
288
+
289
+ private static Properties fetchProperties () throws Exception {
290
+ Properties iosScreenSizeProperties = new Properties ();
291
+ InputStream input = null ;
292
+ try {
293
+ String filename = "ios-screen-size.properties" ;
294
+ input = ImageRecognition .class .getClassLoader ().getResourceAsStream (filename );
295
+
296
+ if (input == null ) {
297
+ throw new Exception ("ios-screen-size.properties does not exist" );
298
+ }
299
+ iosScreenSizeProperties .load (input );
300
+
301
+ } catch (IOException ex ) {
302
+ ex .printStackTrace ();
303
+ } finally {
304
+ if (input != null ) {
305
+ try {
306
+ input .close ();
307
+ } catch (IOException e ) {
308
+ e .printStackTrace ();
309
+ }
310
+ }
311
+ }
312
+ return iosScreenSizeProperties ;
313
+ }
314
+
255
315
private static Dimension getAndroidScreenSize () throws IOException , InterruptedException {
256
316
String adb = "adb" ;
257
317
String [] adbCommand = {adb , "shell" , "dumpsys" , "window" };
0 commit comments