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

Commit 8e0fc82

Browse files
author
Severi Haverila
committed
project now has no compilation errors
1 parent fa55d86 commit 8e0fc82

File tree

3 files changed

+77
-88
lines changed

3 files changed

+77
-88
lines changed

src/main/java/AbstractAppiumTest.java

Lines changed: 1 addition & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -128,57 +128,7 @@ public static AppiumDriver getAndroidDriver() throws Exception {
128128
return driver;
129129
}
130130

131-
public static String takeScreenshot(String screenshotName) throws IOException, InterruptedException {
132-
if (idevicescreenshotExists) {
133-
// Keep Appium session alive between multiple non-driver screenshots
134-
driver.manage().window().getSize();
135-
}
136-
137-
long start_time = System.nanoTime();
138-
String screenshotFile = screenshotsFolder + screenshotName + ".png";
139-
String fullFileName = System.getProperty("user.dir") + "/" + screenshotFile;
140-
141-
if (platformName.equalsIgnoreCase("iOS") && idevicescreenshotExists) {
142-
takeIDeviceScreenshot(fullFileName);
143-
} else {
144-
takeAppiumScreenshot(fullFileName);
145-
}
146-
long end_time = System.nanoTime();
147-
int difference = (int) ((end_time - start_time) / 1e6 / 1000);
148-
logger.info("==> Taking a screenshot took " + difference + " secs.");
149-
return screenshotFile;
150-
}
151-
152-
private static void takeAppiumScreenshot(String fullFileName) {
153-
File scrFile = driver.getScreenshotAs(OutputType.FILE);
154-
try {
155-
File testScreenshot = new File(fullFileName);
156-
FileUtils.copyFile(scrFile, testScreenshot);
157-
logger.info("Screenshot stored to {}", testScreenshot.getAbsolutePath());
158-
} catch (IOException e) {
159-
e.printStackTrace();
160-
}
161-
}
162-
163-
private static void takeIDeviceScreenshot(String fullFileName) throws IOException, InterruptedException {
164-
String[] cmd = new String[]{"idevicescreenshot", "-u", udid, fullFileName};
165-
Process p = Runtime.getRuntime().exec(cmd);
166-
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
167-
String line;
168-
while ((line = in.readLine()) != null)
169-
log(line);
170-
171-
int exitVal = p.waitFor();
172-
if (exitVal != 0) {
173-
log("idevicescreenshot process exited with value: " + exitVal);
174-
}
175-
cmd = new String[]{"sips", "-s", "format", "png", fullFileName, "--out", fullFileName};
176-
p = Runtime.getRuntime().exec(cmd);
177-
exitVal = p.waitFor();
178-
if (exitVal != 0) {
179-
log("sips process exited with value: " + exitVal);
180-
}
181-
}
131+
182132

