-1

I use Appium to do the android automation testing with Java, when I run the command cmd.exe /c adb shell getprop ro.build.version.release in Java, the test script is hanging. Env: Appium: 1.8, Android Emulator: android 8, Platform: Windows 7,

Here is the original code:

public static String main(final String strCmd) throws Exception {
    String cmdResult = excuteCmd("adb shell getprop ro.build.version.release");
}
public static String excuteCmd(final String strCmd) throws Exception {
    String resultLine;
    String resultCmd = "";
    try {
        Process process = Runtime.getRuntime().exec(strCmd);
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        while ((resultLine = bufferedReader.readLine()) != null) {
            System.out.println(resultLine);
            if (!(resultLine.equalsIgnoreCase(""))) {
                resultCmd = resultLine;
            }
        }
        process.destroy();
    } catch (Exception e) {
        e.printStackTrace();
    }
    System.out.println(resultCmd);
    return resultCmd;
}

Here is the original code:

Can anyone help on this issue?

Blues
  • 22
  • 6
  • 1
    Before I just post my own method to accomplish this, please show your own code in your original post so that we can perhaps determine why it's not working for you. – Bill Hileman Mar 05 '19 at 15:51
  • Is adb in your path? Can you drop to a command prompt and type that same command line and view the results? I'll post my version below, but it relies on the path being set outside of the method, which is another process which validates the path. – Bill Hileman Mar 06 '19 at 14:37

2 Answers2

0

I tried following codes and its working for me for both emulator and real devices:

public static void main(String[] strCmd) throws Exception {
    String cmdResult = excuteCmd("adb shell getprop ro.build.version.release");
}

public static String excuteCmd(final String strCmd) throws Exception {
    String resultLine;
    String resultCmd = "";
    try {
        Process process = Runtime.getRuntime().exec(strCmd);
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        while ((resultLine = bufferedReader.readLine()) != null) {
            System.out.println(resultLine);
            if (!(resultLine.equalsIgnoreCase(""))) {
                resultCmd = resultLine;
            }
        }
        process.destroy();
    } catch (Exception e) {
        e.printStackTrace();
    }
    System.out.println(resultCmd);
    return resultCmd;
}

Make sure your emulator is running.

Suban Dhyako
  • 2,436
  • 4
  • 16
  • 38
0

As stated in my comment, adpPath is defined elsewhere in my class, as it's used for various methods.

/**
 * Get a property value, i.e. ro.build.version.release
 * @author Bill Hileman
 * @param String propName 
 * @return String value
 * @throws Exception
 */
public String getDevProp(String propName) throws Exception {

    String value = "";

    String[] getProp = new String[]{adbPath, "shell", "getprop", propName};
    //Execute the shell command
    Process process = new ProcessBuilder(getProp).start();

    BufferedReader inputStream = new BufferedReader(new InputStreamReader(process.getInputStream()));

    startTime = System.nanoTime();

    System.out.println("Getting device property " + propName);
    // wait till the property returns expected value

    value = inputStream.readLine();

    while ("".equals(value)) {
        process.waitFor(1, TimeUnit.SECONDS);
        process.destroy();
        process = new ProcessBuilder(getProp).start();
        inputStream = new BufferedReader(new InputStreamReader(process.getInputStream()));
        value = inputStream.readLine();
    }

    elapsedTime = System.nanoTime() - startTime;
    System.out.println("Returned '" + value + "' - " + 
                       TimeUnit.SECONDS.convert(elapsedTime, TimeUnit.NANOSECONDS) + " seconds elapsed");
    process.destroy();

    return value;

}
Bill Hileman
  • 2,798
  • 2
  • 17
  • 24