Showing posts with label Android. Show all posts
Showing posts with label Android. Show all posts

Road to capture Android Home path from system env

Use below code to read Android_Home path which you have setup in your System.

    public static String getAndroidPath() {
        String androidHome = null;
        Map env = System.getenv();
        for (String envName : env.keySet()) {
            if (envName.equals("ANDROID_HOME"))

                androidHome = env.get(envName);
        }
        if (androidHome.equals(null))
            throw new NullPointerException(
                    "Android Home path not set in machine");
        return androidHome;
    }




Road to capture list all attached android devices with machine

While running Appium test suite, some time we need to execute test parallel on all attached devices, So the question is here that how we can capture all devices udid and pass devices id in appium desire capabilities. I created a function which return list of udid of all attached devices with machine.


public static List getAttachedDevicesList() {

            List devicesID = new ArrayList();
            try {
                    Process process = Runtime.getRuntime().exec(getAndroidPath() + "//platform-tools//adb devices");
                    BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
                   String s;
                   while ((s = reader.readLine()) != null) {

                          if (s.contains("device") && !s.contains("attached")) {
                                      String[] device = s.split("\t");
                                     devicesID.add(device[0]);
                              }
                   }

          } catch (IOException e) {
                        logger.debug("adb command not executed to capture devices");
           }
          return devicesID;
    }


Note:  getAndroidPath() function defined in link Android_Home

Road to automate Network feature of device using Appium

Appium provide NetworkConnectionSetting class using this we can disable and enable network setting of mobile.
Below example show you how to enable and disable network of mobile using Appium.


public void testNetwork()

                    NetworkConnectionSetting networkConnection  = (AndroidDriver driver).getNetworkConnection();

                   networkConnection.setWifi(false);

                    networkConnection.setAirplaneMode(false);

                    networkConnection.setData(true);

                  ((AndroidDriver )driver).setNetworkConnection(networkConnection);

                  networkConnection = ((AndroidDriver )driver).getNetworkConnection();

                 System.out.println("Aireplane Mode status "+networkConnection.airplaneModeEnabled());

                   System.out.println("Data Mode status "+networkConnection.dataEnabled());

                   System.out.println("Wifi Mode status "+networkConnection.wifiEnabled());

}