cucumber and capybara, how to open external url or visit outside url

Bilal Basharat picture Bilal Basharat · Nov 30, 2011 · Viewed 13.7k times · Source

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!

Answer

Marek Př&#237;hoda picture Marek Příhoda · Nov 30, 2011

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