N5ken's technical blog

Just another WordPress.com site

IRB Trick

IRB subsessions let you try things without ending or affecting any of your existing subsessions. The commands to work with subsessions are:

irb start a new subsession
jobs list subsessions
fg # switch to a subsession
kill # kill a subsession

>> my_string = "foo"
=> "foo"
>> irb
>> my_string
NameError: undefined local variable or method `my_string' for main:Object
from (irb#1):1
>> jobs
=> #0->irb on main (#: stop)
#1->irb#1 on main (#: running)
>> fg 0
=> #, @signal_status=:IN_EVAL, @scanner=#>
>> my_string
=> "foo"

Cucumbergherkin Syntax Highlighting for Notepad++

Sometimes I want to edit a .feature file and don’t have IDE handy, so I decided to create a syntax highlighter for Notepad++.

To install it, simply open Notepad++ and move to the User Defined dialogue from the View menu. Save the following XML as Gherkin.xml and click import. If you open a .feature file from Notepad++, it should now have some color coding.

    <UserLang name="Gherkin" ext="feature">
            <Global caseIgnored="no" />
            <TreatAsSymbol comment="no" commentLine="no" />
            <Prefix words1="no" words2="yes" words3="no" words4="no" />
            <Keywords name="Delimiters"><00>00</Keywords>
            <Keywords name="Folder+"></Keywords>
            <Keywords name="Folder-"></Keywords>
            <Keywords name="Operators"></Keywords>
            <Keywords name="Comment"> 1 2 0|</Keywords>
            <Keywords name="Words1">Feature: Scenario: When Then And Given Examples: Scenario Outline: Background:</Keywords>
            <Keywords name="Words2">@</Keywords>
            <Keywords name="Words3"></Keywords>
            <Keywords name="Words4"></Keywords>
            <WordsStyle name="DEFAULT" styleID="11" fgColor="000000" bgColor="FFFFFF" fontName="Consolas" fontStyle="0" fontSize="10" />
            <WordsStyle name="FOLDEROPEN" styleID="12" fgColor="000000" bgColor="FFFFFF" fontName="" fontStyle="0" />
            <WordsStyle name="FOLDERCLOSE" styleID="13" fgColor="000000" bgColor="FFFFFF" fontName="" fontStyle="0" />
            <WordsStyle name="KEYWORD1" styleID="5" fgColor="0000FF" bgColor="FFFFFF" fontName="Consolas" fontStyle="0" fontSize="10" />
            <WordsStyle name="KEYWORD2" styleID="6" fgColor="009393" bgColor="FFFFFF" fontName="Consolas" fontStyle="0" fontSize="10" />
            <WordsStyle name="KEYWORD3" styleID="7" fgColor="A80000" bgColor="FFFFFF" fontName="Consolas" fontStyle="0" fontSize="10" />
            <WordsStyle name="KEYWORD4" styleID="8" fgColor="800000" bgColor="FFFFFF" fontName="" fontStyle="0" />
            <WordsStyle name="COMMENT" styleID="1" fgColor="000000" bgColor="FFFFFF" fontName="Consolas" fontStyle="0" fontSize="10" />
            <WordsStyle name="COMMENT LINE" styleID="2" fgColor="800000" bgColor="FFFFFF" fontName="Consolas" fontStyle="0" fontSize="10" />
            <WordsStyle name="NUMBER" styleID="4" fgColor="000000" bgColor="FFFFFF" fontName="" fontStyle="0" />
            <WordsStyle name="OPERATOR" styleID="10" fgColor="000000" bgColor="FFFFFF" fontName="" fontStyle="0" />
            <WordsStyle name="DELIMINER1" styleID="14" fgColor="B70000" bgColor="FFFFFF" fontName="" fontStyle="0" />
            <WordsStyle name="DELIMINER2" styleID="15" fgColor="000000" bgColor="FFFFFF" fontName="" fontStyle="0" />
            <WordsStyle name="DELIMINER3" styleID="16" fgColor="000000" bgColor="FFFFFF" fontName="" fontStyle="0" />

Installing mysql2 gem in Mac OS X 10.6.x

It’s very exciting that i can finally install mysql2 on Mac OS X 10.6 successfully!!

solved with

sudo gem install mysql2 -- --srcdir=/usr/local/mysql/include

refer from: https://gist.github.com/972626

it’s really great!!!!! hope this can help you.

Uninstalling MySQL on Mac OS X Leopard

To uninstall MySQL and completely remove it (including all databases) from your Mac do the following:

Use mysqldump to backup your databases to text files!
Stop the database server
sudo rm /usr/local/mysql
sudo rm -rf /usr/local/mysql*
sudo rm -rf /Library/StartupItems/MySQLCOM
sudo rm -rf /Library/PreferencePanes/My*
edit /etc/hostconfig and remove the line MYSQLCOM=-YES-
rm -rf ~/Library/PreferencePanes/My*
sudo rm -rf /Library/Receipts/mysql*
sudo rm -rf /Library/Receipts/MySQL*

The last two lines are particularly important as otherwise, you can’t install an older version of MySQL even though you think that you’ve completely deleted the newer version!

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…

Online syntax highlighting tool for the programming language

This is a test of http://www.tohtml.com, to generate the highlighted codes

class Product < Mustache::Rail
  def initialize
    @products = Product.find.all

  def products
    return @products

  def total_price
    price = 0.0
    @products.each do |product|
      price += product.price

Installing Ruby DevKit on Windows

When I upgrading Cucumber 1.0.1 on Windows 7 this morning, i got an error while it’s trying to install the compile version of json gem, it because in Cucumber 1.0.x, json have become a dependence. From this thread we can see, aslak hellesoy did not test this on Win platform before release  http://comments.gmane.org/gmane.comp.programming.tools.cucumber/6669, so we need to install Ruby DevKit for compiling the json C library, okay, now following the steps:

1. Download DevKit from http://rubyinstaller.org/downloads/

2. Unzip file and cd <DEVKIT_INSTALL_DIR>

3. Run “ruby dk.rb init” to create the configure file config.yml,

4. [optional] ruby dk.rb review to review the list of Rubies to be enhanced to use the DevKit and verify the changes you made to it are correct.

5. finally, ruby dk.rb install to DevKit enhance your installed Rubies. This step installs (or updates) an operating_system.rb file into the relevant directory needed to implement a RubyGems pre_install hook and a devkit.rb helper library file into <RUBY_INSTALL_DIR>\lib\ruby\site_ruby. NOTE: you may need to use the –force option to update (with backup of the originals) the above mentioned files as discussed at the SFX DevKit upgrade FAQ entry.

Install ansicon for cucumber to get coloured output on Windows

Today, I use Cucumber 1.0.1 for my testing on Windows 7, it runs quite good except the problem of the output color, this because Windows do not support colored output in command line prompt, however, we still have a solution, that is ANSICON, you can find it in http://adoxa.110mb.com/ansicon/ and download the latest version and follow the steps below to complete your installation:

1. Download and unzip the latest file from http://adoxa.110mb.com/ansicon
2. Open cmd and navigate to the unzipped folder
3. Navigate to x64 (if you have a 64 bit machine) otherwise navigate to x86
4. Execute ‘ansicon.exe -i’ to install and add ansicon to your Windows
5. Run your cucumber 0.10.0 test and you should get the coloured output result on Windows 😉
6. For more information of the options, you can type ‘ansicon.exe -h’ for help

D:\Data\ansicon\x86>ansicon.exe -h
ANSICON by Jason Hood .
Version 1.32 (22 December, 2010).  Freeware.

Process ANSI escape sequences in Win32 console programs.

ansicon -i|I | -u|U
ansicon [-m[]] [-p | -e|E string | -t|T [file(s)] | program [args]]

  -i            install - add ANSICON to the AutoRun entry (implies -p)
  -u            uninstall - remove ANSICON from the AutoRun entry
  -I -U         use local machine instead of current user
  -m            use grey on black ("monochrome") or  as default color
  -p            hook into the parent process
  -e            echo string
  -E            echo string, don't append newline
  -t            display files ("-" for stdin), combined as a single stream
  -T            display files, name first, blank line before and after
  program       run the specified program
  nothing       run a new command processor, or display stdin if redirected

 is one or two hexadecimal digits; please use "COLOR /?" for details.

Could not find a JavaScript runtime error in rails 3.1

If you are trying out the latest release of rails 3.1, you might encounter a problem that quotes:

Could not find a JavaScript runtime. See https://github.com/sstephenson/execjs for a list of available runtimes.

I think this problem will makes the beginner confuse, or it should provides a more explicit message or instruction.

After spending a few minutes searching on the web, I found that you can fix this problem by simply adding gem ‘therubyracer’ into your Gemfile (it’s for Ruby MRI).

gem 'therubyracer

And that’s all folks, enjoy rails 3.1 which integrates jquery, scss, coffeescript, and lots more.. by default 🙂

Isolate your test environment in VM Desktop from the intranet of your company

Due to the increasing number of hosts and devises we are using now in our company, so it’s more easier to cause IP conflict than ever, to avoid affecting the test result, it’s better to separate our network from intranet.

It’s a huge benefit for using the virtual network, on the one hand, it saving the precious IP address, on the other hand, the virtual network is more stable for the intranet, so this makes our test result more reliable! Especially in HA, federation, test automation and so on.

In the following tutorial, we will take a look at how to manage our NAT network:

1. After we finish installing the VM Workstation, we should see two more virtual network interface cards in “Network and Sharing Center”

2. The network structure:

3. So firstly, change the network connection setting of our virtual machine:

4. Secondly, restart network interface card or run “ipconfig /renew” to connect to VMnet08 (here is need to confirm that we get a C class IP address, e.g. 192.168.x.x)

5. Lastly, for testing the connectivity, we can ping each other by the host name, and our machine will send ping packages to the network 10.30.x.x and 192.168.x.x.

6. we need to be sure we are pinging the machine in VMnet 08.

7. If you are unable to connect to the internet, please check the gateway of your VM machine, it should be normally.