183133
//On a test run on the local machine this method will save the Reports folder in different folders on every test run.
184134
public static void savePreviousRunReports() {

src/main/java/TestdroidImageRecognition.java

Lines changed: 70 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
import library.AkazeImageFinder;
33
import library.ImageRecognition;
44

5+
import org.apache.commons.io.FileUtils;
56
import org.opencv.core.Point;
67
import org.openqa.selenium.By;
78
import org.openqa.selenium.Dimension;
9+
import org.openqa.selenium.OutputType;
810
import org.openqa.selenium.interactions.touch.TouchActions;
911
import org.slf4j.Logger;
1012
import org.slf4j.LoggerFactory;
@@ -105,44 +107,23 @@ public Point[] findImageOnScreen(String image) throws Exception {
105107
return findImageOnScreen(image, defaultSettings);
106108
}
107109

108-
//Searches for an image until it disappears from the current view. Good for checking if a loading screen has disappeared.
109-
public void waitForImageToDisappearFromScreen(String image) throws Exception {
110-
boolean first_time = true;
111-
boolean check = true;
112-
long start, present;
113-
start = System.nanoTime();
114-
present = start;
115-
110+
public boolean waitForImageToDisappearFromScreen(String image) throws Exception {
111+
long start = System.nanoTime();
116112
log("==> Trying to find image: " + image);
117-
118-
while ((check) && ((present - start) / 1e6 / 1000 < 300)) {
119-
120-
if (first_time) {
121-
first_time = false;
122-
takeScreenshot(image + "_screenshot", true);
123-
if ((findImage(image, image + "_screenshot" + getRetryCounter())) == null) {
124-
log("Loading screen not found. Moving on");
125-
check = false;
126-
} else {
127-
sleep(3);
128-
}
129-
} else {
130-
takeScreenshot(image + "_screenshot", false);
131-
if ((findImage(image, image + "_screenshot" + getRetryCounter())) == null) {
132-
log("Loading screen not found. Moving on");
133-
check = false;
134-
} else {
135-
sleep(3);
136-
}
137-
}
138-
139-
present = System.nanoTime();
140-
141-
if ((present - start) / 1e6 / 1000 >= 300) {
142-
fail("Application takes too long to load: Stopping tests.....");
143-
check = false;
144-
}
113+
int retry_counter=0;
114+
String queryImageFile = "queryimages/" + queryimageFolder + image;
115+
Dimension screenSize = getScreenSizeADB();
116+
117+
while (((System.nanoTime() - start) / 1e6 / 1000 < 300)) {
118+
String screenShotFile = takeScreenshot(image + "_screenshot_"+retry_counter);
119+
if ((ImageRecognition.findImage(queryImageFile, screenShotFile, platformName, screenSize)) == null) {
120+
log("Image has successfully disappeared from screen.");
121+
return true;
122+
}
123+
sleep(3);
145124
}
125+
logger.warn("Image did not disappear from screen");
126+
return false;
146127
}
147128

148129

@@ -247,6 +228,59 @@ public String grabTextFromImage(String image) throws Exception {
247228
String text = ImageRecognition.getTextStringFromImage(imageSearch.getScreenshotFile());
248229
return text;
249230
}
231+
232+
233+
public String takeScreenshot(String screenshotName) throws IOException, InterruptedException {
234+
if (idevicescreenshotExists) {
235+
// Keep Appium session alive between multiple non-driver screenshots
236+
driver.manage().window().getSize();
237+
}
238+
239+
long start_time = System.nanoTime();
240+
String screenshotFile = screenshotsFolder + screenshotName + ".png";
241+
String fullFileName = System.getProperty("user.dir") + "/" + screenshotFile;
242+
243+
if (platformName.equalsIgnoreCase("iOS") && idevicescreenshotExists) {
244+
takeIDeviceScreenshot(fullFileName);
245+
} else {
246+
takeAppiumScreenshot(fullFileName);
247+
}
248+
long end_time = System.nanoTime();
249+
int difference = (int) ((end_time - start_time) / 1e6 / 1000);
250+
logger.info("==> Taking a screenshot took " + difference + " secs.");
251+
return screenshotFile;
252+
}
253+
254+
private void takeAppiumScreenshot(String fullFileName) {
255+
File scrFile = driver.getScreenshotAs(OutputType.FILE);
256+
try {
257+
File testScreenshot = new File(fullFileName);
258+
FileUtils.copyFile(scrFile, testScreenshot);
259+
logger.info("Screenshot stored to {}", testScreenshot.getAbsolutePath());
260+
} catch (IOException e) {
261+
e.printStackTrace();
262+
}
263+
}
264+
265+
private static void takeIDeviceScreenshot(String fullFileName) throws IOException, InterruptedException {
266+
String[] cmd = new String[]{"idevicescreenshot", "-u", udid, fullFileName};
267+
Process p = Runtime.getRuntime().exec(cmd);
268+
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
269+
String line;
270+
while ((line = in.readLine()) != null)
271+
log(line);
272+
273+
int exitVal = p.waitFor();
274+
if (exitVal != 0) {
275+
log("idevicescreenshot process exited with value: " + exitVal);
276+
}
277+
cmd = new String[]{"sips", "-s", "format", "png", fullFileName, "--out", fullFileName};
278+
p = Runtime.getRuntime().exec(cmd);
279+
exitVal = p.waitFor();
280+
if (exitVal != 0) {
281+
log("sips process exited with value: " + exitVal);
282+
}
283+
}
250284

251285

252286

src/main/java/library/ImageRecognition.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,12 @@ private static void log(String message) {
2121
}
2222

2323

24-
24+
public static Point[] findImage(String image, String scene, String platformName, Dimension screenSize) throws Exception {
25+
ImageRecognitionSettingsDTO setting = new ImageRecognitionSettingsDTO();
26+
return findImage(image, scene, setting, platformName, screenSize);
27+
}
28+
29+
2530
//This method calls on the Akaze scripts to find the coordinates of a given image in another image.
2631
//The "image" parameter is the image that you are searching for
2732
//The "scene" parameter is the image in which we are looking for "image"

0 commit comments

Comments
 (0)