Selenium Testing Tool


Testing is one of the important aspects of the final product. It could be either a web application or a desktop application. This is one of the best tools I have come across and it is user friendly too. Selenium is one of the key ingredients of testing process.

Selenium is an automated web application testing utility that runs your tests directly in a web browser also. It can be called as a Java script framework that runs in your web-browser. It works where Java script is supported. It can simulate a user navigating through pages and then assert for specific marks on the pages. All you need to really know is HTML to start using it right away!

Selenium IDE includes the entire Selenium Core, allowing you to easily and quickly record and playback tests in the actual environment that they are going to run.

You can choose to use its recording capability, or you may edit your scripts by hand. With auto complete support and the ability to move commands around quickly, Selenium IDE is the ideal environment for creating Selenium tests no matter what style of tests you prefer.

Here is a scenario where Selenium IDE can be used. Imagine that you have created an html form that has some 20 text fields and you have to test them all. Filling the form every time is a tedious job. With selenium, you can automate the whole process and the test as you require. With the introduction it is clear that selenium is truly an amazing tool to test your application. So, lets take a step further and have a look at the installation of selenium IDE.

Installation

You may install Selenium IDE by downloading the package from the following location:
https://addons.mozilla.org/en-US/firefox/addon2079


Once the add on is installed, do not forget to restart your browser.

Now you may access the tool by clicking Tools -> selenium IDE.

This lets you open the Selenium IDE which should look like the image shown below:


You may also select the language that you want by clicking Options -> format -> ruby

Click on the red record button on the right side of the tool.

Now you may start giving values to your application provided the URL is mentioned in the Base URL tab of the selenium IDE.

Once the recording is done you may open a new browser tab and select the play button on the IDE to check your tests automatically.

In the table tab, if you see all the rows in green, it means that the test completed successfully.

Once you are done with the test case select the source code option that lets you access the code and also you may edit the code for further changes.

You can also select the speed of the automated test process through the scroll button shown in the image.

Some Limitations

Minor Issue with SSL: Selenium can’t switch properly between http and https pages.

File Input type not supported: You can’t tell selenium to upload a file. But there is a work around in Firefox.

This should give a clear idea of basic testing with selenium IDE and there are lot many features that can be explored in this little tool.

Source: Railscarma.com

Configure Sphinx in Rails application


Install Sphinx

Just run the following three commands on your server or dev machine to install Sphinx:

./configure make sudo make install

That will setup Sphinx with default for use with MySQL. If you want to use it with PostgreSQL, then run configure with the following flag:

./configure –with-pgsql

Note: you can download the sphinx from

http://sphinxsearch.com/downloads/beta/

Install Thinking Sphinx

Even though there are a couple of Sphinx plugins for Rails, I chose to go with Thinking Sphinx, as it seems to be the most popular and feature complete.

So you can install it as a Rails plugin using script/plugin install command:

script/plugin install git://github.com/freelancing-god/thinking-sphinx.git

Writing code to use sphinx search:

We now need to index our models. This consists of adding a few small lines of code into each model that you want to be able to search. So let’s say we have a Blog app (doesn’t everyone!), which has a Post model. And that Post model contains the usual title and description fields. We therefore add the following bit of code beneath our association declarations in

app/models/post.rb:

define_index do indexes title, description end

Those very short three lines will tell Thinking Sphinx to index the title and description fields of the Post model, and allow us to search through all our posts. Now we just need to index and start Sphinx. And Thinking Sphinx makes this very easy with its handy Rake tasks.

Just run this:

rake ts:rebuild

That will stop (if it is started), index and start Sphinx for you. Now we need to create a quick search form. This will eventually be a global site search, and not just a Post search. So we will create a new controller:

script/generate controller search

Then create a view at app/views/search/index.html.erb and place the form within it:

Now in your new Search controller, create a new createaction:

def create @posts = Post.search params[:search] end

Create your createview at app/views/search/create.html.erbwith a bit of code to display your @postsin the usual way.

Note: We can even paginate our results using the WillPaginate plugin:

def create

@posts = Post.search params[:search], :page => params[:page], :per_page => 10

Source: Railscarma.com

How to install Rails 3.2.3?


This release changes the default value of config.active_record.whitelist_attributes to true. This change only affects newly generated applications so it should not cause any backwards compatibility issues for users who are upgrading but it may affect some tutorials and introductory material. Rails 3.2.3 also introduces a new option that allows you to control the behavior of remote forms when it comes to authenticity_token generation. If you want to fragment cache your forms, authenticity token will also get cached, which isn’t acceptable. However, if you only use such forms with ajax, you can disable token generation, because it will be fetched from meta tag. Starting with 3.2.3, you have an option to stop generating authenticity_token in remote forms (ie. :remote => true is passed as an option), by setting config.action_view.embed_authenticity_token_in_remote_forms = false. Please note that this will break sending those forms with javascript disabled. If you choose to not generate the token in remote forms by default, you can still explicitly pass :authenticity_token => true when generating the form to bypass this setting. The option defaults to true, which means that existing apps are NOT affected.

1. Install rails 3.2.3 gem

gem install rails -v=3.2.3

2. To evaluate Javascript from with in Ruby you need to install

gem install therubyracer

3. Install mysql2 library for ruby to establish connection to database and query on connection

gem install mysql2 -v=0.3

4. To execute rake task install the gem

gem install rake -v=0.9.2

5. bcrypt-ruby is a sophisticated and secure hash algorithm designed by the OpenBSD project for hashing passwords

gem install bcrypt-ruby -v=3.0.0

6. ExecJS lets JavaScript code to run from Ruby. It automatically picks the best runtime available to evaluate your JavaScript program, then returns the result to you as a Ruby object.

gem install execjs

7. The jquery-rails gem which comes with Rails as the standard JavaScript library gem. This gem contains an engine class which inherits from Rails::Engine. By doing this, Rails is informed that the directory for this gem may contain assets and the app/assets, lib/assets and vendor/assets directories.

gem install jquery-rails

8. Asset pipeline

The asset pipeline provides framework to compress Javascript and Css assets. It also adds the ability to write these assets in other languages such as Coffeescript, Sass and ERB.

In previous version of Rails, all assets were located in subdirectories of public such as images, javascripts and stylesheets. With the asset pipeline, the prefferred location for these assets is now the app/assets directory.

For example, if ProjectsController is generated, Rails will add a new file at

app/assets/javascripts/projects.js.coffee

app/assets/stylesheets/projects.css.scss.

Rails 3.2.3 Gemfile

gem “rails”, “~> 3.2.3″

# Bundle edge Rails instead:

# gem ‘rails’, :git => ‘git://github.com/rails/rails.git’

gem ‘mysql2′, ‘>= 0.3′

gem ‘rake’, ‘=0.9.2′

gem ‘jquery-rails’

gem ‘bcrypt-ruby’, ‘~> 3.0.0′

gem ‘execjs’

gem ‘therubyracer’, :platforms => :ruby

# Gems used only for assets and not required

# in production environments by default.

group :assets do

gem ‘sass-rails’, ‘~> 3.2.3′

gem ‘coffee-rails’, ‘~> 3.2.1′

gem ‘uglifier’, ‘>= 1.0.3′

end

# Use unicorn as the web server

# gem ‘unicorn’

# Deploy with Capistrano

# gem ‘capistrano’

# To use debugger

# gem ‘ruby-debug19′, :require => ‘ruby-debug’

group :test do

# Pretty printed test output

gem ‘turn’, :require => false

end

Source:Railscarma.com

Ruby enterprise edition + Passenger installation


Ruby enterprise edition can be installed on the server using the following steps :

$ wget http://rubyforge.org/frs/download.php/68719/ruby-enterprise-1.8.7-2010.01.tar.gz

$ tar -zxvf ruby-enterprise-1.8.7-2010.01.tar.gz
PS: Make sure that the patch is installed on the server. If it doesn’t exist then install it via yum.

$ yum install patch

 

$ ./ruby-enterprise-1.8.7-2010.01/installer

Once the ruby enterprise edition is installed , the following message will appear :

“Ruby Enterprise Edition is successfully installed!”

Apache module for passenger can be installed on the server by executing the following command :

$ /opt/ruby-enterprise-1.8.7-2010.01/bin/passenger-install-apache2-module

Add the following entries in an include file (say, /usr/local/apache/conf/mod_rails.conf) to load the mod_passenger.so file.

LoadModule passenger_module /opt/ruby-enterprise-1.8.7-2010.01/lib/ruby/gems/1.8/gems/passenger-2.2.11/ext/apache2/mod_passenger.so

PassengerRoot /opt/ruby-enterprise-1.8.7-2010.01/lib/ruby/gems/1.8/gems/passenger-2.2.11

PassengerRuby /opt/ruby-enterprise-1.8.7-2010.01/bin/ruby

PassengerMaxPoolSize 30

PassengerMaxInstancesPerApp 1

PassengerPoolIdleTime 60

RailsAutoDetect off

PS: PassengerMaxPoolSize can be set to 30 for a sever which is having a minimum RAM of 2GB.
Also, include the mod_rails.conf to the apache configuration file by adding the following line to it :

Include “/usr/local/apache/conf/mod_rails.conf”

Reference: http://www.modrails.com/documentation/Users guide.html

After restarting the apache webserver you can see the passenger process running under the user root:

root 31908 0.0 0.1 3640 1484 ? Sl 00:40 0:00 /opt/ruby-enterprise-1.8.7-2010.01/lib/ruby/gems/1.8/gems/passenger-2.2.11/ext/apache2/ApplicationPoolServerExecutable 0 /opt/ruby-enterprise-1.8.7-2010.01/lib/ruby/gems/1.8/gems/passenger-2.2.11/bin/passenger-spawn-server /opt/ruby-enterprise-1.8.7-2010.01/bin/ruby /tmp/passenger.5338

Configuring a rails app under Passenger

It can be done by adding the following entries to the virtual host entry of the domain corresponding to the rails app :

RailsBaseURI /

 

PassengerAppRoot /home/username/railsapp

Also, make sure that the DocumentRoot is pointing to the public folder of the app.

Source: Railscarma.com

Charts in Rails – XML/SWF Charts with Great Features


We have been working on a project that deals with server data where charts are worth a thousand words. I always get requests for various charts and in the past I’ve used Gruff to produce them. Gruff is a great library and the results look good too! However, recently I’ve have had more demanding requirements to provide more dynamic behavior on the charts. Users wanted to be able to interact with the charts in various ways and drill down to gather more information.

I looked at the alternatives to Gruff and decided to go with XML/SWF charts. These frameworks use Flash rendering for charts and have great features, 24hr turn around support and they’re inexpensive. The other advantage is that we no longer had to deal with figuring out the various spells to install RMagick on client’s servers. Moreover, we were able to delegate the actual chart rendering over to the client, which with dynamic graphs is key. All we had to do now on the server side was generate XML (for CPU usage in real time).

In this article, I would like to throw some light on various charts that are available for rails like Gruff, Google charts, Ziya charts, Open Flash charts and Fusion charts.

I am currently using Open Flash charts for the current project.

Gruff charts:

Gruff is a Ruby library for generating attractive and powerful graphs, useful for web applications, print media and many others.

Snapshot Rendering

You can render a Gruff graph as many times as you want and change any settings between renders. Data, colors, even the graph size and image type can all be changed for the next render.

Easily Extensible

Gruff is designed to be extremely extensible. Adding new graph types or themes can be done in as little as a few lines of code. If you need more control over your graphs, you can customize the layouts, data generation, almost anything.

charts in railsInstallation

sudo gem install gruff

Demonstration of usage

Pie Chart example

require ‘rubygems’

require ‘gruff’

g = Gruff::Line.new

g.title = “My Graph”

g.data(“Apples”, [1, 2, 3, 4, 4, 3])

g.data(“Oranges”, [4, 8, 7, 9, 8, 9])

g.data(“Watermelon”, [2, 3, 1, 5, 6, 8])

g.data(“Peaches”, [9, 9, 10, 8, 7, 9])

