Setting up Appium Android in Eclipse

Tags

, ,

Appium Server is really tricky to setup in Mac OS as there are multiple components to be integrated. I have given the steps to setup Appium Server for Android platform. Using the below steps, the reader can start the Appium Server programmatically, run the tests in Emulator. Feel free to contact me in case of any issues during setup. Constructive feedback is welcome to improve the post which helps users.

Android Appium
Steps to setup Appium Android. I have given the steps below for Mac OS & Windows. Hope Linux follows the same pattern as that of Mac.

Step 1 : Download & Install Eclipse

Mac:

http://www.eclipse.org/downloads/download.php?file=/technology/epp/downloads/release/luna/SR1/eclipse-java-luna-SR1-macosx-cocoa-x86_64.tar.gz

Windows:

https://www.eclipse.org/downloads/download.php?file=/technology/epp/downloads/release/luna/SR1/eclipse-java-luna-SR1-win32-x86_64.zip

Step 2 : Download & Install Appium

Mac & Windows users, choose the respective appium binary at

https://bitbucket.org/appium/appium.app/downloads/

Mac users move theĀ Appium.app to the Applications folder.

Step 3 : Need to install a Emulator of your choice. I prefer Genymotion.

Homepage

Step 4 : Setup Genymotion : In Genymotion or any emulator, Create a Android Virtual Device of your choice. I choose to create Samsung Galaxy S4. Once the virtual device is created, try installing your android app, by drag and drop over the Virtual Device’s home screen.

Step 5 : Install Android SDK. Android Studio is not required and just the Android SDK is sufficient. We can add the required packages later using Android SDK Manager. Download the SDK tools appropriate for your platform.

https://developer.android.com/sdk/index.html#Other

  • On Mac or Linux, open a terminal and navigate to the tools/ directory in the Android SDK, then execute android sdk.
  • Under Tools, Make sure you do have Android SDK Tools, Android SDK Platform-tools, Android SDK Build-tools installed.
  • Also, install the Android Version (5.0.1) in which your application works.

Step 6 : Environment variables if not created properly will create issues when the Appium server is started from the eclipse. So, please follow the below steps diligently.

In Mac, setting up environment variables in ~/.bash_profile, ~/.profile or using any export command in the Terminal window will be available only for Terminal application.

If the Appium server is to be started from Eclipse, ANDROID_HOME env variable should be setup using the below procedure.

  • ANDROID_HOME environment variable setup. Mac users please follow the below link.

http://www.dowdandassociates.com/blog/content/howto-set-an-environment-variable-in-mac-os-x-launchd-plist/

  • Create PATH variable using the same steps as above and add $ANDROID_HOME/platform-tools/, $ANDROID_HOME/tools/, $ANDROID_HOME/build-tools to your PATH variable.

Step 7 : Setting up Eclipse

  • Create a Maven project in Eclipse – File > New > Project > Maven > Maven Project
  • Add the below dependencies in your pom.xml


<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.44.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-server</artifactId>
<version>2.44.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8.13</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.3.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>2.1.0</version>
<scope>compile</scope>
</dependency>
</dependencies>

  • Appium Server programmatically run from Eclipse using the below code in the Project.



import io.appium.java_client.android.AndroidDriver;

import java.io.IOException;
import java.net.URL;
import java.util.concurrent.TimeUnit;

