简体   繁体   English

如何在Android设备的Mac OS上以Java编程方式执行ADB命令

[英]How to execute ADB commands programmatically in Java on Mac OS for Android device

everybody! 大家! I'm trying to write listener for my testing framework using Appium, java, TestNG for Android application on real device. 我正在尝试使用Appium,java,TestNG为真实设备上的Android应用程序为我的测试框架编写侦听器。 I want to use some ADB commands through my tests, for example take screenshot, record video, get logcat file, etc 我想在测试中使用一些ADB命令,例如获取屏幕截图,录制视频,获取logcat文件等

So I have: Mac OS Android device Xiaomi, with Android version 7.0 ADB ANdroid SDK java 所以我有:Mac OS Android设备Xiaomi,具有Android 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. 如果我在终端中使用命令:adb exec-out screencap -p> /Users/dmytro/Documents/other/1.png-屏幕截图已成功保存在适当的文件夹中。 And other commands work fine manually typed in terminal and in terminal of my IntelijIDEA as well. 在终端和我的IntelijIDEA的终端中手动键入其他命令也可以正常工作。

If I use the same command programmatically by using Runtime class: 如果我通过使用Runtime类以编程方式使用同一命令:

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. 它不起作用,屏幕截图不会保存到Mac机器,甚至无法在android设备上找到。

No any error I received, looks like everything works. 我没有收到任何错误,看起来一切正常。 see screenshot from debug mode. 请参阅调试模式的屏幕截图。 screenShot从调试模式

Actually, some adb commands worked fine on WIndows OS, but on Mac it don't. 实际上,某些adb命令在Windows OS上运行良好,但在Mac上却不能。

Help me execute ADB commands in java code please. 请帮我用Java代码执行ADB命令。

The adb commands are succeeding, but not redirected to a file as expected. adb命令成功执行,但未按预期重定向到文件。 With Java Runtime.getRuntime().exec() , the > for redirect won't work. 使用Java Runtime.getRuntime().exec() ,用于重定向的>无效。 Explanation for this can be found from this StackOverflow question: Runtime's exec() method is not redirecting the output 可以从以下StackOverflow问题中找到对此的解释: 运行时的exec()方法未重定向输出

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? adb logcat本身也具有-f标志,该标志adb logcat该过程写入文件,如本讨论所述:以编程方式捕获LogCat还是将其导出到文件?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM