首页 > 解决方案 > 使用 Appium 的 getDeviceName 和 getVersion 运行时

问题描述

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

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

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

标签: appiumui-automationappium-android

解决方案


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

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

}

推荐阅读