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

Commit 67f0ada

Browse files
author
Severi Haverila
committed
updated README and made example code more simplistic
1 parent 0b66c91 commit 67f0ada

File tree

7 files changed

+37
-270
lines changed

7 files changed

+37
-270
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
# Overview
2+
This is an image recognition library that can be used for mobile application testing.
3+
4+
This library has been extracted and refactored from the following project:
5+
<https://github.com/bitbar/testdroid-samples/tree/master/image-recognition>
6+
7+
As this README is currently rather simplistic, more information can be found from the original project's readme or from this blog post: <http://bitbar.com/appium-tip-27-using-appium-for-mobile-game-testing>
8+
19
# Prerequisites
210
## General
311

@@ -35,6 +43,9 @@ cd library
3543
mvn package
3644
cp target/image-recognition-library-2.0-SNAPSHOT.jar ../example/lib/image_recognition_library_test.jar
3745
cd ../example
46+
47+
# Install OpenCV (change path according to your platform)
48+
mvn install:install-file -Dfile=lib/linux/opencv/java7/opencv-2413.jar -DgroupId=opencv -DartifactId=opencv -Dversion=2.4.13 -Dpackaging=jar
3849
3950
# Download the example Android application
4051
wget https://github.com/bitbar/testdroid-samples/blob/master/apps/builds/BitbarSampleApp.apk -O application.apk

example/src/main/java/AbstractAppiumTest.java

Lines changed: 2 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,15 @@
99
import objects.PlatformType;
1010

1111
import java.io.File;
12-
import java.io.IOException;
1312
import java.net.URL;
1413
import java.util.concurrent.TimeUnit;
1514

16-
/**
17-
* Created by testdroid on 22/07/16.
18-
*/
1915
public abstract class AbstractAppiumTest {
2016

21-
public static final int SHORT_SLEEP = 1;
22-
public static final int MEDIUM_SLEEP = 5;
23-
public static final int LONG_SLEEP = 10;
24-
2517
protected static Logger logger = LoggerFactory.getLogger(AbstractAppiumTest.class);
26-
27-
28-
2918
protected static AppiumDriver<MobileElement> driver;
3019
protected static int defaultWaitTime = 120;
3120

32-
public static String screenshotsFolder = "";
3321
public static String appFile = System.getenv("APP_FILE");
3422
public static PlatformType platform;
3523
public static String automationName = System.getenv("AUTOMATION_NAME");
@@ -38,10 +26,8 @@ public abstract class AbstractAppiumTest {
3826
public static String platformVersion = System.getenv("PLATFORM_VERSION");
3927
// Set to false to autoDismiss
4028
public static boolean autoAccept = true;
41-
public static boolean idevicescreenshotExists = false;
42-
4329

44-
public static AppiumDriver getIOSDriver() throws Exception {
30+
public static AppiumDriver<MobileElement> getIOSDriver() throws Exception {
4531
if (appFile == null) {
4632
appFile = "application.ipa";
4733
}
@@ -57,10 +43,6 @@ public static AppiumDriver getIOSDriver() throws Exception {
5743
// Use default "appium" automation for iOS
5844
automationName = "appium";
5945

60-
screenshotsFolder = "target/reports/screenshots/ios/";
61-
File dir = new File(screenshotsFolder);
62-
dir.mkdirs();
63-
6446
DesiredCapabilities capabilities = new DesiredCapabilities();
6547
capabilities.setCapability("platformName", platform.getPlatformName());
6648
capabilities.setCapability("deviceName", deviceName);
@@ -80,15 +62,13 @@ public static AppiumDriver getIOSDriver() throws Exception {
8062
capabilities.setCapability("autoDismissAlerts", true);
8163
}
8264

83-
idevicescreenshotCheck();
84-
8565
log("Creating Appium session, this may take couple minutes..");
8666
driver = new IOSDriver<MobileElement>(new URL("http://localhost:4723/wd/hub"), capabilities);
8767
driver.manage().timeouts().implicitlyWait(defaultWaitTime, TimeUnit.SECONDS);
8868
return driver;
8969
}
9070

91-
public static AppiumDriver getAndroidDriver() throws Exception {
71+
public static AppiumDriver<MobileElement> getAndroidDriver() throws Exception {
9272
if (appFile == null) {
9373
appFile = "application.apk";
9474
}
@@ -101,9 +81,6 @@ public static AppiumDriver getAndroidDriver() throws Exception {
10181
if (automationName == null){
10282
automationName = "appium";
10383
}
104-
screenshotsFolder = "target/reports/screenshots/android/";
105-
File dir = new File(screenshotsFolder);
106-
dir.mkdirs();
10784

10885
DesiredCapabilities capabilities = new DesiredCapabilities();
10986
capabilities.setCapability("automationName", automationName);
@@ -124,51 +101,8 @@ public static AppiumDriver getAndroidDriver() throws Exception {
124101
return driver;
125102
}
126103

127-
128-
129-
//On a test run on the local machine this method will save the Reports folder in different folders on every test run.
130-
public static void savePreviousRunReports() {
131-
long millis = System.currentTimeMillis();
132-
File dir = new File("./target/reports");
133-
File newName = new File("./target/reports" + millis);
134-
if (dir.isDirectory()) {
135-
dir.renameTo(newName);
136-
} else {
137-
dir.mkdir();
138-
dir.renameTo(newName);
139-
}
140-
}
141-
142-
public static boolean idevicescreenshotCheck() throws IOException, InterruptedException {
143-
String[] cmd = new String[]{"idevicescreenshot", "--help"};
144-
int exitVal = -1;
145-
try {
146-
Process p = Runtime.getRuntime().exec(cmd);
147-
exitVal = p.waitFor();
148-
} catch (IOException e) {
149-
log(e.toString());
150-
}
151-
if (exitVal == 0) {
152-
log("idevicescreenshot exited with value: " + exitVal + ". Using it for screenshots.");
153-
idevicescreenshotExists = true;
154-
} else {
155-
log("idevicescreenshot process exited with value: " + exitVal + ". Won't be using it for screenshots.");
156-
idevicescreenshotExists = false;
157-
}
158-
return idevicescreenshotExists;
159-
}
160-
161-
//Stops the script for the given amount of seconds.
162-
public static void sleep(double seconds) throws Exception {
163-
log("Waiting for " + seconds + " sec");
164-
seconds = seconds * 1000;
165-
Thread.sleep((int) seconds);
166-
}
167-
168104
public static void log(String message) {
169105
logger.info(message);
170106
}
171107

172-
173-
174108
}

example/src/main/java/AppiumCommons.java

Lines changed: 0 additions & 144 deletions
This file was deleted.

example/src/main/java/TestdroidImageRecognition.java

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,41 @@
11
import imagerecognition.ImageRecognition;
22

3-
import org.openqa.selenium.Dimension;
3+
import java.io.File;
4+
45
import org.slf4j.Logger;
56
import org.slf4j.LoggerFactory;
67

78
import objects.ImageLocation;
89
import objects.ImageRecognitionSettings;
910
import objects.ImageSearchResult;
1011

11-
12-
/**
13-
* Created by testdroid on 22/07/16.
14-
*/
1512
public class TestdroidImageRecognition extends AbstractAppiumTest {
1613

1714
public Logger logger = LoggerFactory.getLogger(TestdroidImageRecognition.class);
18-
private String queryImageSubFolder = "";
19-
20-
21-
//If this method is called inside a test the script will check if the device has a resolution lower than 500x500 and if so will use
22-
// a different set of images when trying to find a image. These images are located in /queryimages/low_res
23-
public void setQueryImageFolder() {
24-
Dimension size = driver.manage().window().getSize();
25-
log("Screen size: " + size.toString());
26-
if ((size.getHeight() <= 500) || (size.getWidth() <= 500)) {
27-
queryImageSubFolder = "low_res/";
28-
}
15+
String screenshotsFolder;
16+
String queryImageFolder;
17+
18+
public TestdroidImageRecognition(){
19+
super();
20+
screenshotsFolder = "target/reports/screenshots/";
21+
queryImageFolder = "queryimages/";
22+
File dir = new File(screenshotsFolder);
23+
dir.mkdirs();
2924
}
3025

3126
public ImageLocation findImageOnScreen(String image) throws Exception {
3227
ImageRecognitionSettings defaultSettings = new ImageRecognitionSettings();
3328
return findImageOnScreen(image, defaultSettings).getImageLocation();
3429
}
3530

36-
public ImageSearchResult findImageOnScreen(String imageName, ImageRecognitionSettings settings) throws Exception {
37-
// queryImageFolder is "", unless set by setQueryImageFolder()
38-
String queryImageFolder = "queryimages/" + queryImageSubFolder;
39-
String screenshotsFolder = "target/reports/screenshots/";
31+
public ImageSearchResult findImageOnScreen(String imageName, ImageRecognitionSettings settings) throws Exception {
4032
String imageFile = queryImageFolder+imageName;
4133
log("Searching for: "+imageFile);
4234
ImageSearchResult foundImage = ImageRecognition.findImageOnScreen(imageFile, screenshotsFolder, settings, platform);
4335
return foundImage;
4436
}
4537

4638
public void waitForImageToDisappearFromScreen(String image) throws Exception {
47-
String queryImageFolder = "queryimages/" + queryImageSubFolder;
48-
String screenshotsFolder = "target/reports/screenshots/";
4939
String imageFile = queryImageFolder+image;
5040
boolean hasImageDisappeared = ImageRecognition.hasImageDissappearedFromScreenBeforeTimeout(imageFile, screenshotsFolder, platform);
5141
assert(hasImageDisappeared);

0 commit comments

Comments
 (0)