g.labels = {0 => ’2003′, 2 => ’2004′, 4 => ’2005′}

g.write(‘my_fruity_graph.png’)

Google charts:

The Google Chart API returns a PNG-format image in response to a URL. Several types of image can be generated, including line, bar, and pie charts. For each image type, you can specify attributes such as size, colors, and labels.

Installation ruby script/plugin install http://google-charts-on-rails.googlecode.com/svn/google_charts_on_rails/

Once installed, start using it directly in your project.

Pie Examplepie chart in rails

GoogleChart.pie(10,20,40,30).to_url

With labels:

GoogleChart.pie([‘1997’,10],[‘1998’,20],[‘1999’,40],[‘2000’,30]).to_url


Ziya Charts:

Ziya allows you to easily create interactive charts, gauges and maps for your web applications. Ziya leverages flash which offloads heavy server side processing to the client. At the root, Ziya allows you to easily generate an XML files that will be downloaded  for rendering. Using this gem, you will be able to  create great looking charts for your application easily.

Features :
-Supports a wide variety of chart/gauge types.
-Geographical maps.
-Relieves your server load by generating the actual chart on the client side.
-Themes support. You can change the appearance and behavior of any charts by introducing new   themes.

Installation :
> gem sources -a http://gems.github.com
> sudo gem install derailed-ziya

cd to your application directory and issue the following command:
> ziyafy –charts
This will copy the necessary themes and flash files to run ZiYa in your application’s public/charts directory. You can install maps and gauges components as well. Type in:

> ziyafy –help
To see all available options.

Open Flash Charts:

Features :

-More dynamic graphs with eye catching tooltips and legends for readability.
-Various types of charts available, except real time charts.
-Simple and easy to install the plug-in. Does not need and gem like Rmagick or ImageMagick.
-Data is rendered and displayed in SWF format.

To install and use the open flash chart plug-in for Rails do the following:

1.script/plugin install http://svn.pullmonkey.com/plugins/trunk/open_flash_chart/
2.Move the open_flash_chart.swf file into your RAILS_ROOT/public directory
3.Move the swfobject.js file into your RAILS_ROOT/public/javascripts directory
4.Create a controller and a view as in my examples below.

Implementation:

def index_stacked_bar
@graph = open_flash_chart_object(600,300,”/test_it/graph_code_stacked_bar”)
end

