N5ken's technical blog

Just another WordPress.com site

Using Selenium Android Driver in JRuby

Currently, in the official Selenium releases, the Android Driver only available in Java, not Ruby, so here is a test to use this driver in other language:

For the top, preparing the environment:

Follow the tutorial to complete: http://code.google.com/p/selenium/wiki/AndroidDriver setting up the Android Emulator

Install the SDK

Download the Android SDK, and unpack it in a directory, say ~/android_sdk/ .

Note 1: if your sdk is installed in a different folder, edit ./properties.yml to reflect that location. ./properties.yml also specifies the Android platform version to use – again update the value to the version of the sdk you want to use e.g. version 8 for Android 2.2

Note2: On Linux, android SDK is distributed for 32-bits machine. If you have a 64-bit machine you will need to install ia32-libs http://developer.android.com/sdk/installing.html#troubleshooting

Execute the following command to create a new Android Virtual Device (avd):

$cd ~/android_sdk/tools/
$./android create avd -n my_android -t 12 -c 100M

-n or naming your android virtual device.

-t for specifying the target. (“./android list targets” will give you a list of all available targets). Make sure the target level you selected corresponds to the latest API level.

-c option is for the SD card storage space.

When prompted “Do you wish to create a custom hardware profile no” enter “no”.

Note: an issue has been discovered with the Java-to-JavaScript bridge in version 2.3 of the emulator which causes problems for the Android Driver. Currently we recommend using version of the Android platform 2.2 for AVDs.

Start the Emulator

Start the emulator (this can take a while):

$./emulator -avd my_android &

Install the Application

Install Android WebDriver Application. Wait until the emulator has started and the home screen is ready. Install android-server.apk:

$./adb -e install -r android-server.apk

If you face an issue, make sure you are allowing installation of application not coming from Android Market (Android Emulator -> Settings -> Applications -> Unknown sources is checked)

Setup Port Forwarding

In a terminal type:

$~/android_sdk/adb forward tcp:8080 tcp:8080

This will make the android server available at http://localhost:8080/wd/hub

Start the WebDriver Server on the device or emulator

Start the WebDriver application just installed in the device or emulator.


  • Remember to set the following settings on your device: Settings -> Applications -> Development -> Check “USB debugging”, “Stay Awake” and “Allow mock locations”.
  • Restart adb if you have networking errors reported when your tests run. Sometimes there are issues connecting to the device / emulator after switching devices. run adb kill-server followed by adb start-server then setup port forwarding again for the device you want to use.

Run Your Tests

Run the JRuby codes bellow to launch the example test, you can also find out the missing apache and json library in http://www.findjar.com:

require 'java'
require 'guava-r09.jar'
require 'org/openqa/selenium/base.jar'
require 'org/openqa/selenium/android/android.jar'
require 'org/openqa/selenium/webdriver-api.jar'
require 'org/openqa/selenium/remote/remote.jar'
require 'org/openqa/selenium/remote/common.jar'
require 'org/openqa/selenium/net/net.jar'
require 'org/openqa/selenium/browserlaunchers/browserlaunchers.jar'
require 'org/openqa/selenium/browserlaunchers/launcher-utils.jar'
require 'org/openqa/selenium/browserlaunchers/locators/locators.jar'
require 'httpcomponents-client-4.1.1/lib/httpcore-4.1.jar'
require 'httpcomponents-client-4.1.1/lib/httpclient-4.1.1.jar'
require 'commons-logging-1.1.1/commons-logging-1.1.1.jar'
require 'json-jena-1.0.jar'

import 'org.openqa.selenium.By'
import 'org.openqa.selenium.WebElement'
import 'org.openqa.selenium.android.AndroidDriver'
import 'org.openqa.selenium.remote.SessionId'
import 'org.apache.http.HttpHost'
import 'org.apache.http.client.ClientProtocolException'

driver = AndroidDriver.new
driver.get "http://www.google.com.hk"

element = driver.find_element By.name("q")
element.send_keys "Cheese!"


to be continued…


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: