Appium

Setup and launch the first test on APPIUM on Android device using Java

Today we are going to setup and launch the first test on APPIUM on Android device. This is applicable only to Windows system and should have access to an Android Device. I was using JDK version 1.8.0 at the time I wrote this. The primary language that I will use here is Java.

1. Install Java Development Kit

Go to this link and install the JDK based on your Windows version.

http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

Follow the installation procedure and just keep the standard settings.

2. Set up Java environment variable in Windows.

In order to do this follow the following procedure:

  • Goto Control Panel >> System or Security >> SystemOr, you can Right Click My Computer and choose Properties. Or, in Windows 10, just type System in Search Bar and click on System.
  • Click on Advanced system settings.
  • Click on Advanced Tab at the top and Click Environment Variables.
  • In the Environment Variables, click on New on the System Variables section.
  • In the New System Variable Dialog Box enter the following and click OK:

Variable name: JAVA_HOME

Variable value: C:\Program Files\Java\jdk1.8.0_121

  • While in the Environment Variables, select Path in the System Variables section and click edit.
  • At Edit Environment Variables Dialog box, click on New and enter the following in the new row:

%JAVA_HOME%\bin

3. Verify Java version 

To check that everything is working, click on Start, type cmd and in the Run and Enter. Type “java -version” (without the apostrophe), to see the java version. You will get the following message:

java version “1.8.0_121”

Java(TM) SE Runtime Environment (build 1.8.0_121-b13)

Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)

The version may subject to change depending on installation.

4. Install Android Studio.

Goto https://developer.android.com/studio/index.html Download Android Studio. Follow the installation.

5. Install Android tools 

Run Android Studio. In the Welcome to Android Studio, click on Configure and Click on SDK Manager.

In the SDK Platforms tab, select the necessary Android version you need. You can check the version from the About Phone section in your Android Device. Normally I would suggest that you choose Android 7.1.1 upto 5.0.

In the SDK Tools tab, make sure you have the following installed, if not select those and click apply and follow the installation procedure:

    • Android SDK Build-Tools
    • Android SDK Platform-Tools
    • Android SDK Tools
    • Google USB Driver

Set up Android environment variables. Just like in step 2, go to Environment Variables through Systems and Advanced System Settings.

In the Environment Variables, click on New on the System Variables section.

In the New System Variable Dialog Box enter the following and click OK:

Variable name: ANDROID_HOME

Variable value: C:\Users\Neloy\AppData\Local\Android\Sdk

Here replace my name (Neloy) with the user that is being set up your Windows.

While in the Environment Variables, select Path in the System Variables section and click edit.

At Edit Environment Variables Dialog box, click on New and enter the following in the new row:

%ANDROID_HOME%\tools

Repeat again to enter the following in the new row:

%ANDROID_HOME%\platform-tools

Click OK.

7. Install OEM drivers to connect your Android phone to the pc.

Goto the following link:

https://developer.android.com/studio/run/oem-usb.html

Follow the steps over there. In order to get the drivers for your phone model, there are links to get the drivers for different brands. If you cant find the drivers of your brand, you can always Google search.

8. Check adb devices 

After installing the OEM drivers, verify that your phone is connected (make sure your phone is connected to PC) by going to Command Line (click on Start, type cmd and in the Run and Enter) and typing “adb devices” to see your device id.

9. Install Appium Desktop client

Goto http://appium.io/

And download Appium. Follow the installation procedure.

10.  Setup developer option on your Android device.

On your Android device, go to Settings, and then About Phone. Tap on the Build number 4 to 7 times until you get the Developer Options. Go to Developer Options and enable USB debugging and tap OK.

11. Set up your Appium project on Eclipse. At Eclipse, create a project called “AppiumTestProject

12. Right click on folder ‘src’ and select New > Package. Give your package name ‘amazon’ and click on Finish button.

13. Create a new class ‘StartApplication’ and check the option ‘public static void main’ and click Finish button.

14. Before we continuing further, we need necessary java libraries to make the test work. Convert the project into a Maven Project and by Right clicking “AppiumTestProject” and heading to Configure and clicking on Convert to Maven Project.

15. Edit the pom.xml file. The pom.xml file should have the following entries:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

 <modelVersion>4.0.0</modelVersion>

 <groupId>AppiumTestProject</groupId>

 <artifactId>AppiumTestProject</artifactId>

 <version>0.0.1-SNAPSHOT</version>

 <build>

  <sourceDirectory>src</sourceDirectory>

  <plugins>

   <plugin>

    <artifactId>maven-compiler-plugin</artifactId>

    <version>3.5.1</version>

    <configuration>

     <source>1.8</source>

     <target>1.8</target>

    </configuration>

   </plugin>

  </plugins>

 </build>

 <dependencies>

  <dependency>

   <groupId>com.google.code.gson</groupId>

   <artifactId>gson</artifactId>

   <version>2.8.0</version>

  </dependency>

  <dependency>

   <groupId>org.seleniumhq.selenium</groupId>

   <artifactId>selenium-java</artifactId>

   <version>2.53.1</version>

  </dependency>

  <dependency>

   <groupId>io.appium</groupId>

   <artifactId>java-client</artifactId>

   <version>4.1.2</version>

  </dependency>

 </dependencies>

</project>

16. Since we are testing Amazon app (India version), we are going to download the apk from the net and push into the phone while testing.

Goto the following link and download the apk:

http://www.apkmonk.com/app/in.amazon.mShop.android.shopping/

Rename the file to “in.amazon.mShop.android.shopping.apk”

17. In the AppiumTestProject, right click on it and create a new folder Apps. Create a folder “Amazon” inside Apps. Place the apk file inside the Amazon folder.

18. Launch Appium Desktop client and Start server with default setting.

19. Copy the following and paste it in your StartApplication.java class:

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/");

   File app = new File(appDir, "in.amazon.mShop.android.shopping.apk");

   DesiredCapabilities capabilities = new DesiredCapabilities();

   capabilities.setCapability(CapabilityType.BROWSER_NAME, "");

   capabilities.setCapability("deviceName", "OnePlus 3T");

   capabilities.setCapability("platformVersion", "7.1.1");

   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://0.0.0.0:4723/wd/hub"), capabilities);

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

   Thread.sleep(10000);

   driver.quit();

 }

}

In the DesiredCapabilities section, change the setting according to your Android device setting.

20. Run the StartApplication.java class and Run as Java application. You should see, Amazon India getting installed and running.

21In order to get the Elements from the app, run uiautomatorviewer.bat from C:\Users\Neloy\AppData\Local\Android\sdk\tools\bin (my name should be replaced).

22. Make sure the Amazon app is displayed (the phone screen should be left on at all times) and click on Device Screenshot, which is the second button from top left.

If on some case, it does not work, then exit the uiautomatorviewer, right click uiautomatorviewer.bat from the explorer and open with notepad.

23. Find the following code in the file:

call %java_exe% -Djava.ext.dirs=%javaextdirs% -Dcom.android.uiautomator.bindir= -jar %jarpath% %*

and change that code to how i changed it:

call “%java_exe%” “-Djava.ext.dirs=%javaextdirs%” “-Dcom.android.uiautomator.bindir=C:\Users\Neloy\AppData\Local\Android\sdk\tools” -jar %jarpath% %*

In your case, edit the file location after bindir= and include your android sdk tools path. Save it and run the bat again. While you are taking the screenshot make sure your phone stays on and displaying the app.

24. You are free to do further tests through Eclipse but the basic settings are done.

If you have any question feel free to contact me via email.

Leave a Reply

Your email address will not be published. Required fields are marked *