4

I am new to Android and Appium. I have a java test case that uses Appium. It performs actions like scroll, tap etc using Appium driver commands. In this test I need to report the value of the CPU Util of tha Application. I found this peice of code online.

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]);

However how do I run this code inside the Appium java project on eclipse since I do not have an android project?

Thanks a lot for the help in advance

Sriram Manohar
  • 313
  • 3
  • 10

1 Answers1

0

To grab information about the Android device, you can leverage org.apache.commons.exec.CommandLine or with Runtime;

https://developer.android.com/studio/profile/investigate-ram.html#ViewingAllocations

This makes a major assumption that prevent any kind of scalability primarily because the device must be connected to the same machine that is executing the test: 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()

See also: How can I capture the output of a command as a String with Commons Exec?

Community
  • 1
  • 1
Son-Huy Pham
  • 1,899
  • 18
  • 19