From 4efdac2ffb3fe57777fe8943b5dcb398f0d24cd1 Mon Sep 17 00:00:00 2001 From: bootstraponline Date: Thu, 25 Jul 2013 13:24:18 -0400 Subject: [PATCH] Use uiautomator screenshot uiautomator has built in support for taking screenshots. There's no need for an external jar. --- .../bootstrap/AndroidCommandExecutor.java | 2 ++ .../bootstrap/handler/TakeScreenshot.java | 33 +++++++++++++++++++ app/android.js | 21 ++---------- 3 files changed, 37 insertions(+), 19 deletions(-) create mode 100644 android/bootstrap/src/io/appium/android/bootstrap/handler/TakeScreenshot.java diff --git a/android/bootstrap/src/io/appium/android/bootstrap/AndroidCommandExecutor.java b/android/bootstrap/src/io/appium/android/bootstrap/AndroidCommandExecutor.java index 87ff8fdcc..efb5e37c3 100644 --- a/android/bootstrap/src/io/appium/android/bootstrap/AndroidCommandExecutor.java +++ b/android/bootstrap/src/io/appium/android/bootstrap/AndroidCommandExecutor.java @@ -17,6 +17,7 @@ import io.appium.android.bootstrap.handler.PressKeyCode; import io.appium.android.bootstrap.handler.ScrollTo; import io.appium.android.bootstrap.handler.SetText; import io.appium.android.bootstrap.handler.Swipe; +import io.appium.android.bootstrap.handler.TakeScreenshot; import io.appium.android.bootstrap.handler.TouchLongClick; import io.appium.android.bootstrap.handler.WaitForIdle; import io.appium.android.bootstrap.handler.Wake; @@ -54,6 +55,7 @@ class AndroidCommandExecutor { map.put("pressBack", new PressBack()); map.put("dumpWindowHierarchy", new DumpWindowHierarchy()); map.put("pressKeyCode", new PressKeyCode()); + map.put("takeScreenshot", new TakeScreenshot()); } /** diff --git a/android/bootstrap/src/io/appium/android/bootstrap/handler/TakeScreenshot.java b/android/bootstrap/src/io/appium/android/bootstrap/handler/TakeScreenshot.java new file mode 100644 index 000000000..bd162a200 --- /dev/null +++ b/android/bootstrap/src/io/appium/android/bootstrap/handler/TakeScreenshot.java @@ -0,0 +1,33 @@ +package io.appium.android.bootstrap.handler; + +import io.appium.android.bootstrap.AndroidCommand; +import io.appium.android.bootstrap.AndroidCommandResult; +import io.appium.android.bootstrap.CommandHandler; + +import java.io.File; + +import com.android.uiautomator.core.UiDevice; + +/** + * This handler is used to TakeScreenshot. + * + */ +public class TakeScreenshot extends CommandHandler { + + /* + * @param command The {@link AndroidCommand} used for this handler. + * + * @return {@link AndroidCommandResult} + * + * @throws JSONException + * + * @see io.appium.android.bootstrap.CommandHandler#execute(io.appium.android. + * bootstrap.AndroidCommand) + */ + @Override + public AndroidCommandResult execute(final AndroidCommand command) { + final boolean result = UiDevice.getInstance().takeScreenshot( + new File("/data/local/tmp/screenshot.png")); + return getSuccessResult(result); + } +} \ No newline at end of file diff --git a/app/android.js b/app/android.js index c1eaa490d..d4afdd910 100644 --- a/app/android.js +++ b/app/android.js @@ -679,30 +679,13 @@ Android.prototype.getScreenshot = function(cb) { me.adb.requireDeviceId(); var localfile = temp.path({prefix: 'appium', suffix: '.png'}); var b64data = ""; - var jar = path.resolve(__dirname, '..', 'app', 'android', 'ScreenShooter.jar'); - var jarpath = path.resolve(process.env.ANDROID_HOME, "tools", "lib"); - var classpath = ""; async.series([ function(cb) { - if (isWindows) { - classpath = path.resolve(jarpath, "ddmlib.jar") + ";" + path.resolve(jarpath, "x86", "swt.jar") + ";" + jar; - cb(null); - } else { - exec('uname -m', { maxBuffer: 524288 }, function (error, stdout, stderr) { - if (error) { - cb(error); - } else { - classpath = path.resolve(jarpath, "ddmlib.jar") + ":" + path.resolve(jarpath, stdout.trim(), "swt.jar") + ":" + jar; - cb(null); - } - }); - } + me.proxy(["takeScreenshot"], cb); }, function(cb) { - var javaCmd = 'java -classpath "' + classpath + '" io.appium.android.screenshooter.ScreenShooter '; - - var cmd = javaCmd + me.adb.curDeviceId + ' "' + localfile + '"'; + var cmd = me.adb.adbCmd + ' pull /data/local/tmp/screenshot.png "' + localfile + '"'; logger.debug("screenshot cmd: " + cmd); exec(cmd, { maxBuffer: 524288 }, function(err, stdout, stderr) { if (err) {