Skip to content
ryansch edited this page Jan 27, 2011 · 15 revisions

Webrat has Selenium support through Webrat::Selenium (see the Webrat::Selenium RDoc).

Using Selenium through Webrat is surprisingly easy:

  sudo gem install webrat
  sudo gem install selenium-client

Configure Webrat (in your test/test_helper.rb, or in env.rb):

require "webrat"

Webrat.configure do |config|
  config.mode = :selenium
  #optional:
  config.application_port = 4567 # defaults to 3001. Avoid Selenium's default port, 4444
  config.application_framework = :sinatra  # could also be :merb. Defaults to :rails
  config.application_environment = RAILS_ENV || :test # should equal the environment of the test runner because of database and gem dependencies. Defaults to :test. 
end

Webrat::Selenium runs a Mongrel server at your specified port, in the selenium environment, so make sure you have config/environments/selenium.rb created.

Then, run your favorite Webrat integration test, something like:

class SignupTest < ActionController::IntegrationTest

    def test_trial_account_sign_up
      visit home_path
      click_link "Sign up"
      fill_in "Email", :with => "good@example.com"
      select "Free account"
      click_button "Register"
    end

  end

Using Selenium without Rails

This is what worked for me.

sudo gem install cucumber webrat Selenium
selenium

features/step_definitions/webrat_steps.rb:

include Webrat::Methods
include Webrat::Selenium::Methods
include Webrat::Selenium::Matchers

features/support/env.rb:

require 'spec' 
require "webrat/selenium"

Webrat.configure do |config|
  config.mode = :selenium
  config.selenium_server_address = 'localhost'
  config.application_framework = :external
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)

cucumber.yml:

default: features

With the 0.4.4 gem I also had to add a blank “when :external” to webrat-0.4.4/lib/webrat/selenium/application_server.rb

And features defined in features/*.features files as usual.

Clone this wiki locally