Arun Agrawal’s Blog

Ruby on Rails Developer

Use Selenium as a Script

Hey All, I came with a situation where i need to test things from browser. It nothing to do with the different browsers. It just to check some validations, some messages with some existing data with me. I can’t touch the code base. It’s something like QA work. I am not very much aware about using of selenium IDE which is available in browsers. I look into the selenium world with ruby and found some interesting stuff that i can script my test and run for a browser. To run into the browser i need to setup selenium-rc server running. I have done it in my way. Just small code and using selenium-client gem which allows me to start and stop the selenium-rc server. Here is my code for selenium-rc server. It also includes the selenium-jar file. For running selenium-rc server. Just clone it. bundle install and then rake selenium:rc:start All set. Now you are ready to run selenium script from your local machine. To test things i am using hitting up and validating stuff. A google example is also given on the selenium-client gems readme.
#!/usr/bin/env ruby
# Sample Ruby script using the Selenium client API
require "rubygems"
gem "selenium-client", ">=1.2.16"
require "selenium/client"

  @browser = \
      :host => "localhost", 
      :port => 4444, 
      :browser => "*firefox", 
      :url => "", 
      :timeout_in_second => 60

  @browser.start_new_browser_session "/"
    @browser.type "q", "Selenium" "btnG", :wait_for => :page
    puts @browser.text?("")
You can just run above script after start the selenium-rc server and see the result yourself in the browser. Some useful links for get up and running selenium with ruby. Selenium-client for ruby :- All about selenium with ruby :-

Gem 1.5 With Rails 2.3

You may fall down into the situation where you don’t have RVM and your system gem is upgraded for using latest things. And your old application is still running on older version of rails. This is just a workaround of using Gem > 1.3.7 in Rails 2.3 Applications. I have tested this solution with Rails 2.3.5 and different version of gems. After upgrading gems to 1.6.2 i have got an error
uninitialized constant ActiveSupport::Dependencies::Mutex (NameError)
To fix this error need to update boot.rb file. Place this at the top of boot.rb
require 'thread'
After adding this you should be getting this error
 `requirement': undefined local variable or method `version_requirements'
To fix this error you need update your environment.rb file. Add this code above your block.
if >='1.3.7')
 module Rails
   class GemDependency
     def requirement
       r = super
       (r == Gem::Requirement.default) ? nil : r
Now your application should start running properly. Have fun ;) Now you can downgrade or upgrade your system gem version. Your application will still run. Above workaround works for me very well. Any other ideas?

Redis Key-value Store

Redis is really cool and lightweight key-value store. If you are looking for something in which you can store some string, hashes, lists, sets. TheĀ Redis is the best. If you are a Ruby developer then you must try out this with a redis-rb gem. Very easy to configure, very easy to store things. Following is the way of using redis in Ruby way. To install
gem install redis
To load
require 'redis'
Before performing any operation with redis server you need to install Redis and start the redis server. After that you can do like
redis = # Automatically connect with the default port.

# if you have changed the port then you can specify the port in initialize.

>> redis.set "foo", "bar"
=> "OK"

>> redis.get "foo"
=> "bar"
Storing objects
>> redis.set "foo", [1, 2, 3].to_json
=> OK

>> JSON.parse(redis.get("foo"))
=> [1, 2, 3]
There are lot’s more things you can do with the Redis. Links help you in redis Redis official documentation ( Github redis repository ( Github redis rubygem repository (

Bundler Usage – Installing Gems

Every time you change your Gemfile then you might adding/removing any dependancies in your application. Just bundle install will install gems for you. The output may look like
$ bundle install 
Fetching git://
Fetching source index for 
Using rake (0.8.7) 
Installing abstract (1.0.0)
Your bundle is complete! Use `bundle show [gemname]` to see where a
bundled gem is installed.
You can specify groups for special environments
$ bundle install --without development test
$ bundle install --without test
You can specify installation directory for your gems. This will install all the gems into tmp/bundle folder
$ bundle install --path tmp/bundle
You can check your applications bundler config.
$ bundle config 

Settings are listed in order of priority. The top value will be used.

  Set for your local app (/Users/arunagw/foobar/.bundle/config): "1"

  Set for your local app (/Users/arunagw/foobar/.bundle/config): "tmp/bundle"
For more details about bundler you can checkout And also

Bundler Usage – Manage Gemfile

In the previous post we posted about basic Bundler usage. Here i am posting about some advance way of managing Gemfile. Bundler requires a Gemfile. If you are creating a Rails application which is above to version 3, the Gemfile automatic gets created. Gemfile contains all your dependent gems.
gem "rails", "~> 3.0.5"

gem "mysql2"
The above Gemfile explains that we need Rails 3.0.5 and Mysql2 gems for our application. Gemfile can be manage in different ways. We can group things together which is only used in specific environment.
group :test do 
  gem "faker
group :development, :test do
  gem 'ruby-debug'
We can specify the gem version as a second argument in gem. If there is no version then it will fetch the latest one. Some time gem name is different and the library needs to be loaded may be different. Then we need to specify specifically the lib name.
gem 'sqlite3-ruby', :require => 'sqlite3'
Loading gem from a git repository directly If our library or gem is hosted somewhere else then we can add more source at the top of Gemfile. But if we want to fetch Gem from a git repository then we need to something like below. But that library must contain a .gemspec file to consider as a Gem.
gem 'nokogiri', :git => 'git://'
We can also specify a branch/tag/ref of git repository.
gem 'nokogiri', :git => 'git://',
                                   :branch => 'stable-2'
gem 'nokogiri', :git => 'git://', 
                                   :tag => 'tag-2'
gem 'nokogiri', :git => 'git://', 
                                   :ref => '23456'
We can load gems from our local path also.
gem 'nokogiri', :path => '~/code/nokogiri'