Visiting outside URLs in Cucumber/Webrat/Rails

One of the Rails projects I’m working on can create subdomains.  In order to test this in Cucumber, I want to be able to write scenarios that look like this:

When I visit http://blingo.myapp.com
I should see "Welcome to blingo!"

Unfortunately, if you’re using Cucumber to test a Rails application, the visit method basically strips off the entire domain part of any urls you try to visit. The solution I found to this problem is to create a separate Mechanize session and use that for the outside urls. Here’s my updated visit step definition:

require "webrat/mechanize"
 
Given /^I visit (.*)$/ do |url|
  if url.match(/^http:\/\//)
    session = Webrat::MechanizeSession.new
    session.visit(url)
  else 
    visit url
  end
end

Tags: , , , ,

One Response to “Visiting outside URLs in Cucumber/Webrat/Rails”

  1. Ian Fleeton Says:

    Thanks for this.

    Calling
    header(’Host’, ‘mylogin.yourhostedapp.com’)
    might also work for you

    I am using it at the moment since it seems to work has no dependency on Mechanize.

    Webrat uses http://www.example.com by default but checks the Host header which overrides it.

Leave a Reply