使用Appium的getDeviceName和getVersion运行时

时间:2018-05-03 11:43:12

标签: appium ui-automation appium-android

考虑我在多个Real设备中使用Appium运行我的测试。

是否可以使用Appium获取getDeviceName和getVersion运行时。

因为我需要截取屏幕截图并将其保存在具有该Devicename的文件夹中

1 个答案:

答案 0 :(得分:1)

我可以向您展示如何获取正在运行的设备和模拟器的列表,但我没有代码来获取它们正在运行的版本。

/**
 * Determine already connected physical devices or already running emulators
 * @author Bill Hileman
 * @return String List of running/connected devices
 */
public List<String> getRunningDevicesList() {

    List<String> dev = new ArrayList<String>();

    //Location of the Android SDK
    String sdkPath = System.getenv("ANDROID_HOME");

    if (sdkPath == null) {
        System.err.println("ANDROID_HOME is not set in the environment");
        Assert.fail();
    } else {
        //Location of Android Debug Bridge
        String adbPath = sdkPath  + "/platform-tools/adb.exe";
        if (!new File(adbPath).exists()) {
            System.err.println(adbPath + " is not found");
            Assert.fail();
        } else {
            try {
                String[] commandListAVDs = new String[]{adbPath, "devices"};
                System.out.println("Executing command: " + adbPath + " devices");
                Process process = new ProcessBuilder(commandListAVDs).start();
                BufferedReader inputStream = new BufferedReader(
                new InputStreamReader(process.getInputStream()));
                String line = null;
                while ((line = inputStream.readLine()) != null)
                    //ignore lines that do not contain device information
                    if (!(line.trim().equals("") || line.equals("List of devices attached") || line.startsWith("* daemon ")))
                        dev.add(line.trim());
            } catch (Exception e) {
                System.err.println("Unable to read device list: " + e);
                e.printStackTrace();
            }
            System.out.println("Running Devices: " + dev.toString());
        }
    }

    return dev;

}