Appium code Android example - Java

The following Appium Java code example shows typical navigation on an Android device within the demo bank app, Advantage Bank.

You can copy this code and use it in your test, after modifying the MC_SERVER, MC_SERVER_USER, and MC_SERVER_PASSWORD variables.

The first section contains all of the required import statements.

package tests;

import java.net.URL;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;

import org.openqa.selenium.Proxy;

import org.openqa.selenium.Proxy.ProxyType;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.remote.CapabilityType;

import org.openqa.selenium.remote.DesiredCapabilities;

import org.openqa.selenium.support.ui.WebDriverWait;

import org.openqa.selenium.support.ui.ExpectedConditions;

import io.appium.java_client.android.AndroidDriver;

The next section contains the script's actions.

public class AMB_Android_Example {

 

public static final String MC_SERVER = "http://127.0.0.1:8080";

public static final String MC_SERVER_USER = "admin@default.com";

public static final String MC_SERVER_PASSWORD = "******";

 

public static void main(String[] args) {

 

AndroidDriver wd = null;

 

try {

// Set Capabilities instance

DesiredCapabilities capabilities = new DesiredCapabilities();

 

// Set device capabilities

capabilities.setCapability("platformName", "Android");

 

// Application capabilities

capabilities.setCapability("appPackage", "com.hpswdemo.advantageinc");

capabilities.setCapability("appActivity", "com.hpswdemo.advantageinc.activities.MainActivity");

 

// Set MC Server credentials (could be skipped if "Anonymous access" is enabled for Appium scripts in the Administration settings).

capabilities.setCapability("userName", MC_SERVER_USER);

capabilities.setCapability("password", MC_SERVER_PASSWORD);

 

// Create a session to the MC server

wd = new AndroidDriver(new URL(MC_SERVER + "/wd/hub"), capabilities);

System.out.println("MC session was successfully created [Android Device]");

 

// Create a wait object instance in order to verify expected conditions.

WebDriverWait waitController = new WebDriverWait(wd, 60);

 

// Create an implicitly wait instance to define the timeout for 'findElement' commands

wd.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);

 

// Log in

wd.findElement(By.id("loginButton")).click();

System.out.println("Login Completed!");

 

WebElement menu = wd.findElement(By.id("up"));

menu.click(); //Expand menu

Thread.sleep(1000);

menu.click(); //Collapse menu

Thread.sleep(1000);

menu.click(); //Expand menu again

 

System.out.println("Transfer Money - start transaction...");

waitController.until(ExpectedConditions.elementToBeClickable(By.xpath("//android.widget.TextView[@text='MONEY TRANSFER']")));

WebElement Transfer = wd.findElementByXPath("//android.widget.TextView[@text='MONEY TRANSFER']");

Transfer.click();

WebElement amount = wd.findElementByClassName("android.widget.EditText");

amount.sendKeys("5");

wd.navigate().back();

 

// Transfer money

waitController.until(ExpectedConditions.elementToBeClickable(By.id("transferButton")));

wd.findElementById("transferButton").click();

Thread.sleep(10000);

 

// Confirm failed transaction

waitController.until(ExpectedConditions.elementToBeClickable(By.id("button1")));

System.out.println("Transfer Money - completed.");

WebElement message = wd.findElementByClassName("android.widget.TextView");

System.out.println("Status: " + message.getAttribute("text"));

 

wd.findElementById("button1").click();

wd.findElementById("up").click(); //Expand menu

 

// Logout

waitController.until(ExpectedConditions.elementToBeClickable(By.xpath("//android.widget.TextView[@text='LOGOUT']")));

WebElement logout = wd.findElementByXPath("//android.widget.TextView[@text='LOGOUT']");

logout.click();

 

// Cancel LogOut

waitController.until(ExpectedConditions.elementToBeClickable(By.id("button2")));

wd.findElementById("button2").click();

 

 

System.out.println("Test completed.");

} catch (Exception e) {

System.out.println("Test failed.");

e.printStackTrace();

}

finally {

 

//Release lock in all cases

if (wd != null) {

System.out.println("MC session closed.");

wd.quit();

}

}

}

}

Back to top

See also: