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 :)

No comments:

Post a Comment