diff --git a/lib/webrat/core.rb b/lib/webrat/core.rb index b7aaf4f..53c33e6 100644 --- a/lib/webrat/core.rb +++ b/lib/webrat/core.rb @@ -7,3 +7,4 @@ require "webrat/core/area" require "webrat/core/label" require "webrat/core/select_option" require "webrat/core/session" +require "webrat/core/methods" diff --git a/lib/webrat/core/methods.rb b/lib/webrat/core/methods.rb new file mode 100644 index 0000000..264aeea --- /dev/null +++ b/lib/webrat/core/methods.rb @@ -0,0 +1,35 @@ +module Webrat + module Methods + + def self.delegate_to_session(*meths) + meths.each do |meth| + self.class_eval <<-RUBY + def #{meth}(*args, &blk) + with_session do |sess| + sess.#{meth}(*args, &blk) + end + end + RUBY + end + end + + def with_session + @session ||= ::Webrat::Session.new + yield @session + @session.response + end + + # all of these methods delegate to the @session, which should + # be created transparently. + delegate_to_session :visits, :within, :clicks_link_within, + :reload, :header, :http_accept, :basic_auth, + :save_and_open_page, :fill_in, :check, + :uncheck, :choose, :select, :attach_file, + :click_area, :click_link, :click_button, + :field_labeled + + alias reloads reload + alias visit visits + + end +end \ No newline at end of file