Search

Wednesday, October 14, 2015

Appium - Run First Android Automation Test Using In Eclipse

We have configured appium project in eclipse during previous post. Now we are all set to create and run first appium automation test script on android mobile device. In this steps we will learn how to create first appium automation test script in eclipse and then run it in real android mobile device.


PREREQUISITE : All previous 12 STEPS should be completed without any error.

We will use android mobile phone's default installed calculator app to run appium automation test script. You can download Android Calculator App from THIS PAGE if it is not installed in your mobile. This is our first appium test so we will create simple test to sum two numbers. Follow the steps given bellow.

1. Gather Required Capabilities
Before creating appium test for android app, You need bellow given parameters of android device and app to set webdriver capabilities in test script.
  1. Find Android Device Name : As described in THIS POST, Connect your android device with PC and get device name by running adb devices command in command prompt. My android device name Is : ZX1B32FFXF. Find your device name.
  2. Find App Package Name : You can view THIS POST to know how to get android app package name. Package name for my calculator app (Which Is Installed in my android device) is com.android.calculator2. Find your app package name.
  3. Find App Activity Name : You can view THIS POST to know how to get android app activity name. Activity name for my calculator app (Which Is Installed in my android device) is com.android.calculator2.Calculator. Find your app activity name.
  4. Find Android OS Version : In your android device, Open settings -> About phone -> Android version. My device's Android version Is : 4.4.2.
2. Launch And Start Appium Node Server
Appium should be installed and configured and also you need Server Address and Port number (which is used by appium) as described in THIS POST.

Launch Appium
  • Launch Appium from Windows Start menu.


  • For me, Server Address is : 127.0.0.1 and Port Number is : 4723.
Start Appium Node Server
Click on Start button to start appium node server as shown in bellow image. It will take some time to launch node server.

Note : You can consider appium node server is started properly once it shows log as shown in above image.

3. Create Appium Test Script In Eclipse
I have created sample appium test script using selenium webdriver to sum two numbers using android calculator application.

Prerequisite :
  • Calculator app should be installed in your android device.
  • Install TestNG in eclipse if it is not installed. View THIS POST.
  • Also replace capabilities values of bellow given list with your own values in script. Otherwise it will not works.
  1. deviceName - Name of device which is connected with PC.
  2. CapabilityType.VERSION - OS version of your android device.
  3. appPackage - Calculator app's Package name.
  4. appActivity - Calculator app's Activity name.
Now create bellow given test class under Android package of your appium project. Usage of each code syntax is given in script itself.

SimpleAndroidCalcTest.java
package Android;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
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.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class SimpleAndroidCalcTest {

WebDriver driver;

@BeforeTest
public void setUp() throws MalformedURLException {
// Created object of DesiredCapabilities class.
DesiredCapabilities capabilities = new DesiredCapabilities();

// Set android deviceName desired capability. Set your device name.
capabilities.setCapability("deviceName", "ZX1B32FFXF");

// Set BROWSER_NAME desired capability. It's Android in our case here.
capabilities.setCapability(CapabilityType.BROWSER_NAME, "Android");

// Set android VERSION desired capability. Set your mobile device's OS version.
capabilities.setCapability(CapabilityType.VERSION, "4.4.2");

// Set android platformName desired capability. It's Android in our case here.
capabilities.setCapability("platformName", "Android");

// Set android appPackage desired capability. It is
// com.android.calculator2 for calculator application.
// Set your application's appPackage if you are using any other app.

capabilities.setCapability("appPackage", "com.android.calculator2");

// Set android appActivity desired capability. It is
// com.android.calculator2.Calculator for calculator application.
// Set your application's appPackage if you are using any other app.

capabilities.setCapability("appActivity", "com.android.calculator2.Calculator");

// Created object of RemoteWebDriver will all set capabilities.
// Set appium server address and port number in URL string.
// It will launch calculator app in android device.

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

@Test
public void Sum() {
// Click on DELETE/CLR button to clear result text box before running test.
driver.findElements(By.xpath("//android.widget.Button")).get(0).click();

// Click on number 2 button.
driver.findElement(By.name("2")).click();

// Click on + button.
driver.findElement(By.name("+")).click();

// Click on number 5 button.
driver.findElement(By.name("5")).click();

// Click on = button.
driver.findElement(By.name("=")).click();

// Get result from result text box.
String result = driver.findElement(By.className("android.widget.EditText")).getText();
System.out.println("Number sum result is : " + result);

}

@AfterTest
public void End() {
driver.quit();
}
}

Note : Before running above script using testng, Please make sure your android device is connected with PC with USB debugging mode enabled and Appiun node server is launched and started.

4. Running  Appium Test Script 
I hope you already know how to run test script using testng (VIEW EXAMPLE). Run above test script and view your android phone screen. It will 
  • Open calculator app in your mobile device.
  • Tap on buttons in this sequence -> CLR, 2, +, 5 and =.
  • Get result from text area of calculator app.
  • Print result in eclipse console.
This way, We have executed very simple test script in android mobile device. If you have noticed in above example test script, We have used only name locator. We will use all different element locators (Which are described in THIS POST) in upcoming examples.

No comments:

Post a Comment