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
@selenium ="localhost", 4444, "*chrome", "", 10000);

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

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

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 -


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


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


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 ="localhost", 4444, "*chrome",, 10000); 


@selenium ="localhost", 4444, "*iexplore",, 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 :)

No comments:

Post a Comment