Created
March 21, 2009 11:39
-
-
Save oelmekki/82813 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'spec' | |
require 'rubygems' | |
require 'webrat' | |
require "#{File.dirname(__FILE__)}/logger" | |
# default object is the session object | |
def method_missing(name, *args, &block) | |
if webrat_session.respond_to?(name) | |
webrat_session.send(name, *args, &block) | |
else | |
super | |
end | |
end | |
Webrat.configure do |config| | |
config.mode = :mechanize | |
end | |
module Webrat | |
# add a custom logger | |
module Logging | |
def logger | |
Logger.new | |
end | |
end | |
# resolve weird rewriting | |
class Link | |
def absolute_href | |
if href =~ /^\?/ | |
"#{@session.current_url}#{href}" | |
elsif href !~ %r{^https?://} && (href !~ /^\//) | |
"#{APP_URL}/#{href}" | |
else | |
href | |
end | |
end | |
end | |
# prevent webrat to reinitialize the session, by using ||= instead of = | |
class MechanizeSession < Session | |
def mechanize | |
@mechanize ||= WWW::Mechanize.new | |
end | |
end | |
end | |
# in order to be able to do in step definitions : | |
# webrat_session.page.should have_selector( css3_selector ) | |
module WWW | |
class Mechanize | |
class Page | |
def has_selector?( selector ) | |
not search( selector ).empty? | |
end | |
end | |
end | |
end | |
def webrat_session | |
@webrat_session ||= Webrat::MechanizeSession.new | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment