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

Commit 88b20f5

Browse files
author
Severi Haverila
committed
android screenshot without appium
1 parent b292e14 commit 88b20f5

File tree

3 files changed

+35
-11
lines changed

3 files changed

+35
-11
lines changed

src/main/java/AppiumCommons.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1+
import java.io.File;
2+
import java.io.IOException;
3+
4+
import org.apache.commons.io.FileUtils;
15
import org.openqa.selenium.Dimension;
6+
import org.openqa.selenium.OutputType;
27
import org.openqa.selenium.Point;
38
import org.slf4j.Logger;
49
import org.slf4j.LoggerFactory;
@@ -17,6 +22,17 @@ public static void hideKeyboard(AppiumDriver<MobileElement> driver) {
1722
logger.debug(e.toString());
1823
}
1924
}
25+
26+
public static void takeAppiumScreenshot(AppiumDriver<MobileElement> driver, String fullFileName) {
27+
File scrFile = driver.getScreenshotAs(OutputType.FILE);
28+
try {
29+
File testScreenshot = new File(fullFileName);
30+
FileUtils.copyFile(scrFile, testScreenshot);
31+
logger.info("Screenshot stored to {}", testScreenshot.getAbsolutePath());
32+
} catch (IOException e) {
33+
e.printStackTrace();
34+
}
35+
}
2036

2137
public static void swipeUp(AppiumDriver<MobileElement> driver) {
2238
swipeUp(driver, 0.15f, 0.15f);

src/main/java/TestdroidImageRecognition.java

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -235,28 +235,36 @@ public String takeScreenshot(String screenshotName) throws IOException, Interrup
235235
String screenshotFile = screenshotsFolder + screenshotName + ".png";
236236
String fullFileName = System.getProperty("user.dir") + "/" + screenshotFile;
237237

238-
if (platformName.equalsIgnoreCase("iOS") && idevicescreenshotExists) {
238+
if (platformName.equalsIgnoreCase("iOS")) {
239239
takeIDeviceScreenshot(fullFileName);
240240
} else {
241-
takeAppiumScreenshot(fullFileName);
241+
takeAndroidScreenshot(fullFileName);
242242
}
243243
long end_time = System.nanoTime();
244244
int difference = (int) ((end_time - start_time) / 1e6 / 1000);
245245
logger.info("==> Taking a screenshot took " + difference + " secs.");
246246
return screenshotFile;
247247
}
248248

249-
private void takeAppiumScreenshot(String fullFileName) {
250-
File scrFile = driver.getScreenshotAs(OutputType.FILE);
251-
try {
252-
File testScreenshot = new File(fullFileName);
253-
FileUtils.copyFile(scrFile, testScreenshot);
254-
logger.info("Screenshot stored to {}", testScreenshot.getAbsolutePath());
255-
} catch (IOException e) {
256-
e.printStackTrace();
249+
private void takeAndroidScreenshot(String fullFileName) throws IOException, InterruptedException {
250+
log("Taking android screenshot...");
251+
log(fullFileName);
252+
String[] cmd = new String[]{"screenshot2", "-d", fullFileName};
253+
Process p = Runtime.getRuntime().exec(cmd);
254+
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
255+
String line;
256+
while ((line = in.readLine()) != null)
257+
log(line);
258+
259+
int exitVal = p.waitFor();
260+
if (exitVal != 0) {
261+
log("screenshot2 process exited with value: " + exitVal);
257262
}
258263
}
259264

265+
266+
267+
260268
private static void takeIDeviceScreenshot(String fullFileName) throws IOException, InterruptedException {
261269
String[] cmd = new String[]{"idevicescreenshot", "-u", udid, fullFileName};
262270
Process p = Runtime.getRuntime().exec(cmd);

src/test/java/AndroidSample.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public void mainPageTest() throws Exception {
5555

5656
AppiumCommons.hideKeyboard(driver);
5757

58-
findImageOnScreen("bitbar_logo");
58+
findImageOnScreen("bitbar_logo.png");
5959
log("Success.");
6060
}
6161
}

0 commit comments

Comments
 (0)