def graph_code_stacked_bar
title = Title.new(“A stacked bar chart”)
title.set_style( “{font-size: 20px; color: #F24062; text-align: center;}” );
bar_stack = BarStack.new
bsv1 = BarStackValue.new(2.5, ‘#C4D318′) # yellow
bsv2 = BarStackValue.new(5, ‘#50284A’) # purple
bsv3 = BarStackValue.new(1.25, ‘#7D7B6A’) # gray
bsv31 = BarStackValue.new(1.25, ‘#C4D318′) # yellow
bar_stack.append_stack( Array.new( [bsv1, bsv2, bsv1 ]) ); # 0
bar_stack.append_stack( Array.new( [bsv1, bsv2, bsv3,bsv31 ]) ); # 0
bar_stack.set_tooltip( ‘X label [#x_label#], Value [#val#]<br>Total [#total#]‘ );
y = YAxis.new();
y.set_range( 0, 14, 2 );

x = XAxis.new();
x.set_labels_from_array( Array.new( [‘Winter’, ‘Spring’, ‘Summer’, ‘Autmn’ ]) );
tooltip = Tooltip.new;
tooltip.set_hover();
chart = OpenFlashChart.new
chart.set_title(title)
chart.add_element(bar_stack)
chart.x_axis = x ;
chart.y_axis = y ;
chart.set_tooltip( tooltip );
render :text => chart.to_s
end

Fusion Charts:

Fusion Charts is a flash-based charting component which can generate interactive and animated charts. Fusion Charts can be used with any web scripting language, to deliver powerful charts with fairly minimal coding. Fusion Charts comes with wrapper modules for use with RoR.

Fusion Charts offers over 45 types of 2D/3D charts including line, area, bar, column, pie, doughnut (donut), combination, scatter, bubble, scroll charts etc. The functional and cosmetic aspects of each chart can be extensively customized using the XML API used by Fusion Charts.

Generating charts using Fusion Charts in Ruby on Rails through a sample application. To run the sample application, the following would be needed:

• FusionCharts Free/ v3:
Fusion Charts Free can be downloaded by clicking
here

or the commercial version with more options can be downloaded directly from www.fusioncharts.com .

Installation of Fusion Charts involves just copying and pasting the SWF and .rb files from the download package into appropriate application folder. The .rb files are present in Download Package > Code > RoR > Libraries folder.

• Ruby 1.8.6 or above
• Rails gem 2.0.2 or above:
• MySQL 4.0 or above

All the charts explained above have its own advantages and disadvantages. The best way to use them depends on the type of the requirements of your project. Hope this article helps to implement the best use of the charts available in rails.

Source: Railscarma.com

RoR installation from source and deploying a test app


Download the ruby source, then unzip, untar, configure and install

[root@railstestwebmin ~]#wget http://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.3-p194.tar.bz2

[root@railstestwebmin ~]#tar xjvf ruby-1.9.3-p194.tar.bz2

[root@railstestwebmin ~]#cd ruby-1.9.3-p194

[root@railstestwebmin ruby-1.9.3-p194]#./configure

[root@railstestwebmin ruby-1.9.3-p194]#make

[root@railstestwebmin ruby-1.9.3-p194]#make install

Check the ruby version

[root@railstestwebmin ~]#ruby -v

Download ruby gems and install

[root@railstestwebmin ~]#wget http://rubyforge.org/frs/download.php/76073/rubygems-1.8.24.tgz

[root@railstestwebmin ~]#tar xzvf rubygems-1.8.24.tgz

[root@railstestwebmin ~]#cd rubygems-1.8.24

[root@railstestwebmin rubygems-1.8.24]#ruby setup.rb

Installing Rails by using ruby gems (gem is a package manager)

[root@railstestwebmin ~]#gem install rails –include-dependencies

Creating an application

[root@railstestwebmin ~]#rails new prv —for creating prv application and rails is a binary command

inside this application root scripts folder, check the database.yml file for which database this application is using.

Running the application

[root@railstestwebmin prv]#rails server

inside that app directory run rails server command, it may give some error like, in this case i got sqlite3 is not found.

sqlite3 database it used by the app and it is not installed so, we need to install sqlite2.

[root@railstestwebmin prv]#yum install sqlite-devel

After, while running the app I got the error like

—-

/usr/lib/ruby/gems/1.8/gems/execjs-1.4.0/lib/execjs/runtimes.rb:51:in `autodetect’: Could not find a JavaScript runtime. See https://github.com/sstephenson/execjs for a list of available runtimes. (ExecJS::RuntimeUnavailable)”

so we ve to install java script runner ie add a gem in Gemfile of that app

like gem ‘therubyracer’ or gem ‘execJS’ , here therubyracer and execJS are supports the java scripts.

after adding javascript gem in Gemfile

do bundle install it ll read the Gemfile and installs all gems which are mentioned in that file,

then run “rails server” in that app root ,

[root@railstestwebmin prv]#rails server

=> Booting WEBrick

=> Rails 3.1.0 application starting in development on http://0.0.0.0:3000

=> Call with -d to detach

=> Ctrl-C to shutdown server

[2012-09-07 17:09:45] INFO WEBrick 1.3.1

[2012-09-07 17:09:45] INFO ruby 1.8.7 (2010-06-23) [i386-linux]

[2012-09-07 17:09:45] INFO WEBrick::HTTPServer#start: pid=3049 port=3000

Go to the browser and You can see the Rails welcom page
http://<server ip/localhost>:3000

Source: Railscarma.com

How to Setup Rails Development Environment?


Couple of months back I had a situation, which concerned about installing Ruby and Rails on my system. After reading through many blogs and posts related with installation, at last I found RVM (Ruby Version Manager) as the best option among all. Most importantly I didn’t need to Google all the errors, which pops up while installing them using other methods. With RVM comes lots of flexibility and installation is pretty easier.

Note: I have tested these installation steps on ubuntu versions.

Do’s and Dont’s

 This beginner’s guide will set up with Ruby 1.9.3, RVM and Rails 3.1.3 and is specifically written for a development environment on Ubuntu 10.10, but will probably work on many other operating systems, including older/newer versions of Ubuntu and Debian.

  • If one is looking for a way to set this up on a production server then the use of the “railsready script” is recommended which installs all the necessary packages for Ruby 1.9.3p0 and then that version of Ruby itself, Bundler and Rails. Then it is left to user to install Apache or nginx to get your application online.
  • Under no circumstance should one install Ruby, Rubygems or any Ruby related packages from apt-get. This system is outdated and leads to major headaches. Avoid it for Ruby-related packages.
Installation Procedure

1. First of all, update the system, so that all the latest resources are updated to install latest packages.

   Open terminal(alt+ctrl+t).

   In terminal type:- sudo apt-get update

2. Next, we’re going to install Git (a version control system) and curl which are both required to install and use RVM, and build essential which is required to compile Ruby versions, amongst other compilable things. To install these three packages we use this command:

    sudo apt-get install build-essential git-core curl

3. Installing RVM (Ruby Version Manager)

  RVM is a Ruby Version Manager created by Wayne E. Seguin and is extremely helpful for installing and managing many different versions of Ruby all at once. Sometimes you could be working on a project that requires an older (1.8.7) version of Ruby but also need a new version (1.9.3) for one of your newer projects. This is a problem that RVM solves beautifully.

  Another situation could be that you want to have different sets of gems on the same version of Ruby but don’t want to have to do deal with Gem Conflict Hell. RVM has gemsets for this. This is a feature you wouldn’t have if you used the packaged Ruby.

=> With git-core and curl installed we’ll be able to install RVM with this command: => Run this command in terminal bash < <(curl -s https://rvm.beginrescueend.com/install/rvm) =>The beautiful part of this is that it installs Ruby to our home directory, providing a sandboxed environment just for us.

=>Once that is done add following line to your ~/.bashrc file. => echo ‘[[ -s “$HOME/.rvm/scripts/rvm” ]] && source “$HOME/.rvm/scripts/rvm”‘ >> ~/.bashrc //.bashrc file is responsible for setting up the bash session

=>Then we’ll need to reload ~/.bashrc file,which can be done using this small command. => . ~/.bashrc => After using above command,if error comes i.e rvm(commadn not found) etc…Then simply close this terminal and start again(Beleive me,few times such things do work!!!)

=>Next we have to see what are the things required by RVM to install Ruby on Rails. Just use:- => rvm requirements //this will show all the packages required to be installed before,few of which we may have already installed.

=>To install all the required packages mentioned by rvm requirements,just run following command:- =>sudo apt-get install build-essential openssl libreadline6 libreadline6-dev curl git-core //please note these all should be contained in single line, otherwise zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-0 libsqlite3-dev sqlite3 libxml2-dev //few packages may not get installed. libxslt-dev autoconf libc6-dev ncurses-dev automake libtool bison subversion

  1. Installing Ruby and Rails–>Installing Ruby4.1 With RVM and packages installed,we can install ruby using :- => rvm install 1.9.3 //this will install ruby with 1.9.3 version and it takes a little time to get installed. Be patient.4.2 With RVM installed, we can use that particular version to be used:- => rvm use 1.9.3 //now the ruby 1.9.3 will be used as a ruby version4.3 To check the version of the ruby version installed:- => ruby -v //check the current version being used4.4 To make a particular version to be used as default,we can use:- => rvm –default use 1.9.3=> Installing Rails4.5 As we have installed ruby in our home directory, we dont need to use sudo command for rails installation. Simply type following command:-
    => gem install rails -v 3.0.9 //This will install 3.0.9 version.Any suitable version can be downloaded by just mentioning the version here.
     //It will also install the rails gem and the other 22 gems that it and its dependencies depend on, including Bundler.

5.Well, Thats it !!  Enjoy coding !

Source: Railscarma.com