如何在appium测试用例中运行android shell命令?
问题描述:
我是Android和Appium的新手。我有一个使用Appium的java测试用例。 它使用Appium驱动程序命令执行滚动,点击等操作。 在此测试中,我需要报告该应用程序的CPU Util的值。我在网上找到了这个代码。如何在appium测试用例中运行android shell命令?
RandomAccessFile reader = new RandomAccessFile(“/ proc/stat”,“r”); String load = reader.readLine();
String[] toks = load.split(" +"); // Split on one or more spaces
idle1 = Long.parseLong(toks[4]);
cpu1 = Long.parseLong(toks[2]) + Long.parseLong(toks[3]) + Long.parseLong(toks[5])
+ Long.parseLong(toks[6]) + Long.parseLong(toks[7]) + Long.parseLong(toks[8]);
但是,如何在eclipse上的Appium java项目中运行此代码,因为我没有安卓项目?
在此先感谢了很多的帮助
答
抢关于Android设备的信息,您可以利用org.apache.commons.exec.CommandLine
或Runtime
;
https://developer.android.com/studio/profile/investigate-ram.html#ViewingAllocations
这使得一个重要假设,即防止任何类型的可扩展性,这主要是因为必须将设备连接到正在执行测试在同一台机器:adb shell dumpsys meminfo <package_name|pid> [-d]
import java.io.ByteArrayOutputStream;
import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.Executor;
import org.apache.commons.exec.PumpStreamHandler
CommandLine cmd = new CommandLine("adb");
cmd.addArgument("shell", false).addArgument("dumpsys", false).addArgument("meminfo", false).addArgument("YOUR_ANDROID_PACKAGE", false);
DefaultExecutor exec = new DefaultExecutor();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream);
exec.setStreamHandler(streamHandler);
exec.execute(cmd);
outputStream.toString()
另请参见: How can I capture the output of a command as a String with Commons Exec?