Posts Tagged ‘webrat’

Visiting outside URLs in Cucumber/Webrat/Rails

Monday, March 16th, 2009

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

Webrat error on form submit

Thursday, February 26th, 2009

I was getting a bizarre error from Webrat:

    And I press "Next"                                                     # features/step_definitions/webrat_steps.rb:10
      You have a nil object when you didn't expect it!
      The error occurred while evaluating nil.submit (NoMethodError)
      (eval):2:in `click_button'
      ./features/step_definitions/webrat_steps.rb:12:in `/^I press "(.*)"$/'
      features/choosing_varieties.feature:39:in `And I press "Next"'

It turns out, my HTML tags were not nested properly… I had a <table><form></table></form> kind of situation.  If you’re getting an error like this, try validating your HTML.