在Java的模拟器上执行ADB Shell时无响应

时间:2019-03-05 14:24:30

标签: adb appium appium-android

我使用Appium用Java进行android自动化测试,当我在Java中运行命令cmd.exe /c adb shell getprop ro.build.version.release时,测试脚本已挂起。 信封: 阿皮:1.8, Android模拟器:Android 8, 平台:Windows 7,

这是原始代码:

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;
}

这是原始代码:

有人可以帮助解决这个问题吗?

2 个答案:

答案 0 :(得分:0)

我尝试了以下代码及其对模拟器和真实设备的工作:

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;
}
  

确保模拟器正在运行。

答案 1 :(得分:0)

正如我的评论所述,adpPath在类的其他地方定义,因为它用于各种方法。

/**
 * 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;

}