In the above diagram we can see, here we have Bootstrap.jar in place of bootstrap.js which represents out test case when compiled in java. As soon as it gets launched it spawns a TCP server. Here the TCP server resides inside the device and client is in the Appium process which is just opposite to the way it is in iOS.
Set up Appium with Eclipse in detail: Run The First Test
Now you are all set to write your first test script. Create a small test Program for opening a Amazon application on your device using Appium (Here, I use emulator on ubuntu 14.04).
1) Copy & paste the below code to your StartApplication class and give it a run.
package amazon;
import io.appium.java_client.android.AndroidDriver;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
public class StartApplication {
private static AndroidDriver driver;
public static void main(String[] args) throws MalformedURLException, InterruptedException {
File classpathRoot = new File(System.getProperty("user.dir"));
File appDir = new File(classpathRoot, "/Apps/Amazon/");
//System.out.println(classpathRoot.getAbsolutePath() + ":");
//System.out.println(appDir.getAbsolutePath());
File app = new File(appDir, "in.amazon.mShop.android.shopping.apk");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
capabilities.setCapability("deviceName", "Android");
capabilities.setCapability("platformVersion", "4.4.2");
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("app", app.getAbsolutePath());
capabilities.setCapability("appPackage", "in.amazon.mShop.android.shopping");
capabilities.setCapability("appActivity", "com.amazon.mShop.home.HomeActivity");
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
driver.manage().timeouts().implicitlyWait(80, TimeUnit.SECONDS);
Thread.sleep(10000);
driver.quit();
System.out.println("DONE!");
}
}
2) Do no get in to details for now and simply run the test. To start the test just select Run > Run As > Java Application Or Right Click on Eclipse code and Click Run As > Java Application.
3) After a few Seconds, you will see that with the help of your script, Amazon application will belaunched on your device. Once the execution is finished, you will see a long list of messages on theAppium console.We will come back to these messages later.
Code Explanation
Lets now discuss what each line of code means in the above test. I hope you are enjoying your journey of learning Appium.
Import libraries statements
// Llibrary for Appium drivers
import io.appium.java_client.android.AndroidDriver;
// Library to create the path to APK
import java.io.File;
// Library used to verify if URL is malformed
import java.net.MalformedURLException;
// Library used to create URL for the Appium server
import java.net.URL;
// Libraries for configuring Desired Capabilities
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
Path to APK file
Since the Amazon app apk is stored in the computer and is not already installed on the device we need to create a file object which represents the actual apk file on the disk. I placed the folder ‘/Apps/Amazon/‘ which contains the apk file inside the Eclipse project.
// Path to Eclipse project
File classpathRoot = new File(System.getProperty(“user.dir”));
// Path to <project folder>/Apps -> Amazon
File appDir = new File(classpathRoot, “/Apps/Amazon/”);
// Path to <project folder>/Apps -> Amazon/Amozon apk file
File app = new File(appDir, “in.amazon.mShop.android.shopping.apk”);
Desired Capabilities
To be able to test the app on an actual device Desired Capabilities need to be set. Desired Capabilities are a set of keys and values sent to the Appium server to tell the server what kind of automation session we’re interested in starting up. There are also capabilities used to modify the behavior of the server during automation.
// To create an object of Desired Capabilities
DesiredCapabilities capabilities = new DesiredCapabilities();
// Name of mobile web browser to automate. It should be an empty string, as we are automation an app
capabilities.setCapability(CapabilityType.BROWSER_NAME, “”);
// Name of the OS: Android, iOS or FirefoxOS
capabilities.setCapability(“platformName”, “Android”);
// Mobile OS version – My device is running Android 4.4.2
capabilities.setCapability(CapabilityType.VERSION, “4.4.2”);
// Device name: – I am using Micromax A311
capabilities.setCapability(“deviceName”, “Micromax A311”);
// An absolute local path to the APK file
capabilities.setCapability(“app”, app.getAbsolutePath());
// Java package of the tested Android app
capabilities.setCapability(“appPackage”, “in.amazon.mShop.android.shopping”);
// An activity name for the Android activity you want to run from your package.
capabilities.setCapability(“appActivity”, “com.amazon.mShop.home.HomeActivity”);
// Constructor to initialize driver object with new Url and Capabilities
driver = new AndroidDriver(new URL(“http://127.0.0.1:4723/wd/hub”), capabilities);