22

i am using cucumber and capybara. in a rails 3.0.9 platform. i am getting this test case fail: log is:

(::) failed steps (::)

No route matches "/wiki/Baltimore_Ravens" (ActionController::RoutingError)
<internal:prelude>:10:in `synchronize'
./features/step_definitions/web_steps.rb:20:in `/^(?:|I )am on (.+)$/'
features/annotate.feature:7:in `Given I am on a web page'

Failing Scenarios:
cucumber features/annotate.feature:11 # Scenario: launch annotation/ logged in

6 scenarios (1 failed, 5 skipped)
63 steps (1 failed, 62 skipped)

the file web_steps: got this piece of code:

19 Given /^(?:|I )am on (.+)$/ do |page_name|
20   visit path_to(page_name)
21 end

the file annotate.feature got this code:

7 Given I am on a web page

"a web page" is defined in support/paths.rb as:

when /a web page/
  'http://en.wikipedia.org/wiki/Baltimore_Ravens'

obviously this is an external url. i want to open it and capybara and cucumber wont allow me to do it. so, help me find a way to open outside url in cucumber test case!

Bilal Basharat
  • 3,066
  • 6
  • 21
  • 20

1 Answers1

26

Capybara uses RackTest as the default driver, and this driver doesn't allow to visit external urls (i.e. test remote applications).

If you want to visit external urls (to test, e.g, that your app redirects correctly), you have basically two options:

1/ Use another driver like e.g selenium:

before do
  Capybara.current_driver = :selenium
end

Then, in code, you can call the url like so:

visit 'http://en.wikipedia.org/wiki/Baltimore_Ravens'

Or, if you set the default app_host like so:

Capybara.app_host = 'http://en.wikipedia.org'
Capybara.run_server = false # don't start Rack

Then you can call the url:

visit '/wiki/Baltimore_Ravens'

You can configure the driver and app host in your spec_helper.rb to enable them globally across all you specs:

Capybara.configure do |config|
  config.current_driver = :selenium
  config.run_server = false
  config.app_host   = 'http://en.wikipedia.org'
end

2/ Use capybara-mechanize

Marek Příhoda
  • 11,108
  • 3
  • 39
  • 53
  • i just add the gem of selenium to my gemfile and this is resolved! thanx alot – Bilal Basharat Dec 01 '11 at 18:04
  • Hi Marek, thanks for pointing at the right direction. Just one small update, if we would configure the driver under `Capybara.configure` module it is `config.default_driver` in the capybara (3.36.0). – aluk.e Mar 11 '22 at 01:52
  • Thanks! My default capybara selenium is tied to existing route. Using this, my "visit" is free to go anywhere I wanted. – Yakob Ubaidi Dec 20 '22 at 14:04