import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecuteResultHandler;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.ExecuteException;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class SetupAndroidAppium{

public static AndroidDriver driver;

static DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
static DefaultExecutor executor = new DefaultExecutor();
@BeforeMethod

public static void setUp() throws Exception {

System.out.println(“****************setUP Starts****************”);

System.out.println(“ANDROID_HOME : “+System.getenv(“ANDROID_HOME”));
System.out.println(“PATH : “+System.getenv(“PATH”));

resultHandler = new DefaultExecuteResultHandler();
executor = new DefaultExecutor();
executor.setExitValue(1);
// executor.execute(killNode,resultHandler);
// executor.execute(killPlayerEmulator,resultHandler);
// executor.execute(killADB,resultHandler);

// CommandLine command = new CommandLine(“/bin/sh -c”);
// command.addArgument(“/Applications/Appium.app/Contents/Resources/node/bin/node”,false);

CommandLine command = new CommandLine(“/Applications/Appium.app/Contents/Resources/node/bin/node”);

command.addArgument(“/Applications/Appium.app/Contents/Resources/node_modules/appium/bin/appium.js”, false);
command.addArgument(“–address”, false);
command.addArgument(“127.0.0.1”);
command.addArgument(“–port”, false);
command.addArgument(“6723”);
command.addArgument(“–bootstrap-port”, false);
command.addArgument(“6724”);
// command.addArgument(“–no-reset”, false);

executor.execute(command, resultHandler);

Thread.sleep(20000);

DesiredCapabilities capabilities = new DesiredCapabilities();

capabilities.setCapability(“platformName”, “Android”);
// capabilities.setCapability(“platformVersion”, “4.4.2”);
// capabilities.setCapability(“browserName”, “android”);

/* appPackage & appActivity is not mandatory as the Appium will inspect the .apk file for the default package and the activity
* Issues if any, please set it explicitly.
* */
// capabilities.setCapability(“appPackage”, “com.migmeplayground”);
// capabilities.setCapability(“appActivity”, “com.projectgoth.activity.MainActivity”);
//
capabilities.setCapability(“app”, “<PATH/TO/YOUR/ANDROID/APK>.apk”);
// capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, “device”);

// The device name could be found by running the command adb in the terminal window.
/*
*
* android-sdk-macosx$ adb devices
List of devices attached
192.168.56.101:5555 device
*
* */
capabilities.setCapability(“deviceName”, “device”);

driver = new AndroidDriver(new URL(“http://127.0.0.1:6723/wd/hub&#8221;),capabilities);

System.out.println(“****”);

driver.manage().timeouts().implicitlyWait(80, TimeUnit.SECONDS);

System.out.println(“****************setUp Ends****************”);
}

public static void main(String args[]){

try{

System.out.println(“****************main Starts****************”);
killNodeAdbPlayer();
launchEmulator();
Thread.sleep(20000);
setUp();
test01();
tearDown();
System.out.println(“****************main Ends****************”);
}
catch(Exception e){

System.out.println(e);
}

}

@AfterMethod
public static void tearDown() throws Exception {
System.out.println(“****************tearDown Starts****************”);

driver.quit();
killNodeAdbPlayer();
System.out.println(“****************tearDown Ends****************”);
}

@Test
public static void test01() throws InterruptedException {

System.out.println(“****************test01 Starts****************”);

driver.findElement(By.xpath(“YOUR TESTS HERE”));
driver.findElement(By.xpath(“YOUR TESTS HERE”));

System.out.println(“****************test01 Ends****************”);

}

/*
* Method to launch the emulator programmatically
* */

public static void launchEmulator() throws ExecuteException, IOException{

System.out.println(“****************launchEmulator Starts****************”);

CommandLine launchEmul = new CommandLine(“/Applications/Genymotion.app/Contents/MacOS/player”);

// command.addArgument(“–address”, false);
// command.addArgument(“127.0.0.1”);
launchEmul.addArgument(“–vm-name”, false);
launchEmul.addArgument(“SamsungGalaxyS4”);

executor.setExitValue(1);

executor.execute(launchEmul, resultHandler);
System.out.println(“****************launchEmulator Ends****************”);
}

private static void killNodeAdbPlayer() throws ExecuteException, IOException, Exception{
System.out.println(“****************kill Node Adb Player Starts****************”);

CommandLine killNode = new CommandLine(“kill -9 $(lsof -i | grep 6723 | awk ‘{print $2}’)”);
CommandLine killPlayer = new CommandLine(“kill -9 $(lsof -i | grep 6724 | awk ‘{print $2}’)”);

executor.setExitValue(1);
executor.execute(killNode,resultHandler);
executor.execute(killPlayer,resultHandler);

System.out.println(“****************kill Node Adb Player Ends****************”);
}

}