0

everybody! I'm trying to write listener for my testing framework using Appium, java, TestNG for Android application on real device. I want to use some ADB commands through my tests, for example take screenshot, record video, get logcat file, e.t.c.

So I have: Mac OS Android device Xiaomi, with Android version 7.0 ADB ANdroid SDK java

If I use commands in Terminal: adb exec-out screencap -p > /Users/dmytro/Documents/other/1.png - screenshot successfully saved in appropriate folder. And other commands work fine manually typed in terminal and in terminal of my IntelijIDEA as well.

If I use the same command programmatically by using Runtime class:

Runtime.getRuntime().exec(adb exec-out screencap -p > /Users/dmytro/Documents/other/1.png"); // Save screenshot to Mac machine

or

Runtime.getRuntime().exec("adb logcat > /Users/dmytromynziak/Documents/other/log.txt"); //Save logical file

Runtime.getRuntime().exec("db exec-out screencap -p > /sdcard/1.png"); // Save screenshot to scared of android device

It doesn't work, screenshot is not saved to Mac machine and even can't find on android device.

No any error I received, looks like everything works. see screenshot from debug mode.screenShot from debug mode

Actually, some adb commands worked fine on WIndows OS, but on Mac it don't.

Help me execute ADB commands in java code please.

dmytro Minz
  • 407
  • 1
  • 8
  • 19
  • I'have tried to execute another command by using shell in script: Runtime.getRuntime().exec("adb shell screencap -p sdcard/sc1.png"). and it works! but screen saved into android device, so I need to pull it. So I think problem is that I tried to use commands in my terminal, but programmatically I stay in android device, so need to use shell script – dmytro Minz May 31 '18 at 09:37
  • Do you get any output, if you do `adb devices` from your Java code? – Lasse May 31 '18 at 09:37
  • @Domestus yes, it shows list of devices, so I think problem in executed commands – dmytro Minz May 31 '18 at 09:54

1 Answers1

2

The adb commands are succeeding, but not redirected to a file as expected. With Java Runtime.getRuntime().exec(), the > for redirect won't work. Explanation for this can be found from this StackOverflow question: Runtime's exec() method is not redirecting the output

adb logcat itself also has -f flag, which tells the process to write into a file, as described in this discussion: Catch LogCat programmatically or export it to file?

Lasse
  • 880
  • 5
  • 11