2016-08-17 107 views
3

我是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項目中運行此代碼,因爲我沒有安卓項目?

在此先感謝了很多的幫助

回答

0

搶關於Android設備的信息,您可以利用org.apache.commons.exec.CommandLineRuntime;

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?

相關問題