Search code examples
appiumui-automationappium-android

getDeviceName and getVersion runtime using Appium


Consider I am running my tests using Appium in multiple Real device.

is it possible to get getDeviceName and getVersion runtime using Appium.

Because i need to take screenshots and save it in a folder with that Devicename


Solution

  • I can show you how to get a list of running devices and emulators but I have no code to get the versions they are running.

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