N5ken's technical blog

Just another WordPress.com site

Tag Archives: Test Automation

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.

Tips

  • 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!"
element.submit

driver.quit

to be continued…

Using Ruby STAF interface on Ubuntu

As we know, STAF is a fantastic test automation framework which supports Java, Python, C++ perfectly except Ruby. but there still a solution for this, in the following post, there is an interface for the original Ruby:

http://sourceforge.net/tracker/index.php?func=detail&aid=643842&group_id=33142&atid=407383

But while I’m trying to compile this library according to this post on Ubuntu, i got:

root@ubuntu:~/Downloads/rubystaf# ruby extconf.rb
checking for STAFRegister() in -lSTAF... no
creating Makefile

It because of missing the required libraries, so include the path to STAF installation path:

root@ubuntu:~/Downloads/rubystaf# ruby extconf.rb --with-staf-dir=/usr/local/staf/ --with-staf-lib=/usr/local/staf/lib
checking for STAFRegister() in -lSTAF... yes
creating Makefile

Then we can create the “Makefile” successfully

Next, run “make” to make the library, but some errors occur here

root@ubuntu:~/Downloads/rubystaf# make
gcc -I. -I/usr/local/ruby/include/ruby-1.9.1/i686-linux -I/usr/local/ruby/include/ruby-1.9.1/ruby/backward -I/usr/local/ruby/include/ruby-1.9.1 -I. -I/usr/local/staf//include  -D_FILE_OFFSET_BITS=64  -fPIC -O3 -ggdb -Wextra -Wno-unused-parameter -Wno-parentheses -Wpointer-arith -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long  -o STAFHandle.o -c STAFHandle.c
STAFHandle.c: In function ‘sh_new’:
STAFHandle.c:74: error: ‘rb_get_argv’ declared as function returning an array
STAFHandle.c:74: error: conflicting types for ‘rb_get_argv’
/usr/local/ruby/include/ruby-1.9.1/ruby/intern.h:571: note: previous declaration of ‘rb_get_argv’ was here
STAFHandle.c:90: warning: initialization makes pointer from integer without a cast
STAFHandle.c:101: error: subscripted value is neither array nor pointer
STAFHandle.c:102: error: subscripted value is neither array nor pointer
STAFHandle.c: In function ‘sh_submit’:
STAFHandle.c:122: warning: assignment makes pointer from integer without a cast
STAFHandle.c:123: warning: assignment makes pointer from integer without a cast
STAFHandle.c:124: warning: assignment makes pointer from integer without a cast
make: *** [STAFHandle.o] Error 1

As we can see, there is a conflicting types in line 74, 101 and 102, because in ruby 1.9.x “rb_argv” was changed to “rb_get_argv”, so just rename the variant name from “rb_argv” to “rb_get_argv” and make again:

http://www.ruby-forum.com/topic/153426

root@ubuntu:~/Downloads/rubystaf# make
gcc -I. -I/usr/local/ruby/include/ruby-1.9.1/i686-linux -I/usr/local/ruby/include/ruby-1.9.1/ruby/backward -I/usr/local/ruby/include/ruby-1.9.1 -I. -I/usr/local/staf//include  -D_FILE_OFFSET_BITS=64  -fPIC -O3 -ggdb -Wextra -Wno-unused-parameter -Wno-parentheses -Wpointer-arith -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long  -o STAFHandle.o -c STAFHandle.c
gcc -shared -o STAFHandle.so STAFHandle.o -L. -L/usr/local/ruby/lib -Wl,-R/usr/local/ruby/lib -L/usr/local/staf/lib -Wl,-R/usr/local/staf/lib -L/usr/local/staf//lib -Wl,-R/usr/local/staf//lib -L.  -rdynamic -Wl,-export-dynamic   -lSTAF  -lpthread -lrt -ldl -lcrypt -lm   -lc

After this step, we should see “STAFHandle.so” in the build path, now for using this library, just simply copy “STAFException.rb”, “STAFResult.rb” and “STAFHandle.so” under the directory named “staf”

For the testing, we can create a new ruby file and run the code below:

$LOAD_PATH << '.'

require 'STAFCommand.rb'

staf_result = STAF::STAFCommand.new 'local', 'ping', 'ping'

puts "STAF rc: #{staf_result.rc}"
puts "STAF result: #{staf_result.result}"
puts "Error message: #{staf_result.errorMsg}"

If no error come up, that means you have build the STAFHandle for Ruby properly~ 🙂

NOTE:

If you are using Ruby 1.9.x, you will receive:

ruby: symbol lookup error: /root/Downloads/rubystaf/staf/STAFHandle.so: undefined symbol: STR2CSTR

It because STR2CSTR was deprecated in ruby 1.9.x, so we need to use StringValuePtr instead, so you need to open the  “STAFHandle.c” to replace all STR2CSTR and compile again

refer to: http://isitruby19.com/sqlite3-ruby