Skip to content

Instantly share code, notes, and snippets.

@oelmekki
Created March 21, 2009 11:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oelmekki/82813 to your computer and use it in GitHub Desktop.
Save oelmekki/82813 to your computer and use it in GitHub Desktop.
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