Create Appium script for Android native App

After setting up Android and Appium on your system, next step is to  create an Appium script for Android native app. I hope that you have already downloaded and installed the required tools and APIs mentioned below. If not, then you can read our previous post in which we have explained the installation part.

Required Tools and APIs

  • Android SDK
  • JDK
  • Appium
  • Eclipse
  • Junit
  • Appium Client library
  • Selenium standalone Jar

Scenario

Here is the scenario that we are going to cover :
1.   Launch calculator app
2.   Add two numbers
3.  Print result and validate the result
4.  Close Application

Before creating a script, first we need to get the following information of Calculator App :

  • Main Activity class Name
  • Package name

We have to mentioned this information in our script, so that Appium could recognize that which activity it has to launch on device. We will get this information from “apk Info” App. You can download this app from Google play store on your android emulator. When you open this app, it will display all the installed apps of your emulator. Just tap on the calculator app to get the information of it. Refer screen shot below :

Apk-info

Program


import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;
import java.net.MalformedURLException;
import java.net.URL;
import junit.framework.Assert;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;


public class CalcTests {

     public AppiumDriver driver = null;

     @Before()
     public void setupstart() throws MalformedURLException
     {
            DesiredCapabilities capabilities = DesiredCapabilities.android();
            capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, "Appium");
            capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
            capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "192.168.56.101:5555");
            capabilities.setCapability(MobileCapabilityType.APP_PACKAGE, "com.android.calculator2");
            capabilities.setCapability(MobileCapabilityType.APP_ACTIVITY, "com.android.calculator2.Calculator");
            driver = new AndroidDriver(new URL("http://localhost:4723/wd/hub"), capabilities);
     }



        @Test
        public void calcTest1() throws Exception
        {
           driver.findElement(By.name("4")).click();
           driver.findElement(By.name("×")).click();
           driver.findElement(By.name("3")).click();
           driver.findElement(By.name("=")).click();
           System.out.println("Result : " + result);
           String result = driver.findElement(By.className("android.widget.EditText")).getText();
           Assert.assertEquals("Incorrect Result", "12", result);

        }


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

If you want to understand about the concept of desired capabilities written in the above program, Please refer our previous post.

If you really like the information provided above, please don’t forget to leave the comment. You can also like us on Facebook.

Leave a Reply

Your email address will not be published. Required fields are marked *