Appium is an open source mobile application user interface (UI) testing framework, and is 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-re-usability. Appium provides flexibility of writing and running automation tests in languages such as, Java, C#, PHP, Ruby, Python, Perl, Objective-C.
Appium Architecture In Android:
Appium is a webserver that exposes a REST API. Appium client establishes connection with Appium Server through JSON Wire Protocol by creating a JSON object called as `Desired Capabilities’. Appium Server then creates an automation session by running a session id for the client, based on Key-Value pairs of Desired Capabilities set in Appium client. Appium Server then connects with the UI-Automator provided by Android SDK. UI-Automator communicates with Bootstrap.jar which will run user commands from the test script, on the mobile app in device.
Prerequisites for running Appium automation test for a native Android app:
- 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. Path for ANDROID_HOME, platform-tools is set on the windows system.
- Eclipse IDE is installed. TestNG is installed.
- Eclipse ADT Plugin is installed and configured to integrate android development environment in eclipse IDE.
- Microsoft .Net Framework and Node JS is Installed.
- Appium is installed.
- Latest Selenium Webdriver jar files for java client, Appium Client library jar file, Google-gson jar file are downloaded.
- Android Device is connected to your laptop/computer In USB Debugging mode.
In eclipse create a Java project, with package name Android and class name AndroidCalculator as shown below.
Add selenium webdriver’s jar files, Appium Client library jar file, Google-gson jar file in to java build path, by right clicking on project > Properties > Java Build Path > Libraries > Add External JARs >
Set Desired Capabilities:
We need to provide the following set of Desired Capabilities to provide Appium Server with details of automation session we want to set up:
- Android Device Name – In command prompt, run command adb devices to show Android Device Name.
- Android OS Version – In android device, Open settings -> About phone -> Android version
- Android Software App Package Name, App Activity Name – In command prompt, run command adb logcat, and open Calculator App in Android phone and press ctrl+c. Log show’s App Package Name = com.android.calculator2, App Activity Name = com.android.calculator2.Calculator.
In class AndroidCalculator, following code snippet, creates a object of DesiredCapabilities class.
DesiredCapabilities capabilities = new DesiredCapabilities();
//Set android deviceName desired capability.
//Set BROWSER_NAME desired capability to Android.
//Set android VERSION from device desired capability.
// Set android platformName desired capability to Android.
// Set android appPackage desired capability.
// Set android appActivity desired capability.
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);
Locate elements in Android App:
To locate elements in Android App, we use UI Automator Viewer tool provided by Android SDK.
- Run uiautomatorviewer.bat file from SDKtools folder.
- In Android device, open calculator app.
- In UI Automator Viewer tool, click on Device Screenshot image button.
- UI Automator Viewer now shows calculator app’s screenshot on left pane, and right pane show’s calculator app’s UI element’s hierarchy view with node structure that explain’s how elements are arranged. Ex: In calculator snapshot, button 4 is selected, and right pane shows different properties of button 4, which can be used in test script for locating button 4.
Code The snippet 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 make configuration changes as shown in the below snapshots and click on Run button.
…to be continued.
Read Other Appium related blogs