I am using Cucumber with Capybara to build my testing framework for a site. I have found a way using hooks to switch between the Capybara driver in Cucumber. I want to be able just to use RackTest when I am not testing for JavaScript because I was lead to believe RackTest is faster since it doesn’t render the page. Right now I have create 3 identical test that jump to Google and check that the title says Google. Each test uses a different driver. The Selenium and WebKit test work fine but the RackTest fails on the “I should see the title “Google”” test. I have attached a copy of my feature and steps and testing gems to this post.
Feature:
Feature: Rack Test
In order to run test faster
As a tester
I want to use Rack Test to preform all non-javascript related test
Scenario: Visit a page and check title
When I go to "http://www.google.com"
Then I should see the title "Google"
@firefox
Scenario: Visit a page and check title in Firefox
When I go to "http://www.google.com"
Then I should see the title "Google"
@webkit
Scenario: Visit a page and check title in WebKit
When I go to "http://www.google.com"
Then I should see the title "Google"
Steps:
When /^I go to "(http:\/\/[^"]*)"$/ do |url|
visit url
end
Then /^I should see the title "([^"]*)"$/ do |title_cont|
page.should have_selector "title", text: title_cont
end
Hooks:
Before('@firefox') do
Capybara.current_driver = :selenium
end
Before('@webkit') do
Capybara.current_driver = :webkit
end
After('@firefox, @webkit') do
Capybara.use_default_driver
end
Gemfile:
group :test do
gem 'capybara'
gem 'capybara-webkit'
gem 'cucumber'
gem 'cucumber-rails'
gem 'database_cleaner'
gem 'factory_girl_rails'
gem "rack-test", require: "rack/test"
gem 'rspec'
gem 'selenium-webdriver'
end
That’s because
rack-testcan only test Rack applications (e.g. your Ruby On Rails website). It’s not browser-based solution, while both Selenium and WebKit are. That’s whyrack-testcan’t access external websites (Google in your case).