Skip to content

Instantly share code, notes, and snippets.

@cee-dub
Created August 21, 2009 00:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cee-dub/171570 to your computer and use it in GitHub Desktop.
Save cee-dub/171570 to your computer and use it in GitHub Desktop.
require 'www/mechanize'
# Patches WWW::Mechanize for Cucumber + Webrat
module WWW
class Mechanize
def self.log_disabled
@@log ||= Logger.new(STDOUT)
end
class File
attr_accessor :redirects
def redirect?
redirects > 0
end
def response_code
code.to_i
end
def error?
(500..599).include?(response_code)
end
def success?
(200..299).include?(response_code)
end
def unauthorized?
response_code == 401
end
end
# Mechanize throws exceptions for certain HTTP status codes.
# We want to catch these and still create a page.
def fetch_page_with_secret_sauce(params)
begin
page = fetch_page_without_secret_sauce(params)
rescue ResponseCodeError => e
page = e.page
end
page.redirects ||= params[:redirects].to_i
page
end
alias_method :fetch_page_without_secret_sauce, :fetch_page
alias_method :fetch_page, :fetch_page_with_secret_sauce
end
end
require 'webrat'
require 'action_controller'
require 'test/unit'
Webrat.configure do |config|
config.mode = :mechanize
end
World do
Webrat::Session.new(Webrat::MechanizeAdapter.new)
end
World(Webrat::Matchers)
World(Test::Unit::Assertions)
World(ActionController::TestCase::Assertions)
World(ActionController::Assertions::ResponseAssertions)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment