Visiting outside URLs in Cucumber/Webrat/Rails
Monday, March 16th, 2009One 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