Nov 4, 2017

Mobile App Automation with Appium on Android Emulator/Device

Overview :-


Appium is an open source test automation tool for mobile applications. It allows you to test all the three types of mobile applications: native, hybrid and mobile web.It also allows you to run the automated tests on actual devices, emulators and simulators.

If you want to start mobile automation on Android device/emulator, first you must need to setup the Android SDK on your machine and set the paths in environment variables.Then you can create your own emulator and run the test scripts on it.

Pre-requisites :-


1. JDK installation

2. Android SDK installation and Emulator Creation

3. Appium installation

4. Native App information
  • .apk file - This is not required if you specify appPackage and appActivity capabilities
  • appPackage - Activity name for the Android activity you want to launch from your package. This often needs to be preceded by a . (e.g., .MainActivity instead of MainActivity)
  • appActivity  - Java package of the Android app you want to run


Prerequisted Jars:

  • java-client.jar - For Appium
  • selenium-server-standalone.jar - For webdriver capabilities
  • commons-validator.jar - if want to run appium server programatically


Example : 


Desired capabilities are a set of keys and values (i.e., a map or hash) sent to the Appium server to tell the server what kind of automation session we’re interested in starting up. There are also various capabilities which can modify the behavior of the server during automation.

For example, we set the platformName capability to Android to tell Appium that we want a Android session, rather than an iOS or Windows one.

For Native app tests, we pass either .apk file path or "appPakage" and "appActivity" information to appium server.

Before execute this program make sure appium server is running on your machine.

public class WebTest {

static AppiumDriver driver;

public static void main(String[] args) throws InterruptedException {

    DesiredCapabilities capabilities = new DesiredCapabilities();
    capabilities.setCapability("deviceName", "android emulator");
    capabilities.setCapability(CapabilityType.VERSION, "8.0");
    capabilities.setCapability("platformName", "android");
    capabilities.setCapability("appPackage", "com.android.calculator2");
    capabilities.setCapability("appActivity", "com.android.calculator2.Calculator");

try {

driver = new AndroidDriver(new URL("http://0.0.0.0:4723/wd/hub"), capabilities);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

driver.findElements(By.xpath("//android.widget.Button")).get(0).click(); driver.findElement(By.name("2")).click();
driver.findElement(By.name("+")).click();
driver.findElement(By.name("5")).click();
driver.findElement(By.name("=")).click();
String result = driver.findElement(By.className("android.widget.EditText")).getText(); System.out.println("Number sum result is : " + result);
System.out.println("done");
}
}

No comments:

Post a Comment

Mobile App Automation with Appium on Android Emulator/Device

Overview :- Appium  is an open source test automation tool for mobile applications. It allows you to test all the three types of mobile...