Appium

Monday 19 August 2013

Native Android Apps Automation with WebDriver Using Appium

I am writing this post to share my personal experience with Appium to automate Native Android Apps on Windows platform.

About WebDriver - WebDriver is a web automation framework that allows to execute test scripts on desktop and mobile browsers.
If you are considering to test mobile apps, then the correct solution is Appium.

About Appium - Appium is an open source automation tool which can automate web, native and hybrid mobile apps on iOS, Android and FirefoxOS platforms.
It uses the WebDriver JSON wire protocol to drives the Android UI Automator framework.

Appium's Benefits- 
1. It can automate web, hybrid and native mobile apps. 
2. It allows us to write test cases in any programming language (like java, c#, ruby etc) using Selenium WebDriver. 
3. The other benefits of Appium is that it is cross- platform test automation tool for mobile apps which means the same test cases would work on multiple platforms.

Getting started with Appium drives on Real Android Device--

Pre-requisites--
1. Appium for Windows (download)
2. Android SDK (download)
3. JDK (download)
4. Eclipse (download)
5. TestNG (download)
6. Appium client library (download)
7. Selenium Server jar (download)
8. WebDriver client library (download)
9. Apk info- Users need to install the same app. in android mobile to get the package and activity info of the apps.(download)
Apk Info

Step 1- Setting up the Android SDK on Windows

a. Open System properties window by pressing window-key +Pause or right click on My Computer and click properties.

b. Click on 'Advanced system settings'
c. Click on 'Environment Variables'
d. Set ANDROID_HOME as shown in above figure.
e. In the 'System variables' find 'Path' and then double click on it.

f. In the 'variable value' text box, add the new path 'C:\Users\dell\android-sdks\platform-tools'.Just be sure that there is a semi-colon separating this new entry from the preexisting values.

g. Now click ok button of all boxes.

Now start android server using "adb start-server" from command prompt.


Step 2- Connect your Android device with the PC and make sure device Developer options is 'ON' and also USB debugging mode is checked as shown in below picture.



Developer options
Now hit "adb devices" from command prompt to make sure device connected successfully.

Step 3- Upzip downloaded AppiumForWindows.zip, the folder contains Appium.exe, run it by double clicking, it will look like-

 Appium v1.4.16

No need to change the IP address and port number.
Now, click on Launch button then Appium Server will start at 127.0.0.1:4723 as shown in below picture.
Appium Server Started

Step 4- Here i am taking a example of Android native app Calculator.
Calculator
Step 5- Writing test scripts for Calculator using java, WebDriver and TestNG unit framework.
package com;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;

import io.appium.java_client.remote.AndroidMobileCapabilityType;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.*;

public class CalculatorTest {

AppiumDriver driver;

@BeforeClass
public void setUp() throws MalformedURLException{

     //Set up desired capabilities and pass the Android app-activity and app-package to Appium

   DesiredCapabilities capabilities = new DesiredCapabilities();
   capabilities.setCapability(MobileCapabilityType.VERSION, "4.4.4");
   capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
   capabilities.setCapability(MobileCapabilityType.DEVICE_NAME,"TA64301YDK");
   capabilities.setCapability(AndroidMobileCapabilityType.APP_PACKAGE, "com.android.calculator2"); // This is package name of your app (you can get it from apk info app
   capabilities.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY, "com.android.calculator2.Calculator"); // This is Launcher activity of your app (you can get it from apk info app)
   //Create AndroidDriver instance and connect to the Appium server.
   //It will launch the Calculator App in Android Device using the configurations specified in Desired Capabilities

   driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
}

@Test
public void testCal(){

    //locate the Text on the calculator by using By.name()
    WebElement two=driver.findElement(By.name("2"));
    two.click();
    WebElement plus=driver.findElement(By.name("+"));
    plus.click();
    WebElement four=driver.findElement(By.name("4"));
    four.click();
    WebElement equalTo=driver.findElement(By.name("="));
    equalTo.click();
    //locate the edit box of the calculator by using By.className()
    WebElement results=driver.findElement(By.className("android.widget.EditText"));
    //Check the calculated value on the edit box
    assert results.getText().equals("6"):"Actual value is : "+results.getText()+" did not match with expected value: 6";
}
@AfterClass
public void teardown(){
  
    //close the app
    driver.closeApp();
}
}


Step 6 - Execute above test scripts using TestNG.

Step 7 - Done