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. https://github.com/arunagw/selenium-server 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 google.com 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"

begin
  @browser = Selenium::Client::Driver.new \
      :host => "localhost", 
      :port => 4444, 
      :browser => "*firefox", 
      :url => "http://www.google.com", 
      :timeout_in_second => 60

  @browser.start_new_browser_session
    @browser.open "/"
    @browser.type "q", "Selenium seleniumhq.org"
    @browser.click "btnG", :wait_for => :page
    puts @browser.text?("seleniumhq.org")
ensure
  @browser.close_current_browser_session    
end
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 :- https://github.com/ph7/selenium-client All about selenium with ruby :- http://seleniumhq.org/projects/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
/activesupport-2.3.5/lib/active_support/dependencies.rb:55: 
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
 
/gem_dependency.rb:119:in
 `requirement': undefined local variable or method `version_requirements'
To fix this error you need update your environment.rb file. Add this code above your Rails::Initializer.run block.
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.3.7')
 module Rails
   class GemDependency
     def requirement
       r = super
       (r == Gem::Requirement.default) ? nil : r
     end
   end
 end
end
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 = Redis.new # 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 (http://redis.io) Github redis repository (https://github.com/antirez/redis) Github redis rubygem repository (https://github.com/ezmobius/redis-rb)

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://github.com/rails/rails.git
Fetching source index for http://rubygems.org/ 
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.

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

path
  Set for your local app (/Users/arunagw/foobar/.bundle/config): "tmp/bundle"
For more details about bundler you can checkout http://gembundler.com/ And also http://railscasts.com/episodes/201-bundler

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
end
group :development, :test do
  gem 'ruby-debug'
end
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://github.com/tenderlove/nokogiri.git'
We can also specify a branch/tag/ref of git repository.
gem 'nokogiri', :git => 'git://github.com/tenderlove/nokogiri.git',
                                   :branch => 'stable-2'
gem 'nokogiri', :git => 'git://github.com/tenderlove/nokogiri.git', 
                                   :tag => 'tag-2'
gem 'nokogiri', :git => 'git://github.com/tenderlove/nokogiri.git', 
                                   :ref => '23456'
We can load gems from our local path also.
gem 'nokogiri', :path => '~/code/nokogiri'