Monday 5 August 2013

Appium android example program for windows using java - native app

To perform the automation using appium for the native android app just .apk file is enough. No need of developers code at all.

When we write the appium script for automation we need to specify the package name and activity name. We can get package and all activity names from the .apk file itself. For this we need to convert .apk file to java class files. See here to perform that.


I used following environments to accomplish this example program,


  • Windows7
  • Android SDK
  • JDK
  • Eclipse
  • TestNG

Install and start the appium server:


Appium server for windows platform is just a zip folder. It contains all the packages needed for appium such as node, webdriver etc. We no need to install any APIs additionally

  • Download the latest AppiumForWindows.zip from here
  • Unzip the AppiumForWindows.zip
  • Click on the appium.exe present in the unzipped appium folder. It will be opened up as below,


Appium Server For Windows

  • Now click on the 'Launch' button, It will start the server at 127.0.0.1:4723 as below,

Appium server is started

  • Then run the below program once the android emulator is ready.

Appium example program for android


package com.qa.test;

import java.io.File;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class androidExample {
WebDriver driver = null;
         
        @BeforeMethod
         public void setup() {
                File appDir = new File("E://AndroidApps");
File app = new File(appDir, "myApp.apk");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("device","Android");
capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
capabilities.setCapability(CapabilityType.VERSION, "4.2.2");
capabilities.setCapability(CapabilityType.PLATFORM, "WINDOWS");
                // Here we mention the app's package name, to find the package name we  have to convert .apk file into java class files
capabilities.setCapability("app-package","com.myApp.activity");
                //Here we mention the activity name, which is invoked initially as app's first page.
capabilities.setCapability("app-activity","LoginActivity");
//capabilities.setCapability("app-wait-activity","LoginActivity,NewAccountActivity");
capabilities.setCapability("app", app.getAbsolutePath());

driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
driver.manage().timeouts().implicitlyWait(80, TimeUnit.SECONDS);

         }

       @Test
public void loginTest() throws Exception {
driver.findElement(By.xpath("//EditText[@text='Email Address']")).sendKeys("tester@gmail.com");
driver.findElement(By.xpath("//LinearLayout/EditText[2]")).sendKeys("Testerpwd");
driver.findElement(By.xpath("//CheckBox")).click();
driver.findElement(By.xpath("//Button[@text='Login']")).click();

WebDriverWait wait = new WebDriverWait(driver,80);
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//Button[@text='Logout']")));
driver.findElement(By.xpath("//Button[@text='Logout']")).click();

        }

       @AfterMethod
        public void tearDown() {
               driver.quit();
        }

}

Running this program will install the app in the emulator if it is not present already. If the app is installed already it will open and perform the automation steps.

If your app is already opened with some other activity(e.g. my profile page) appium will be waiting for the specified activity (com.myApp.activity.LoginActivity) when you run the automation script. In this case automation script will fail, since the app is opened with some other activity.

So it is better to start the appium server with an option full reset. Perform the following steps in appium GUI server,
  • Check the option 'Perform Full Reset'
  • Go to File > Preferences and check the option 'Reset Application State After Each Session'
When you use these options in the GUI server, you might also need to specify the app path, package and Activity name as below,

Start server with full reset

This will uninstall the app once your tests are completed and will install the app into emulator when the test starts initially.

Note:
  • Here I used the uiautomatorviewer to find the element hierarchy and then I constructed the xpath manually. To learn about constructing the xpath, please go here
  • You should set ANDROID_HOME and path for Android SDK in environment variable
  • Emulator adb device should have API level greater than or equal to 17