For executing android tests in android emulator(AVD), we need to start it and ensure its running before our test starts executing. This post will explain you how can we start android emulator from code and from command line(mainly these will be helpful when we configure our test execution jobs in CI tools like Jenkins)
Manually launching emulator:
This can be easily opened from the IDE(eclipse if you are using adt bundle)
Open eclipse > select menu "Windows" > Select "Android Virtual Device Manager" > select the AVD you want to launch(create an AVD if not yet created) > Click on "Start..." button > Click "Launch" button to launch the emulator > The emulator will launch in a moment
Launching emulator from command line:
The following command will launch the android emulator. Please ensure you have SDK installed and path has set.
Launching emulator programmatically:
You can achieve this in code too. You can execute this script using ProcessBuilder class in java as below.
Manually launching emulator:
This can be easily opened from the IDE(eclipse if you are using adt bundle)
Open eclipse > select menu "Windows" > Select "Android Virtual Device Manager" > select the AVD you want to launch(create an AVD if not yet created) > Click on "Start..." button > Click "Launch" button to launch the emulator > The emulator will launch in a moment
Launching emulator from command line:
The following command will launch the android emulator. Please ensure you have SDK installed and path has set.
emulator -avd <avd_name>
For example,
emulator -avd AVD_for_Nexus_4_by_GoogleYou can start with many options, for more information please refer here.
Launching emulator programmatically:
You can achieve this in code too. You can execute this script using ProcessBuilder class in java as below.
private static String sdkPath = "/Applications/adt-bundle-mac-x86_64-20140702/sdk/";// or for windows D:/Android/adt-bundle-windows-x86_64-20140702/sdk/ private static String adbPath = sdkPath + "platform-tools" + File.separator + "adb"; private static String emulatorPath = sdkPath + "tools" + File.separator + "emulator";
Please make sure to change the value of "sdkPath" variable to your SDK installation directory.
The following code will start an emulator with the provided AVD name.
/** * Starts an emulator for the provided AVD name * * @param nameOfAVD */ public static void launchEmulator(String nameOfAVD) { System.out.println("Starting emulator for '" + nameOfAVD + "' ..."); String[] aCommand = new String[] { emulatorPath, "-avd", nameOfAVD }; try { Process process = new ProcessBuilder(aCommand).start(); process.waitFor(180, TimeUnit.SECONDS); System.out.println("Emulator launched successfully!"); } catch (Exception e) { e.printStackTrace(); } }
The 3 minute(180 sec) wait in the above code is to wait for the emulator which can be decreased or increased depending upon your system performance.
If you want to stop or kill your running emulator, please refer my post Stop or kill android emulator programmatically and from command line
If you want to stop or kill your running emulator, please refer my post Stop or kill android emulator programmatically and from command line
Hi Sir, I am facing some issues while running this script, the error as below
ReplyDeleteFAILED CONFIGURATION: @BeforeTest launchEmulator
org.testng.TestNGException:
Can inject only one of into a BeforeTest annotated launchEmulator.
For more information on native dependency injection please refer to http://testng.org/doc/documentation-main.html#native-dependency-injection
at org.testng.internal.Parameters.checkParameterTypes(Parameters.java:244)
at org.testng.internal.Parameters.createParameters(Parameters.java:172)
at org.testng.internal.Parameters.createParameters(Parameters.java:458)
So please give me the solution for this
thanks A lot
ReplyDeleteThanks A lot, How do I close the emulator after test ?
ReplyDeleteYou can refer the post http://aksahu.blogspot.in/2016/01/stop-or-kill-android-emulator.html for closing the emulator after your test
Delete