Switching Webrat to Selenium mode

Ryan Bates did a great job covering Webrat in Railscast #156, and I’d recommend anyone who hasn’t used Webrat check it out. It’s a clear demonstration of how you can start using Webrat in a few minutes today with Rails built-in integration testing framework. Webrat works with RSpec, Cucumber, Shoulda, etc. too, but they aren’t required.

Unfortunately, during the screencast some Webrat bugs prevented running the tests in Selenium mode. Webrat has worked with Selenium for some time, but there have been some lingering rough edges around getting it up and running that I’ve been meaning to resolve. Today I’m happy to report those issues are now fixed, and getting started using Selenium with Webrat should now be drop-dead simple.

Give it a try by checking out the full Episode #156 source code, then make the following changes:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# config/environments/test.rb
config.gem "webrat", :version => ">=0.4.4" # was 0.4.3

# test/test_helper.rb
class ActiveSupport::TestCase
  # ...
  self.use_transactional_fixtures = false # was true

  # ...

  # Add these lines:
  setup do |session|
    session.host! "localhost:3001"
  end
end
  
Webrat.configure do |config|
  config.mode = :selenium # was :rails
end

Running the test again should pop open a Firefox window for the running tests and print output like this:

$ rake test:integration
(in /Users/bhelmkamp/p/railscasts-episodes/episode-156/authenticator)
Loaded suite /Library/Ruby/Gems/1.8/gems/rake-0.8.4/lib/rake/rake_test_loader
Started
==> Waiting for Selenium RC server on port 4444... Ready!
==> Waiting for rails application server on port 3001... Ready!
..
Finished in 11.679288 seconds.

2 tests, 2 assertions, 0 failures, 0 errors

Success!

Note: I just pushed Webrat 0.4.4, so it might not be available on all the gem sever mirrors yet. If not, you can install it with the following commands:

git clone git://github.com/brynary/webrat.git
cd webrat
rake install_gem

7 Responses to “Switching Webrat to Selenium mode”

  1. # Ben Mabey Says:

    FYI for everyone following Bryan’s instructions… By turning transactional_fixtures off you will need to clean your database manually with cucumber’s After or Before hooks. (i.e truncate your DB.) I have a gem that makes this even easier. Just add this to your env.rb:

    require 'database_cleaner'
    require 'database_cleaner/cucumber'
    DatabaseCleaner.strategy = :truncation
    

    You can get the gem and learn more here.

  2. # Victor Moroz Says:

    Logging in 0.4.4 is great! If I knew it two hours ago…

    It is worth mentioning for us, poor linux fellows who use Firefox 3 (location of binary may vary for other dists, mine is Gentoo):

    Webrat.configure do |config| ... config.selenium_browser_key = '*firefox3 /opt/firefox/firefox-bin' end
  3. # SayYesToTest Says:

    I can’t seem to figure out why use_transactional_fixtures needs to be set to false.

  4. # Zubin Says:

    Thanks Bryan, I was stuck on this just the other day!

  5. # Doug Says:

    @SayYesToTest: if transactional fixtures are on, any changes to the database will be inside a transaction and invisible to the mongrel that is serving the pages.

  6. # Doug Says:

    @SayYesToTest: if transactional fixtures are on, any changes to the database will be inside a transaction and invisible to the mongrel that is serving the pages.

  7. # My maps Says:

    Blog added to my favorites! :-)