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

Commit d9f619d

Browse files
author
Severi Haverila
committed
moved screensize method library
1 parent 5f2485a commit d9f619d

File tree

2 files changed

+47
-41
lines changed

2 files changed

+47
-41
lines changed

src/main/java/TestdroidImageRecognition.java

Lines changed: 6 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -40,57 +40,23 @@ public ImageLocation findImageOnScreen(String image) throws Exception {
4040
public ImageSearchResult findImageOnScreen(String imageName, ImageRecognitionSettings settings) throws Exception {
4141
// queryImageFolder is "", unless set by setQueryImageFolder()
4242
String queryImageFolder = "queryimages/" + queryImageSubFolder;
43-
String screenshotsFolder = "target/reports/screenshots/"; //TODO Severi remove this
43+
String screenshotsFolder = "target/reports/screenshots/";
4444
String imageFile = queryImageFolder+imageName;
4545
log("Searching for: "+imageFile);
46-
ImageSearchResult foundImage = ImageRecognition.findImageOnScreen(imageFile, screenshotsFolder, settings, getScreenSize(), platform);
46+
Dimension screenSize = ImageRecognition.getScreenSize(platform, driver);
47+
ImageSearchResult foundImage = ImageRecognition.findImageOnScreen(imageFile, screenshotsFolder, settings, screenSize, platform);
4748
return foundImage;
4849
}
4950

5051
public void waitForImageToDisappearFromScreen(String image) throws Exception {
51-
String queryImageFolder = "queryimages/" + queryImageSubFolder; //TODO Severi remove this
52-
String screenshotsFolder = "target/reports/screenshots/"; //TODO Severi remove this
53-
Dimension screenSize = getScreenSize(); //TODO Severi remove this
52+
String queryImageFolder = "queryimages/" + queryImageSubFolder;
53+
String screenshotsFolder = "target/reports/screenshots/";
54+
Dimension screenSize = ImageRecognition.getScreenSize(platform, driver);
5455
String imageFile = queryImageFolder+image;
5556
boolean hasImageDisappeared = ImageRecognition.hasImageDissappearedFromScreenBeforeTimeout(imageFile, screenshotsFolder, screenSize, platform);
5657
assert(hasImageDisappeared);
5758
}
5859

59-
public Dimension getScreenSize() throws Exception {
60-
log("trying to get size from adb...");
61-
log("------------------------------");
62-
if (platform.equals(PlatformType.IOS)) {
63-
return driver.manage().window().getSize();
64-
} else {
65-
return getAndroidScreenSize();
66-
}
67-
}
68-
69-
private Dimension getAndroidScreenSize() throws IOException, InterruptedException {
70-
String adb = "adb";
71-
String[] adbCommand = {adb, "shell", "dumpsys", "window"};
72-
ProcessBuilder p = new ProcessBuilder(adbCommand);
73-
Process proc = p.start();
74-
InputStream stdin = proc.getInputStream();
75-
InputStreamReader isr = new InputStreamReader(stdin);
76-
BufferedReader br = new BufferedReader(isr);
77-
String line = null;
78-
String[] size = null;
79-
while ((line = br.readLine()) != null) {
80-
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.
81-
if (line.contains("mUnrestrictedScreen")) {
82-
proc.waitFor();
83-
String[] tmp = line.split("\\) ");
84-
size = tmp[1].split("x");
85-
}
86-
}
87-
}
88-
int width = Integer.parseInt(size[0]);
89-
int height = Integer.parseInt(size[1]);
90-
Dimension screenSize = new Dimension(width, height);
91-
return screenSize;
92-
}
93-
9460

9561
public String grabTextFromImage(String image) throws Exception {
9662
ImageSearchResult imageSearch = findAndCropImage(image);

src/main/java/library/ImageRecognition.java

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import dtos.ImageRecognitionSettings;
1616
import dtos.ImageSearchResult;
1717
import dtos.PlatformType;
18+
import io.appium.java_client.AppiumDriver;
19+
import io.appium.java_client.MobileElement;
1820

1921
public class ImageRecognition {
2022

@@ -23,6 +25,41 @@ public class ImageRecognition {
2325
private static void log(String message) {
2426
logger.info(message);
2527
}
28+
29+
// TODO remove this and make private when a way is found to get the screen size for iOS without the appium driver
30+
// and then remove the screen size parameter from other methods
31+
public static Dimension getScreenSize(PlatformType platform, AppiumDriver<MobileElement> driver) throws Exception {
32+
if (platform.equals(PlatformType.IOS)) {
33+
return driver.manage().window().getSize();
34+
} else {
35+
return getAndroidScreenSize();
36+
}
37+
}
38+
39+
private static Dimension getAndroidScreenSize() throws IOException, InterruptedException {
40+
String adb = "adb";
41+
String[] adbCommand = {adb, "shell", "dumpsys", "window"};
42+
ProcessBuilder p = new ProcessBuilder(adbCommand);
43+
Process proc = p.start();
44+
InputStream stdin = proc.getInputStream();
45+
InputStreamReader isr = new InputStreamReader(stdin);
46+
BufferedReader br = new BufferedReader(isr);
47+
String line = null;
48+
String[] size = null;
49+
while ((line = br.readLine()) != null) {
50+
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.
51+
if (line.contains("mUnrestrictedScreen")) {
52+
proc.waitFor();
53+
String[] tmp = line.split("\\) ");
54+
size = tmp[1].split("x");
55+
}
56+
}
57+
}
58+
int width = Integer.parseInt(size[0]);
59+
int height = Integer.parseInt(size[1]);
60+
Dimension screenSize = new Dimension(width, height);
61+
return screenSize;
62+
}
2663

2764

2865
public static ImageLocation findImage(String image, String scene, PlatformType platform, Dimension screenSize) throws Exception {
@@ -252,8 +289,11 @@ private static void takeAndroidScreenshot(String fullFileName) throws IOExceptio
252289

253290

254291

255-
private static void takeIDeviceScreenshot(String fullFileName) throws IOException, InterruptedException {
292+
private static void takeIDeviceScreenshot(String fullFileName) throws Exception {
256293
String udid = System.getenv("UDID");
294+
if (udid==null){
295+
throw new Exception("$UDID was null, set UDID environment variable and try again");
296+
}
257297
String[] cmd = new String[]{"idevicescreenshot", "-u", udid, fullFileName};
258298
Process p = Runtime.getRuntime().exec(cmd);
259299
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));

0 commit comments

Comments
 (0)