Monday, January 18, 2016

Waiting for android emulator to be ready

When we start android emulator(or is already running), we need to ensure its ready for testing and is responding. 

Command line:
This can be ensured by following commands:
adb shell getprop dev.bootcomplete 
We need to wait for the above command till it returns 1
adb shell getprop sys_bootcomplete 
We need to wait for the above command till it returns 1
adb shell getprop init.svc.bootanim
We need to wait for the above command till it returns "stopped"
Note: 1st and 3rd commands are important to check. Maximum case 2nd command will be ready.

Programmatically:
Also can be done programmatically,
private static String sdkPath = "/Applications/adt-bundle-mac-x86_64-20140702/sdk/";
private static String adbPath = sdkPath + "platform-tools" + File.separator + "adb";
/**
 * Waits for the emulator to be ready
 */
public static void waitForEmulatorToBeReady() {
 try {
  String[] commandBootComplete = new String[] { adbPath, "shell", "getprop", "dev.bootcomplete" };
  Process process = new ProcessBuilder(commandBootComplete).start();
  BufferedReader inputStream = new BufferedReader(new InputStreamReader(process.getInputStream()));

  // wait till the property returns '1'
  while (!inputStream.readLine().equals("1")) {
   process.waitFor(1, TimeUnit.SECONDS);
   process = new ProcessBuilder(commandBootComplete).start();
   inputStream = new BufferedReader(new InputStreamReader(process.getInputStream()));
  }

  String[] commandBootAnim = new String[] { adbPath, "shell", "getprop", "init.svc.bootanim" };
  process = new ProcessBuilder(commandBootAnim).start();
  inputStream = new BufferedReader(new InputStreamReader(process.getInputStream()));

  // wait till the property returns 'stopped'
  while (!inputStream.readLine().equals("stopped")) {
   process.waitFor(1, TimeUnit.SECONDS);
   process = new ProcessBuilder(commandBootAnim).start();
   inputStream = new BufferedReader(new InputStreamReader(process.getInputStream()));
  }

  System.out.println("Emulator is ready to use!");
 } catch (Exception e) {
  e.printStackTrace();
 }
}
You can ensure if an emulator or device is already running before using the above method or commands by the post Checking if android emulator or device is running

1 comment:

  1. Thanks for sharing this Information, Got to learn new things from your Blog on Appium
    Ref link : http://thecreatingexperts.com/appium-training-in-chennai/

    ReplyDelete