So here I am – back with a new post and full of energy and ready to share my knowledge in Appium – this time on a different OS and an environment platform- Android Automation in Windows!!

Before we begin learning anything new here , let’s retouch some concepts of Appium which makes it a popular cross-platform test automation tool for automating native, hybrid and mobile web apps and is widely used for testing on simulators, emulators and real devices.


 

Advantages  of  using  Appium :

  • It uses WebDriver API (i.e ‘Selenium WebDriver‘) which provides a client-server protocol (known as the JSON Wire Protocol) thus making it possible to write the test code in any language.
  • Appium is a server which is written in node.js and can be built and installed from source or directly from npm.
  • There are client libraries (in Java, Ruby, Python, PHP, JavaScript, and C#) which can be used instead of regular WebDriver client.
  • Last but not the least – it is Open-Source.

 

Appium  on  Windows  (Requirements) :

Following things will be required as we work on appium android automation on windows platform –

  1. Appium.exe
  2. Android SDK API >= 17
  3. Java JDK
  4. Apache Ant
  5. Eclipse
  6. Android Emulator or an Android Device
  7. Selenium Server (formerly the Selenium RC Server) version (download link)
  8. Appium Client Library jar file (according to your language choice) (download link)
  9. .apk‘ file of the app on which tests will be executed.

We would be using java-client here for client library as we discuss further.


 

 Pre – Conditions

[i]  Set  Environment  Variables –

Make sure you set up environment variables correctly before executing any script. All these variables should be set under ‘System variables’ not in ‘User variables’.

Depending on the location where you have jdk, android-sdk and ant installed in your machine, path should be specified . For example ,in my machine , path is specified as –

 

  • JAVA_HOME
    • C:\Program Files\Java\jdk1.8.0_25
  • ANDROID_HOME
    • D:\Android_Development\android_sdk
  • ANT_HOME
    • C:\apache-ant-1.9.4
  • PATH variable (would contain System32 , platform-tools , tools ,ant bin and jdk bin set) –
    • C:\Windows\System32;D:\Android_Development\android_sdk\platform-tools;%JAVA_HOME%\bin;D:\Android_Development\android_sdk\tools;%ANT_HOME%\bin;
[ii]  USB Debugging ‘ON’ –

Make sure the device which you are connecting to your PC to run tests on has Developer Options enabled and USB debugging option is checked there as shown below –

usb_debugging

 


 

Executing   Basic   Script

 

FIRST   STEP  :  Creating   a   new   project

[1] Create a new java project in Eclipse :

  • Go to ‘File’ -> ‘New’ -> ‘Java Project’

 

 

[2] Add project name and click ‘Next’ –

 

[3] Click ‘Finish’ –

 

[4] Expand your project. Right click on src folder -> ‘New’ -> ‘Class’ –

 

[5] Enter ‘Name’ for class.  Also mention ‘Package’ and then click ‘Finish’ –

 

[6] You will see something like –

 

 

SECOND   STEP  :  Adding   required   jar   files

[1] You will need to export required selenium jar files in your project.

  • Selenium Server (formerly the Selenium RC Server) version here.
  • Appium Client Library jar file (according to your language choice) from here.

[2] Import all these jar files in your project.

  • Right click on your project -> Properties.
  • Go to Libraries section -> Add External JARs (Note: I am using java client here)
  • Select both jar files which needs to be added -> click on ‘Open’ -> then tap ‘OK’.

 

 

THIRD   STEP  :  Download  Appium

Download Appium for Windows zip file here and extract the zip file contents. After Appium is downloaded , you will find an “Appium.exe” application inside extracted ‘Appium’ folder.

 

 

FOURTH   STEP  :  Understanding   Appium   Parameters

  1. Launch Appium.exe and you would see following buttons –
    1. Android button
    2. Settings button
    3. Developer Settings
    4. About
    5. Inspector (magnifying glass icon)
    6. Launch/Stop button
    7. Delete (trash icon)
  2. launch_appiumHover over gear icon and you would see “General Settings”. Tap on it. Some important points to note here –general_settings_appium
    1. You would see “Server Address” (IP address on which you want the Appium server to run) and “Port” (port on which the Appium server will listen for WebDriver commands)  as it shows “127.0.0.1” (localhost) and “4723” respectively here –
    2. “Check for Updates” is to check regular Appium updates.
    3. “Pre-Launch Application” is to prelaunch the application before any commands start executing.
    4. “Override Existing Session” can be checked to override any existing Appium sessions(if any).
  3. Now hover over android icon and you would see “Android Settings. Tap on it and you will see a window opens up with too many fields.
  4. Under “Application” section , some fields which we can consider here –
    1. “Application Path” – You should check this option and can tap on ‘Choose’ and give the path of your ‘.apk’ file downloaded which you want to test.
    2. “Package” – This is the package name of your test application (ex – com.example.testApp)
    3. “Launch Activity” – Activity name for the Android activity which needs to be launched from your package.
  5.  Under “Capabilities” section –
    1. “Platform Name” – mobile platform.
    2. “Automation Name” – automation tool (Android or Selendroid).
    3. “Platform Version” – mobile platform version.
    4. “Device Name” – name of the device you would be testing on.
  6.  Hover on the magnifying glass and you can all the elements accessible on the screen. Appium should be running for inspector to work. An example screenshot of “Settings” section of an android device can be viewed in Inspector as –inspector_window
  7. Through appium inspector – you can view name , value , label , xpath of different elements on the screen. Given below are some examples how to use elements from the screen using xpath, accessibility_id, class name, id etc –
//driver declaration
public AppiumDriver<MobileElement> driver;
public WebDriverWait wait ;      

//using xpath
driver.findElementByXPath("//window[1]/scrollview[1]/button[1]");

//using accessibility_id ('text' and 'label' locators in inspector are used as 'AccessibilityId')-
driver.findElementByAccessibilityId("").click();

//using class name ('type' locator is used as 'className')
driver.findElementsByClassName("")

//using id ('resource-id' locator is used as 'id')
driver.findElementsById("")

//sendkeys can be used for giving some input in any field
driver.findElementById("org.email.android:id/email_username").sendKeys("xyz@gmail.com");

//provide delay between two events:
1. explicit wait:
 * Thread.sleep(time in milliseconds);
 * wait.until(ExpectedConditions.presenceOfElementLocated(By.id()); where wait = new WebDriverWait(driver, 20);
2. implicit wait:
 * driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

 

Note – You can run your test scripts either in simulator or on real device. Refer about Appium parameters detailed information here.

 

 

FIFTH   STEP  :  Writing  Script

Now we will have a look at one sample script.

Following desired capabilities need to be taken care of while writing script for android automation (Desired capabilities are a set of keys to communicate with Appium server to tell the server what kind of automation session you are interested in) –

  • platformName (mobile platform)
  • platformVersion (mobile platform version)
  • deviceName (name of device on which you are running test)
  • app (path to your ‘.apk’ file)
  • appPackage (app package)
  • appActivity (main activity class)
package com.androidtest.appium;

import java.net.URL;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;

public class TestCases  {

  String userName = "org.android:id/user_username";
  String password = "org.android:id/user_password";
  String logIn = "org.android:id/user_sign_in";
  
  public AppiumDriver<MobileElement> driver;
  public WebDriverWait wait;
  
  @Before
  public void setUp() throws Exception {
      // set up appium for android instance
      DesiredCapabilities capabilities = new DesiredCapabilities();
      capabilities.setCapability("deviceName", "Nexus 5");
      capabilities.setCapability(MobileCapabilityType.APP_PACKAGE, "org.sampleapp.android");
      capabilities.setCapability(MobileCapabilityType.APP_ACTIVITY, "org.sampleapp.android.LaunchActivity");
      capabilities.setCapability(MobileCapabilityType.APP, System.getProperty("user.dir") + "/build/sampleapp.apk");
      driver = new AndroidDriver<MobileElement>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
      wait = new WebDriverWait(driver, 30);
    }

  @After
  public void tearDown() throws Exception {
    driver.quit();
  }

  @Test
  public void logIn() throws InterruptedException {
    // Login to the Application
    wait.until(ExpectedConditions.presenceOfElementLocated(By.id(userName)));
    driver.findElementById(userName).sendKeys("xyz@gmail.com");
    driver.findElementById(password).sendKeys("Hello123");
    driver.findElementById(logIn).click();	
  }
}

 

 

SIXTH   STEP  :  Run  Script

After writing the test script , follow these steps to run your test –

  1. Open Appium.exe.
  2. Go to ‘General Settings’ and check that ‘Server Address’ and ‘Port’ should be set as “127.0.0.1” and “4723” respectively.
  3. Go to ‘Android Settings’ add your ‘.apk’ file path(path where you downloaded the apk file in your machine) under “Application Path”. You will notice that it takes up ‘Package’ and ‘Launch Activity’ paths automatically when you enter the apk file path.
  4. Under ‘Capabilities’ section –
    1. Make sure ‘Platform Name’ and  ‘Automation Name’ are set to ‘Android’ and ‘Appium’ respectively considering you aren’t using older android versions. If yes , chose ‘Selendroid’.
    2. Select ‘PlatformVersion’ based on mobile platform version you are working on.
    3. Select checkbox next to Device Name and mention your device name there say “S3” if my Samsung S3 device name is S3.
  5. Now trigger Appium server by tapping on launch button.
  6. Now go to Eclipse and run your project as a ‘Java Application’ –
  7. run_java_fileOnce Appium server is triggered and your test app launches , you can tap on Appium Inspector and view all accessible elements on any active screen and write test cases accordingly and execute.

 

There you go running your first test script in Android platform using Appium in Windows 😀

So this was all about Android Mobile Automation using Appium tool on Windows platform. Stay tuned for more posts!! 🙂

Share if you like : Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

31 comments on “Android Automation using Appium in Windows

  • Hi Smriti ,
    Good to hear that you are back .
    Wanted to know that does Appium support phonegap or any such frameworks .
    Thanks,
    Priyanka

    • If you have this doubt whether Phone gap applications can be automated using Appium then I believe that since PhoneGap apps are bundled into a native wrapper around UIWebViews- we should be able to automate it same as hybrid apps.
      Can refer here to get an idea about hybrid apps automation.

      • Hi Smriti ,
        Thanks for the response .
        So far I have automated Android and iOS hybrid apps using Appium . Now I want to do a POC to automate application build using Phonegap Framework.

        Can you please help me in this . Where can I get a sample application build using phonegap application to start the automation.And can you please tell the steps to automate the same .

        Thanks
        Priyanka

        • Hey
          As PhoneGap apps are bundled into a native wrapper around UIWebViews- we should be able to automate it same as hybrid apps.You can refer here to get an idea about hybrid apps automation.

          Sample code can be found here.

  • Hi Smriti,

    Thanks for such a detailed report. Quite helpful.
    I had a query about the soft or virtual keyboard testing using appium. I have an app which requires testing of an keyboard application.It’s a qwerty keyboard derivative app. How can I use appium to access the elements of the this soft keyboard. Any ideas would help.

    Thanks much!

  • Hi i am new to appium i just wanted to know from where we come to know “about app-package and app-activity” while testing android emulators.

    • App package: would be the package name of the app which you are testing. Every app has a unique package name say “com.smriti.firsttest” for example. It is the Java package of the Android app to run.
      App Activity: in case of Android is the launcher activity (i.e activity name for the Android activity to launch from your package. It is the app’s main activity to be called which will also launch the app). This is often a splash activity or main activity.

  • Hello,
    I am using appium and when I am running my script in eclipse the following error is coming at the time of launching the app:
    Launching App
    Exception in thread “main” org.openqa.selenium.remote.UnreachableBrowserException: Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure.
    Build info: version: ‘2.45.0’, revision: ‘5017cb8’, time: ‘2015-02-26 23:59:50’
    System info: host: ‘DIKSHAMINOTRA’, ip: ‘192.168.1.139’, os.name: ‘Windows 8’, os.arch: ‘amd64’, os.version: ‘6.2’, java.version: ‘1.8.0_31’
    Driver info: driver.version: RemoteWebDriver
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:593)
    at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:240)
    at org.openqa.selenium.remote.RemoteWebDriver.(RemoteWebDriver.java:126)
    at org.openqa.selenium.remote.RemoteWebDriver.(RemoteWebDriver.java:153)
    at com.rockstand.rockstand.launchApp(rockstand.java:37)
    at com.rockstand.rockstand.main(rockstand.java:23)
    Caused by: org.apache.http.conn.HttpHostConnectException: Connect to 127.0.0.1:4723 [/127.0.0.1] failed: Connection refused: connect
    at org.apache.http.impl.conn.HttpClientConnectionOperator.connect(HttpClientConnectionOperator.java:142)
    at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:319)
    at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:363)
    at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:219)
    at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:195)
    at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:86)
    at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:108)
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:72)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:57)
    at org.openqa.selenium.remote.internal.ApacheHttpClient.fallBackExecute(ApacheHttpClient.java:126)
    at org.openqa.selenium.remote.internal.ApacheHttpClient.execute(ApacheHttpClient.java:72)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:133)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:572)
    … 5 more
    Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at org.apache.http.conn.socket.PlainConnectionSocketFactory.connectSocket(PlainConnectionSocketFactory.java:72)
    at org.apache.http.impl.conn.HttpClientConnectionOperator.connect(HttpClientConnectionOperator.java:125)
    … 18 more
    Could you please help me in this?
    Please reply as soon as possible..
    Thanks

  • Error occurred when running test script “Failed to start an Appium session, err was: Error: Activity used to start app doesn’t exist or cannot be launched! Make sure it exists and is a launchable activity”

    Can you please help me out of this error?

    Vijay

  • Error occurred while running test script in appium “Failed to start an Appium session, err was: Error: Activity used to start app doesn’t exist or cannot be launched! Make sure it exists and is a launchable activity”

    Can you please help me out of this error?

    Regards,
    Vijay

  • System.out.println(“Launching App”);
    DesiredCapabilities capabilities = new DesiredCapabilities();
    capabilities.setCapability(“platformName”, “Android”);
    capabilities.setCapability(“platformVersion”, “4.0.4”);
    capabilities.setCapability(“deviceName”, “S3”);
    capabilities.setCapability(“app”, “E:\\b.apk”);
    capabilities.setCapability(“app-package”, “com.example.intentexample”);
    capabilities.setCapability(“app-activity”, “.MainActivity”);
    driver = new RemoteWebDriver(new URL(“http://127.0.0.1:4723/wd/hub”),
    capabilities);
    Thread.sleep(20000); // delay of 20s
    System.out.println(“App Launched”);

    I am using above code > But when I run the app as java application its not running at all.
    It’s giving the error as below.

    Exception in thread “main” java.lang.NoClassDefFoundError: com/google/common/base/Function
    at com.mobileiron.enrolldevice.Enrollment.launchApp(Enrollment.java:38)
    at com.mobileiron.enrolldevice.Enrollment.main(Enrollment.java:22)
    Caused by: java.lang.ClassNotFoundException: com.google.common.base.Function
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    … 2 more

    Please help me out on this!!!

    Thanks in Advance!!

  • Hi Smriti,
    I am trying to automate my phonegap app. I used appium inspector to write script. Here is my script:
    wd.findElement(By.xpath(“//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.webkit.WebView[1]/android.webkit.WebView[1]/android.widget.EditText[1]”)).click();
    wd.findElement(By.xpath(“//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.webkit.WebView[1]/android.webkit.WebView[1]/android.widget.EditText[1]”)).sendKeys(“xyz@mail.com”);
    wd.findElement(By.xpath(“//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.webkit.WebView[1]/android.webkit.WebView[1]/android.widget.EditText[2]”)).click();
    wd.findElement(By.xpath(“//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.webkit.WebView[1]/android.webkit.WebView[1]/android.widget.EditText[2]”)).sendKeys(“test12”);
    wd.findElement(By.name(“LOGIN”)).click();

    I got below error when i execute my testApp:
    org.openqa.selenium.NoSuchElementException: An element could not be located on the page using the given search parameters. (WARNING: The server did not provide any stacktrace information)
    Command duration or timeout: 26.68 seconds
    For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html

    What might be a problem here? Because the script written by appium inspector only. I don’t know why it is not running. Do you have any idea about this issue?
    Thanks

  • @ Smriti, could you let me know what fix you given to make it work.

    i am facing same issue (Failed to start an Appium session, err was: Error: Activity used to start app doesn’t exist or cannot be launched! Make sure it exists and is a launchable activity)

  • Hi Smriti,
    Thanks a lot for the information.
    You have explained each step very clearly.
    Hope you will keep post new information about this automation regularly.

    Thanks,
    Vijay

  • Hi Smriti ,
    I wanted to ask how to switch between two activities. For eg: If SplashActivity is my first activity and LaunchActivity is second, then how do I inspect elements in LaunchActivity page- everytime getting an error saying SplashActivity never started.

  • Hi Smriti Tuteja,
    Iam facing issue on getting element from the pop up in android . The pop up is located in the frame layout .. Any way to get the element from the pop up in android

  • Hello Smriti.
    Nice and descriptive blog posts. I am new to mobile testing and your blog helped me a lot.
    I have some doubts.

    1. Neither Appium inspector nor UIAutomator shows me XPath. I have installed the latest versions of SDK.
    2. Can we access android views(ImageViews, TextViews) properties in our script ? Say for an example we want to check if an image is displayed or not in a widget, can we directly access the ImageView Propeties in our script to validate that?

Leave a Reply

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