public
Rubygem
Description: BDD that talks to domain experts first and code second
Home | Edit | New

Setting up Selenium

Update: This guide is out of date, a fix is upcoming but in the meantime you do not need the Selenium gem. Also, you must use selenium-client -v=1.2.14. You do not need to overwrite any jars. Make sure all selenium processes are stopped and the correct versions are installed and it should run. The information about multiple environments for selenium/non-selenium tests is still valid and useful.

Pre-requisites

Selenium-RC requires the Java Runtime Environment (JRE) version 1.5.0 or higher

Step 1: Install the Selenium gem

sudo gem install Selenium

Step 2: Install Selenium-remote

Unfortunately the jar that the Selenium gem (version 1.1.14) comes with does not work with Firefox 3. So we have to get the latest jar from the Selenium-RC site:

Download the Selenium RC Beta 2:

http://seleniumhq.org/download/

Once you have extracted the download we copy:

selenium-remote-control-1.0-beta-2/selenium-server-1.0-beta-2/selenium-server.jar

Over the jar that was installed with the Selenium gem:

GEM_INSTALL_DIR/Selenium-1.1.14/lib/selenium/openqa/selenium-server.jar.txt

Alternate Solution

You can also patch the binary manually if the above solution does not work for you.

http://www.spacevatican.org/2008/9/27/selenium-and-firefox-3

A pre-patched binary can be found linked in the comments.

Some versions of webrat also include a selenium server so you may need to patch that as well:

GEM_INSTALL_DIR/webrat-0.4.1/vendor/selenium-server.jar

Step 3: Ubuntu fix (skip if you’re not using Ubuntu)

There is a bug with Selenium and Firefox 3 where Selenium cannot find: firefox-bin

To solve this we create a sym link:
sudo ln -s /usr/lib/firefox-3.0.4/firefox /usr/bin/firefox-bin

Step 4: Starting Selenium

The Selenium gem installs a script which allows us to get Selenium-RC running
selenium
OR you can run the jar directly
java -jar selenium-server.jar.txt

Step 5: Run the cucumber example

Run the Cucumber Selenium example

cucumber examples/selenium/features/

Useful links

Official Selenium-RC site

Selenium gem at rubyforge

Cucumber / Selenium / Webrat

Many apps have features that don’t require javascript to work, and running them all in selenium makes things slow. Since webrat steps support selenium out of the box, you can reuse almost all of your standard step definitions for selenium as well. Here’s a recipe for how you can:

  • Run your non-selenium features with transactional fixtures and standard webrat steps
  • Run your entire suite (html and javascript) features with selenium, without transactional fixtures
  • Reuse all (or almost all) or your step definitions between selenium and non-selenium runners

NOTE – when using this setup, you do not need to start selenium manually. Webrat will spawn a new selenium process and a mongrel process for you.

First, setup a directory structure like the following:

features/
|-- enhanced
|   `-- ajaxy.feature
|-- plain
|   `-- non_ajaxy.feature
|-- step_definitions
|   `-- webrat_steps.rb
`-- support
    |-- enhanced.rb
    |-- env.rb
    `-- plain.rb

Create a cucumber.yml file in the root directory of your project (yes, this is cluttered). In it, create these 2 profiles:

default: -r features/support/env.rb -r features/support/plain.rb -r features/step_definitions features/plain
selenium: -r features/support/env.rb -r features/support/enhanced.rb -r features/step_definitions features/enhanced features/plain

In env.rb, turn on all webrat features:

ENV["RAILS_ENV"] = "test" 
require File.expand_path(File.dirname(__FILE__) + '/../../config/environment')
require 'cucumber/rails/world'
require 'cucumber/formatter/unicode'
require 'webrat/rails'
require 'cucumber/rails/rspec'

In enhanced.rb, set up the selenium-specific files:

Webrat.configure do |config|
  config.mode = :selenium
  # Selenium defaults to using the selenium environment. Use the following to override this.
  # config.application_environment = :test
end

# this is necessary to have webrat "wait_for" the response body to be available
# when writing steps that match against the response body returned by selenium
World(Webrat::Selenium::Matchers)

Before do
  # truncate your tables here, since you can't use transactional fixtures*
end

* For truncating your DB take a look at DatabaseCleaner

In plain.rb, set up the standard webrat features:

# truncate your tables here if you are using the same database as selenium, since selenium doesn't use transactional fixtures
Cucumber::Rails.use_transactional_fixtures

To run your regular features, you can execute:

cucumber

To run your selenium features, you can execute:

cucumber -p selenium

Rails 2.2+ and Sessions

When running Selenium with Rails 2.3.2 (and this appears to be true for 2.2 as well) sessions did not work. They were being saved, but then were lost upon reload. Adding the following to the selenium environment file fixed this problem:

config.action_controller.session = { :session_http_only => false }
Last edited by rxcfc, Thu Jul 02 11:22:41 -0700 2009
Home | Edit | New
Versions: