Wednesday, October 7, 2009

Running Selenium tests written in Ruby on Windows

Here is a follow-up to my earlier post about installing and using Ruby and Selenium on Windows.

Recording a Selenium test using Selenium IDE for FireFox.

I used the Selenium IDE add-on with FireFox to record a quick example test in Ruby. I visited the Yahoo homepage and typed a search for Selenium RC. Here is the code in Ruby

require "selenium"
require "test/unit"

class SeleniumRubyWindowsTest < Test::Unit::TestCase
def setup
@verification_errors = []
if $selenium
@selenium = $selenium
else
@selenium = Selenium::SeleniumDriver.new("localhost", 4444, "*chrome", "http://www.yahoo.com/", 10000);
@selenium.start
end
@selenium.set_context("SeleniumRubyWindowsTest")
end

def teardown
@selenium.stop unless $selenium
assert_equal [], @verification_errors
end

def test_SeleniumRubyWindowsTest
@selenium.open "/"
assert_equal "Yahoo!", @selenium.get_title
@selenium.type "p_13838465-p", "Selenium RC"
@selenium.click "search-submit"
@selenium.wait_for_page_to_load "30000"
assert_equal "Selenium RC - Yahoo! Search Results", @selenium.get_title
end
end


Save this file as SeleniumRubyWindowsTest.rb


Now that a test is ready to be run, its time to start up the Selenium server.



Running the Selenium server


If you have followed the earlier post about installing Ruby, then you open a command window and go to the directory where you copied the selenium jars. In short go to the directory where Ruby Gems are installed -

<GEM_INSTALL_DIR>\Selenium-1.1.16\lib\selenium\openqa


In this location, using the Java JRE installed in your system, start the Selenium server like this


<GEM_INSTALL_DIR>\Selenium-1.1.16\lib\selenium\openqa java –jar selenium-server.jar


See the screen shot


clip_image002


Now your Selenium Server should start running at the default port of 4444. See screen shot.



clip_image002[5]



Running your first Selenium test written in Ruby

The Selenium test has already been recorded using the Seleniun IDE add-on for FireFox - SeleniumRubyWindowsTest.rb



  • Ensure that your Selenium server is still running

  • Run this test by going to C:\Ruby and typing the following command

  • C:\Ruby>ruby SeleniumRubyWindowsTest.rb

  • Selenium RC window should launch in Internet Explorer

  • A separate FireFox window should launch and show the web pages as per the test SeleniumRubyWindowsTest.rb



Running tests in Internet Explorer on Windows XP and Windows Vista

I have tried the above test using Internet Explorer 7 on Windows XP as well as Internet Explorer 8 on Windows Vista. There were no problems in running the test on IE7 with Win XP. Offcourse you must turn off your popup blocker before running tests. Extra steps are required to run on Windows Vista, as described in the next pargraph.



Code change required to make the example test run in IE is as simple as changing the following line



@selenium = Selenium::SeleniumDriver.new("localhost", 4444, "*chrome", http://www.yahoo.com/, 10000); 

to


@selenium = Selenium::SeleniumDriver.new("localhost", 4444, "*iexplore", http://www.yahoo.com/, 10000); 


Steps to run the same test on Windows Vista and IE8

However, you need to take a few more steps to run the tests without any problems on Windows Vista and IE8.



  • Create a Desktop shortcut for running the Selenium Server.

  • Start the Selenium server by right clicking the shortcut, and then selecting Run as Administrator option

  • Once server starts running, you can use the same steps as described above for running your test from the command line.


One more tip – IE8 has a popup blocker turned on by default. Turn it off before you start running your tests.



Thats all there is to using Ruby and Selenium on your Windows machine! Happy testing :)

Installing Ruby and Selenium on Windows

Though Ruby is primarily used on *nix and Mac OS X, I wanted to run it on Windows. So I went about setting up a ruby instance on Windows with the aim of writing web application tests using Selenium.

Aim: To set up Ruby and Selenium on Windows to be able to run Selenium based tests against a web application

There is a lot of information out there, but its in bits and pieces for Windows. So here is an attempt to make this a one stop resource for setting up Ruby and Selenium on Windows.

Pre-requisites:

Since Selenium server needs to be started from the provided jar, you will need to have a JRE on your Windows machine.

Getting and installing Ruby on Windows:
  • Go to the following site http://rubyforge.org/frs/?group_id=167

  • Download the latest Ruby Installer for windows. Example - ruby186-27_rc2.exe

  • Run this file to install Ruby.

  • When installing you will get to see options that need to be installed. Ensure that you select ‘Enable RubyGems’ to be installed. See screen shot. Click Next.

clip_image002
  • Install Ruby in a top level directory like C:\Ruby as per screen shot. Click Next.

clip_image002[5]

  • Accept defaults on rest of the screens and complete the installation.

Installing the Ruby Selenium Gem on Windows
  • Go to the following site http://selenium.rubyforge.org/

  • Navigate to the Downloads section (http://selenium.rubyforge.org/download.html) or go to the latest builds here http://selenium.rubyforge.org/builds/ and download the latest version.

    Example – Selenium-1.1.16.gem

  • Save this file to the “C:\Ruby” directory where you installed Ruby for Windows

  • Now start a command prompt and go to C:\Ruby

  • Type the command C:\Ruby>gem install Selenium

  • Wait for the Selenium gem to be installed.

  • Note that Selenium should be with uppercase S and not lowercase s. See screen shot.

clip_image002[7]

Installing Selenium Remote Control for Windows

Selenium Remote Control (RC) is required so that the Selenium Jetty Server can start running and then tests written in Ruby can be run against the web application under test using Selenium Ruby Client driver.

Tests can be executed against any Windows browser like Internet Explorer and FireFox.

  • Go to the following site and download the latest Selenium RC driver http://seleniumhq.org/download/

  • Example – Selenium RC 1.0.1

  • Extract this zip file

  • Copy only the following two folders to the C:\Ruby folder. Refer screen shot.

      1. selenium-server-1.0.1

      2. selenium-ruby-client-driver-1.0.1

    clip_image002[9]

    • Now go to the folder C:\Ruby\selenium-server-1.0.1

    • Copy the selenium-server.jar file from here to the following location

      C:\Ruby\lib\ruby\gems\1.8\gems\Selenium-1.1.16\lib\selenium\openqa

    • In short locate the directory where Ruby Gems are installed <GEM_INSTALL_DIR>\Selenium-1.1.16\lib\selenium\openqa

    You are done with all installations! Now the next step is to run the Selenium RC via the Jetty Server bundled with Selenium. I will post about this later on.