Thursday, July 22, 2010

The troll takes on selenium 2.0

EDIT: On checking the selenium-webdriver gem, it seems to include everything needed to drive the browsers. So step 2-4 is not really needed. But still, below is exactly how I did this yesterday.

Not that I really work with testing anymore, it still interest me. Well at least getting it more and more automated.
So getting Selenium 2.0 running on my windows laptop sounded like a good summer activity.

In fairness the people responsible for both Selenium 2.0 and selenium-webdriver (which is the ruby frontend, packaged as a ruby gem) did a fantastic job.

You need following installed to get it working:
Firefox (or else you can change the example code to use IE or Chrome etc.)
Ruby 1.9
(Selenium 2.0)
And a few more stuff detailed below.
  1. I installed first ruby 1.8.7 and the ruby 1.9.1 from the one-click installers.
  2. Then I went to the SeleniumHQ and specifically they're Webdriver page.
  3. Downloaded the complete package (selenium-server-2.0a5.zip) from the downloads page.
  4. Unpacked it and added that directory to the CLASSPATH environment variable (I used C:\selenium-2.0a5).
  5. Fired up a cmd prompt and installed the selenium-webdriver gem (gem install selenium-webdriver).
  6. Navigated to the ruby bindings webpage for the WebDriver API => http://code.google.com/p/selenium/wiki/RubyBindings
  7. Fired up gvim (feel free to use your favorite editor here!)
  8. Copy and pasted they're first example and it failed horrible. (Note that on some systems you will need 'require "rubygems"', best is to always use it). Save as google_test.rb
  9. Googled the problem "msvcrt-ruby18.dll selenium-webdriver", found a related watir problem => http://code.google.com/p/selenium/issues/detail?id=583
  10. Installed the devkit from rubyinstaller.org, devkit is available for download on this page => http://rubyinstaller.org/downloads/
  11. unpacked the contents into my ruby 1.9 directory (I use C:\Ruby191)
  12. fired up the cmd again and ran
    gem uninstall win32-api
    gem install win32-api --platform=ruby
    as detailed in the watir fix
  13. Then "ruby google_test.rb" in the cmd and everything worked as intended!

2 comments:

  1. Hey -
    This is great information. I'm having problems with my spec to continue through the webdriver methods. It currently opens a Firefox 5.0 browser window, but it does not proceed to the webdriver.get method. Have you seen this problem before.

    Here is what my code looks like

    require 'rubygems'
    require 'rspec'
    require 'selenium-webdriver'

    driver = Selenium::WebDriver.for :firefox
    driver.get "http://google.com"

    Any help would be appreciated.

    Thanks!

    ReplyDelete
  2. Here is the example that I used. Been a few months since I did this, well a year or so. Notice that I do not use the "get" but the "navigate" method.

    Not sure if they have changed webdriver since then. Anyway here it is.

    ----
    require "rspec"
    require "selenium-webdriver"

    describe "test" do

    before(:all) do
    @driver = Selenium::WebDriver.for :chrome
    end
    it "bla bla" do
    @driver.navigate.to "http://google.com"

    element = @driver.find_element(:name, 'q')
    element.send_keys "Hello WebDriver!"
    element.submit

    puts @driver.title[0..4].should eq("Hello")
    end
    after(:all) do
    @driver.quit
    end
    end
    ---

    ReplyDelete