Skip to content
This repository was archived by the owner on Dec 7, 2020. It is now read-only.

Commit 81690a2

Browse files
author
Severi Haverila
committed
fetch ios screensize from properties file
1 parent aa6bf5e commit 81690a2

File tree

3 files changed

+81
-22
lines changed

3 files changed

+81
-22
lines changed

example/src/main/java/TestdroidImageRecognition.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,15 @@ public ImageSearchResult findImageOnScreen(String imageName, ImageRecognitionSet
3939
String screenshotsFolder = "target/reports/screenshots/";
4040
String imageFile = queryImageFolder+imageName;
4141
log("Searching for: "+imageFile);
42-
Dimension screenSize = ImageRecognition.getScreenSize(platform, driver);
43-
ImageSearchResult foundImage = ImageRecognition.findImageOnScreen(imageFile, screenshotsFolder, settings, screenSize, platform);
42+
ImageSearchResult foundImage = ImageRecognition.findImageOnScreen(imageFile, screenshotsFolder, settings, platform);
4443
return foundImage;
4544
}
4645

4746
public void waitForImageToDisappearFromScreen(String image) throws Exception {
4847
String queryImageFolder = "queryimages/" + queryImageSubFolder;
4948
String screenshotsFolder = "target/reports/screenshots/";
50-
Dimension screenSize = ImageRecognition.getScreenSize(platform, driver);
5149
String imageFile = queryImageFolder+image;
52-
boolean hasImageDisappeared = ImageRecognition.hasImageDissappearedFromScreenBeforeTimeout(imageFile, screenshotsFolder, screenSize, platform);
50+
boolean hasImageDisappeared = ImageRecognition.hasImageDissappearedFromScreenBeforeTimeout(imageFile, screenshotsFolder, platform);
5351
assert(hasImageDisappeared);
5452
}
5553

library/src/main/java/imagerecognition/ImageRecognition.java

Lines changed: 78 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@
44
import java.io.IOException;
55
import java.io.InputStream;
66
import java.io.InputStreamReader;
7+
import java.util.Properties;
78

89
import org.apache.commons.io.FilenameUtils;
910
import org.opencv.core.Point;
1011
import org.openqa.selenium.Dimension;
1112
import org.slf4j.Logger;
1213
import org.slf4j.LoggerFactory;
1314

14-
import io.appium.java_client.AppiumDriver;
15-
import io.appium.java_client.MobileElement;
1615
import objects.ImageLocation;
1716
import objects.ImageRecognitionSettings;
1817
import objects.ImageSearchResult;
@@ -30,17 +29,27 @@ private static void log(String message) {
3029
AkazeImageFinder.setupOpenCVEnv();
3130
}
3231

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 {
3433
ImageRecognitionSettings setting = new ImageRecognitionSettings();
35-
return findImage(searchedImageFilePath, sceneImageFilePath, setting, platform, screenSize);
34+
return findImage(searchedImageFilePath, sceneImageFilePath, setting, platform);
3635
}
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 {
3938
log("Searching for " + searchedImageFilePath);
4039
log("Searching in " + sceneImageFilePath);
4140
ImageLocation imgLocation = imageFinder.findImage(searchedImageFilePath, sceneImageFilePath, settings.getTolerance());
4241

4342
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+
4453
if (platform.equals(PlatformType.IOS)) {
4554
imgLocation = scaleImageRectangleForIos(screenSize, imgLocation, sceneImageFilePath);
4655
}
@@ -96,14 +105,14 @@ private static boolean isPointInsideScreenBounds(Point center, Dimension screenS
96105

97106

98107
public static boolean hasImageDissappearedFromScreenBeforeTimeout(String searchedImageFilePath,
99-
String screenshotBaseDirectory, Dimension screenSize, PlatformType platform) throws Exception {
108+
String screenshotBaseDirectory, PlatformType platform) throws Exception {
100109
log("==> Trying to find image: " + searchedImageFilePath);
101110
int retry_counter=0;
102111
long start = System.nanoTime();
103112
while (((System.nanoTime() - start) / 1e6 / 1000 < 300)) {
104113
String screenshotName = FilenameUtils.getBaseName(searchedImageFilePath) + "_screenshot_"+retry_counter;
105114
String screenShotFile = ImageRecognition.takeScreenshot(screenshotName, screenshotBaseDirectory, platform);
106-
if ((findImage(searchedImageFilePath, screenShotFile, platform, screenSize)) == null) {
115+
if ((findImage(searchedImageFilePath, screenShotFile, platform)) == null) {
107116
log("Image has successfully disappeared from screen.");
108117
return true;
109118
}
@@ -137,8 +146,8 @@ public static String getTextStringFromImage(String imageInput) {
137146

138147

139148

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);
142151
if (imageSearchResult.isFound() && settings.isCrop()) {
143152
log("Cropping image..");
144153
imageFinder.cropImage(imageSearchResult);
@@ -147,14 +156,14 @@ public static ImageSearchResult findImageOnScreen(String searchedImagePath, Stri
147156
return imageSearchResult;
148157
}
149158

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 {
151160
long start_time = System.nanoTime();
152161
ImageSearchResult imageSearchResult = new ImageSearchResult();
153162
String imageName = FilenameUtils.getBaseName(searchedImagePath);
154163
for (int i = 0; i < settings.getRetries(); i++) {
155164
String screenshotName = imageName + "_screenshot_"+i;
156165
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);
158167
if (imageLocation!=null){
159168
long end_time = System.nanoTime();
160169
int difference = (int) ((end_time - start_time) / 1e6 / 1000);
@@ -240,18 +249,69 @@ private static void takeIDeviceScreenshot(String screenShotFilePath) throws Exce
240249
}
241250
}
242251

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 {
248253
if (platform.equals(PlatformType.IOS)) {
249-
return driver.manage().window().getSize();
254+
return getIosScreenSize();
250255
} else {
251256
return getAndroidScreenSize();
252257
}
253258
}
254259

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+
255315
private static Dimension getAndroidScreenSize() throws IOException, InterruptedException {
256316
String adb = "adb";
257317
String[] adbCommand = {adb, "shell", "dumpsys", "window"};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
iPhone7,2=667x375

0 commit comments

Comments
 (0)