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

Commit 088916e

Browse files
author
Severi Haverila
committed
created dtos for paramteres
1 parent 382a3c2 commit 088916e

File tree

4 files changed

+144
-56
lines changed

4 files changed

+144
-56
lines changed

src/main/java/AbstractAppiumTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,15 @@ public static AppiumDriver getAndroidDriver() throws Exception {
128128
return driver;
129129
}
130130

131-
public static void takeScreenshot(String screenshotName) throws IOException, InterruptedException {
131+
public static String takeScreenshot(String screenshotName) throws IOException, InterruptedException {
132132
if (idevicescreenshotExists) {
133133
// Keep Appium session alive between multiple non-driver screenshots
134134
driver.manage().window().getSize();
135135
}
136136

137137
long start_time = System.nanoTime();
138-
String fullFileName = System.getProperty("user.dir") + "/" + screenshotsFolder + screenshotName + ".png";
138+
String screenshotFile = screenshotsFolder + screenshotName + ".png";
139+
String fullFileName = System.getProperty("user.dir") + "/" + screenshotFile;
139140

140141
if (platformName.equalsIgnoreCase("iOS") && idevicescreenshotExists) {
141142
takeIDeviceScreenshot(fullFileName);
@@ -145,6 +146,7 @@ public static void takeScreenshot(String screenshotName) throws IOException, Int
145146
long end_time = System.nanoTime();
146147
int difference = (int) ((end_time - start_time) / 1e6 / 1000);
147148
logger.info("==> Taking a screenshot took " + difference + " secs.");
149+
return screenshotFile;
148150
}
149151

150152
private static void takeAppiumScreenshot(String fullFileName) {

src/main/java/TestdroidImageRecognition.java

Lines changed: 42 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
import org.slf4j.Logger;
77
import org.slf4j.LoggerFactory;
88

9+
import dtos.ImageRecognitionSettingsDTO;
10+
import dtos.ImageSearchDTO;
11+
912
import java.io.*;
1013

1114
import static org.junit.Assert.assertNotNull;
@@ -17,12 +20,7 @@
1720
public class TestdroidImageRecognition extends AbstractAppiumTest {
1821

1922
public Logger logger = LoggerFactory.getLogger(TestdroidImageRecognition.class);
20-
private final int DEFAULT_RETRIES = 5;
21-
private final int DEFAULT_RETRY_WAIT = 0;
22-
private final double DEFAULT_TOLERANCE = 0.6;
23-
private final boolean DEFAULT_WITH_ASSERT = true;
24-
private final boolean DEFAULT_TAKE_SCREENSHOT = true;
25-
private final boolean DEFAULT_CROP = false;
23+
2624
AkazeImageFinder imageFinder = new AkazeImageFinder();
2725

2826
private String queryimageFolder = "";
@@ -200,62 +198,52 @@ public void tapAtRelativeCoordinates(double x_offset, double y_offset) throws Ex
200198
// "image" is the searched image name
201199
// "retries" is the number of times the method will try to find the searched image. If not set, default is 5.
202200
// "tolerance" sets the required accuracy for the image recognition algorithm.
203-
// "with_assert" specifies if the method will return a fail or not if the searched image is not found on the screen. Use findImageOnScreenNoAssert() to have this set by default to FALSE
204-
public Point[] findImageOnScreen(String image, int retries, int retryWait, double tolerance, boolean withAssert, boolean take_screenshot, boolean crop) throws Exception {
205-
Point[] imgRect = null;
206-
boolean new_step = true;
207-
long start_time = System.nanoTime();
208-
int originalRetries = retries;
209-
while ((retries > 0) && (imgRect == null)) {
210-
if (retries < originalRetries) {
211-
if (retryWait > 0) {
212-
log("retryWait given, sleeping " + retryWait + " seconds.");
213-
sleep(retryWait);
214-
}
215-
new_step = false;
216-
}
217-
218-
log("Find image started, retries left: " + retries);
219-
if (take_screenshot)
220-
takeScreenshot(image + "_screenshot");
221-
222-
// queryImageFolder is "", unless set by setQueryImageFolder()
223-
String queryImageFile = "queryimages/" + queryimageFolder + image + "_screenshot";
224-
String screenshotFile = screenshotsFolder+image + "_screenshot";
225-
226-
imgRect = findImage(queryImageFile, screenshotFile, tolerance);
227-
retries = retries - 1;
228-
}
229-
230-
long end_time = System.nanoTime();
231-
int difference = (int) ((end_time - start_time) / 1e6 / 1000);
232-
log("==> Find image took: " + difference + " secs.");
233-
234-
if (withAssert) {
235-
assertNotNull("Image " + image + " not found on screen.", imgRect);
236-
}
237-
238-
if (crop) {
201+
public Point[] findImageOnScreen(String image, ImageRecognitionSettingsDTO settings) throws Exception {
202+
ImageSearchDTO foundImage = findImageLoop(image, settings);
203+
if (foundImage.isFound() && settings.isCrop()) {
204+
Point[] imgRect = foundImage.getImageRectangle();
239205
Point top_left = imgRect[0];
240206
Point top_right = imgRect[1];
241207
Point bottom_left = imgRect[2];
242208
Point center = imgRect[4];
243-
imageFinder.cropImage(screenshotsFolder + getScreenshotsCounter() + "_" + image + "_screenshot" + getRetryCounter() + "_" + timeDifferenceStartTest + "sec", top_left.x, top_left.y, top_right.x - top_left.x, bottom_left.y - top_left.y);
209+
imageFinder.cropImage(foundImage.getScreenshotFile(), top_left.x, top_left.y, top_right.x - top_left.x, bottom_left.y - top_left.y);
244210
}
245-
return imgRect;
211+
return foundImage.getImageRectangle();
246212
}
247213

248-
public Point[] findImageOnScreen(String image, int retries, int retryWait, double tolerance, boolean withAssert, boolean takeScreenshot) throws Exception {
249-
return findImageOnScreen(image, retries, retryWait, tolerance, withAssert, takeScreenshot, DEFAULT_CROP);
250-
}
251-
252-
253-
public Point[] findImageOnScreen(String image) throws Exception {
254-
return findImageOnScreen(image, DEFAULT_RETRIES, DEFAULT_RETRY_WAIT, DEFAULT_TOLERANCE, DEFAULT_WITH_ASSERT, DEFAULT_TAKE_SCREENSHOT, DEFAULT_CROP);
255-
}
214+
private ImageSearchDTO findImageLoop(String image, ImageRecognitionSettingsDTO settings) throws InterruptedException, IOException, Exception {
215+
long start_time = System.nanoTime();
216+
ImageSearchDTO foundImageDto = new ImageSearchDTO();
217+
for (int i = 0; i < settings.getRetries(); i++) {
218+
// queryImageFolder is "", unless set by setQueryImageFolder()
219+
String queryImageFile = "queryimages/" + queryimageFolder + image + "_screenshot";
220+
String screenshotFile = takeScreenshot(image + "_screenshot");
221+
Point[] imgRect = findImage(queryImageFile, screenshotFile, settings.getTolerance());
222+
if (imgRect!=null){
223+
long end_time = System.nanoTime();
224+
int difference = (int) ((end_time - start_time) / 1e6 / 1000);
225+
log("==> Find image took: " + difference + " secs.");
226+
227+
foundImageDto.setImageRectangle(imgRect);
228+
foundImageDto.setScreenshotFile(screenshotFile);
229+
return foundImageDto;
230+
}
231+
retryWait(settings);
232+
}
233+
log("==> Image not found");
234+
return foundImageDto;
235+
}
236+
237+
private void retryWait(ImageRecognitionSettingsDTO settings) throws InterruptedException {
238+
if (settings.getRetryWaitTime() > 0) {
239+
log("retryWait given, sleeping " + settings.getRetryWaitTime() + " seconds.");
240+
sleep(settings.getRetryWaitTime());
241+
}
242+
}
256243

257-
public Point[] findImageOnScreen(String image, boolean take_screenshot) throws Exception {
258-
return findImageOnScreen(image, DEFAULT_RETRIES, DEFAULT_RETRY_WAIT, DEFAULT_TOLERANCE, DEFAULT_WITH_ASSERT, take_screenshot, DEFAULT_CROP);
244+
public Point[] findImageOnScreen(String image) throws Exception {
245+
ImageRecognitionSettingsDTO defaultSettings = new ImageRecognitionSettingsDTO();
246+
return findImageOnScreen(image, defaultSettings);
259247
}
260248

261249
//Searches for an image until it disappears from the current view. Good for checking if a loading screen has disappeared.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package dtos;
2+
3+
public class ImageRecognitionSettingsDTO {
4+
5+
private final int DEFAULT_RETRIES = 5;
6+
private final int DEFAULT_RETRY_WAIT = 0;
7+
private final double DEFAULT_TOLERANCE = 0.6;
8+
private final boolean DEFAULT_CROP = false;
9+
10+
public ImageRecognitionSettingsDTO(){
11+
this.retries = DEFAULT_RETRIES;
12+
this.retryWaitTime = DEFAULT_RETRY_WAIT;
13+
this.tolerance = DEFAULT_TOLERANCE;
14+
this.crop = DEFAULT_CROP;
15+
}
16+
17+
18+
public int getRetries() {
19+
return retries;
20+
}
21+
public void setRetries(int retries) {
22+
this.retries = retries;
23+
}
24+
25+
26+
public int getRetryWaitTime() {
27+
return retryWaitTime;
28+
}
29+
30+
31+
public void setRetryWaitTime(int retryWaitTime) {
32+
this.retryWaitTime = retryWaitTime;
33+
}
34+
35+
36+
public double getTolerance() {
37+
return tolerance;
38+
}
39+
40+
41+
public void setTolerance(double tolerance) {
42+
this.tolerance = tolerance;
43+
}
44+
45+
46+
public boolean isCrop() {
47+
return crop;
48+
}
49+
50+
51+
public void setCrop(boolean crop) {
52+
this.crop = crop;
53+
}
54+
55+
56+
private int retries;
57+
private int retryWaitTime;
58+
private double tolerance;
59+
private boolean crop;
60+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package dtos;
2+
3+
import org.opencv.core.Point;
4+
5+
public class ImageSearchDTO {
6+
7+
public ImageSearchDTO(){
8+
this.screenshotFile = null;
9+
this.imageRectangle = null;
10+
}
11+
12+
public ImageSearchDTO(String screenshotFile, Point[] imageRectangle){
13+
this.screenshotFile = screenshotFile;
14+
this.imageRectangle = imageRectangle;
15+
}
16+
17+
public boolean isFound(){
18+
return screenshotFile!=null && imageRectangle!=null;
19+
}
20+
21+
public String getScreenshotFile() {
22+
return screenshotFile;
23+
}
24+
public void setScreenshotFile(String screenshotFile) {
25+
this.screenshotFile = screenshotFile;
26+
}
27+
28+
public Point[] getImageRectangle() {
29+
return imageRectangle;
30+
}
31+
32+
public void setImageRectangle(Point[] imageRectangle) {
33+
this.imageRectangle = imageRectangle;
34+
}
35+
36+
private String screenshotFile;
37+
private Point[] imageRectangle;
38+
}

0 commit comments

Comments
 (0)