About Appium
Appium is an Open Source mobile application UI testing framework, used to automate native, hybrid and web mobile apps. It enables cross-platform mobile app testing by using common API for both Android and iOS platform test scripts, thereby enhancing code-reusability. Appium provides flexibility of writing and running automation tests in any language such as, Java, C#, PHP, Ruby, Python, Perl, Objective-C.
About Android Emulator
Without an Android device, we can still run automation tests of Android Native App’s using an Emulator which mimics the OS and hardware of an Android device on a computer.
In Android SDK folder, there is an interface called as AVD Manager, which creates a virtual mobile device (Emulator) to run on a computer. Running Emulator starts a virtual console in which we can install Android apps.
In the following example, I will demonstrate automation of Android mobile phone’s default installed calculator app on Android Emulator.
Prerequisites for running Appium automation test for a native Android app on Emulator:
- Java/JDK is installed and path for JAVA_HOME is set on the windows system.
- Android SDK is installed and required packages of Android API’s are downloaded in SDK Manager.
- Intel HAXM is installed.
- Appium is installed.
Start Android Emulator:
In SDK folder on computer, run AVD Manager.exe, AVD Manager Dialog as shown below:
Click Create button, enter below details to create a new AVD.
AVD Manager shows newly created AVD as below, select this AVD and click Start.
In Launch Options dialog click Launch and in 10 to 15 minutes, Android Emulator will be started as below.
In eclipse create a Java project, with package name Android and class name AndroidEmulatorCalculator.
Set Desired Capabilities:
Following code snippet in AndroidEmulatorCalculator class, sets Desired Capabilities for Calculator App.
// Created object of DesiredCapabilities class. DesiredCapabilities capabilities = new DesiredCapabilities();
//Set android deviceName desired capability to Android Emulator.
capabilities.setCapability(“deviceName”, “Android Emulator”);
//Set BROWSER_NAME desired capability to Android.
capabilities.setCapability(“browserName”, “Android”);
//Set android VERSION from device desired capability.
capabilities.setCapability(“platformVersion”, “6.0”);
// Set android platformName desired capability to Android.
capabilities.setCapability(“platformName”, “Android”);
// Set android appPackage desired capability.
capabilities.setCapability(“appPackage”, “com.android.calculator2”);
// Set android appActivity desired capability.
capabilities.setCapability(“appActivity”, “com.android.calculator2.Calculator”);
Android Driver:
Following code snippet Creates an object of AndroidDriver class with Desired Capabilities as mentioned above and sets Appium server address with port number, and launches Android calculator app on device.
AndroidDriver driver; driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
The code snippet given below shows a simple addition on calculator app by using id tag to locate calculator app elements.
driver.findElement(By.id("com.android.calculator2:id/digit_4")).click(); driver.findElement(By.id("com.android.calculator2:id/op_add")).click(); driver.findElement(By.id("com.android.calculator2:id/digit_8")).click(); driver.findElement(By.id("com.android.calculator2:id/eq")).click(); String result = driver.findElement(By.className("android.widget.EditText")).getText(); System.out.println("Result of Addition is : " + result);
Run Automation Test
Start Appium server, and run AndroidEmulatorCalculator class as TestNG test. Automation test will open calculator app on Emulator, clicks on buttons in this sequence -> 4, +, 8 and =.
Gets result from text area of calculator app, and print it in eclipse console.
Learn more why trigent is one of the best automation testing companies.
Read Other Appium related blogs
How to Automate Testing for Android Native Apps Using Appium
Using Appium automation script to Check SQLite DB Content of a Device!
Automated Device Certification Testing On Cloud AWS Device Farm with Appium