From adf68c2f8fc47053319199b90b922093efd69f0c Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Sat, 22 Nov 2008 23:49:12 -0500 Subject: [PATCH] Change Webrat Rails integration to use the Webrat::Methods module --- lib/webrat/core/configuration.rb | 4 ++ lib/webrat/core/methods.rb | 9 +++- lib/webrat/core/session.rb | 22 +++++++++- lib/webrat/mechanize.rb | 21 ++++------ lib/webrat/merb.rb | 2 + lib/webrat/rack.rb | 4 +- lib/webrat/rails.rb | 41 ++++++------------- lib/webrat/selenium.rb | 1 + lib/webrat/sinatra.rb | 4 +- spec/webrat/core/configuration_spec.rb | 8 ++-- .../mechanize/mechanize_session_spec.rb | 6 +-- spec/webrat/rails/rails_session_spec.rb | 6 --- 12 files changed, 70 insertions(+), 58 deletions(-) diff --git a/lib/webrat/core/configuration.rb b/lib/webrat/core/configuration.rb index 52c118f..73bc7dc 100755 --- a/lib/webrat/core/configuration.rb +++ b/lib/webrat/core/configuration.rb @@ -12,10 +12,14 @@ module Webrat end class Configuration + + attr_accessor :mode + # Sets whether to save and open pages with error status codes in a browser attr_accessor :open_error_files def initialize + self.mode = :rails self.open_error_files = true end diff --git a/lib/webrat/core/methods.rb b/lib/webrat/core/methods.rb index 43f7c44..04253c7 100644 --- a/lib/webrat/core/methods.rb +++ b/lib/webrat/core/methods.rb @@ -5,7 +5,7 @@ module Webrat meths.each do |meth| self.class_eval <<-RUBY def #{meth}(*args, &blk) - @_webrat_session ||= ::Webrat::MerbSession.new + @_webrat_session ||= ::Webrat.session_class.new(self) @_webrat_session.#{meth}(*args, &blk) end RUBY @@ -38,7 +38,12 @@ module Webrat :clicks_button, :click_button, :reload, :reloads, :clicks_link_within, :click_link_within, - :field_labeled + :field_labeled, + :set_hidden_field, :submit_form, + :request_page, :current_dom, + :selects_date, :selects_time, :selects_datetime, + :select_date, :select_time, :select_datetime + end end \ No newline at end of file diff --git a/lib/webrat/core/session.rb b/lib/webrat/core/session.rb index dac1149..92c33fd 100644 --- a/lib/webrat/core/session.rb +++ b/lib/webrat/core/session.rb @@ -7,17 +7,37 @@ module Webrat class PageLoadError < WebratError end + def self.session_class + case Webrat.configuration.mode + when :rails + RailsSession + when :merb + MerbSession + when :selenium + SeleniumSession + when :rack + RackSession + when :sinatra + SinatraSession + when :mechanize + MechanizeSession + else + raise WebratError.new("Unknown Webrat mode: #{Webrat.configuration.mode.inspect}") + end + end + class Session extend Forwardable include Logging attr_reader :current_url - def initialize #:nodoc: + def initialize(context = nil) #:nodoc: @http_method = :get @data = {} @default_headers = {} @custom_headers = {} + @context = context end # Saves the page out to RAILS_ROOT/tmp/ and opens it in the default diff --git a/lib/webrat/mechanize.rb b/lib/webrat/mechanize.rb index a1d65f2..bdd9a40 100644 --- a/lib/webrat/mechanize.rb +++ b/lib/webrat/mechanize.rb @@ -6,13 +6,8 @@ module Webrat attr_accessor :response alias :page :response - def initialize(mechanize = WWW::Mechanize.new) - super() - @mechanize = mechanize - end - def get(url, data, headers_argument_not_used = nil) - @response = @mechanize.get(url, data) + @response = mechanize.get(url, data) end def post(url, data, headers_argument_not_used = nil) @@ -25,11 +20,7 @@ module Webrat end memo end - @response = @mechanize.post(url, post_data) - end - - def response - @response + @response = mechanize.post(url, post_data) end def response_body @@ -39,8 +30,14 @@ module Webrat def response_code @response.code.to_i end + + def mechanize + @mechanize = WWW::Mechanize.new + end - def_delegators :@mechanize, :basic_auth + def_delegators :mechanize, :basic_auth end end + +Webrat.configuration.mode = :mechanize \ No newline at end of file diff --git a/lib/webrat/merb.rb b/lib/webrat/merb.rb index e81b014..e128439 100644 --- a/lib/webrat/merb.rb +++ b/lib/webrat/merb.rb @@ -73,3 +73,5 @@ class Merb::Test::RspecStory #:nodoc: end end +Webrat.configuration.mode = :merb + diff --git a/lib/webrat/rack.rb b/lib/webrat/rack.rb index 931b1b9..6f7cf81 100644 --- a/lib/webrat/rack.rb +++ b/lib/webrat/rack.rb @@ -21,4 +21,6 @@ module Webrat @response.status end end -end \ No newline at end of file +end + +Webrat.configuration.mode = :rack \ No newline at end of file diff --git a/lib/webrat/rails.rb b/lib/webrat/rails.rb index f95d75c..19d9008 100644 --- a/lib/webrat/rails.rb +++ b/lib/webrat/rails.rb @@ -1,10 +1,5 @@ module Webrat class RailsSession < Session #:nodoc: - - def initialize(integration_session) - super() - @integration_session = integration_session - end def doc_root File.expand_path(File.join(RAILS_ROOT, 'public')) @@ -40,9 +35,13 @@ module Webrat protected + def integration_session + @context + end + def do_request(http_method, url, data, headers) #:nodoc: update_protocol(url) - @integration_session.request_via_redirect(http_method, remove_protocol(url), data, headers) + integration_session.request_via_redirect(http_method, remove_protocol(url), data, headers) end def remove_protocol(href) #:nodoc: @@ -55,14 +54,14 @@ module Webrat def update_protocol(href) #:nodoc: if href =~ /^https:/ - @integration_session.https!(true) + integration_session.https!(true) elsif href =~ /^http:/ - @integration_session.https!(false) + integration_session.https!(false) end end def response #:nodoc: - @integration_session.response + integration_session.response end end @@ -71,30 +70,14 @@ end module ActionController module Integration class Session #:nodoc: - unless instance_methods.include?("put_via_redirect") require "webrat/rails/redirect_actions" include Webrat::RedirectActions end - def respond_to?(name) - super || webrat_session.respond_to?(name) - end - - def method_missing(name, *args, &block) - if webrat_session.respond_to?(name) - webrat_session.send(name, *args, &block) - else - super - end - end - - protected - - def webrat_session - @webrat_session ||= Webrat::RailsSession.new(self) - end - + include Webrat::Methods end end -end \ No newline at end of file +end + +Webrat.configuration.mode = :rails \ No newline at end of file diff --git a/lib/webrat/selenium.rb b/lib/webrat/selenium.rb index 2cec5f5..5343b43 100644 --- a/lib/webrat/selenium.rb +++ b/lib/webrat/selenium.rb @@ -2,3 +2,4 @@ gem "selenium-client", ">=1.2.9" require "selenium/client" require "webrat/selenium/selenium_session" +Webrat.configuration.mode = :selenium \ No newline at end of file diff --git a/lib/webrat/sinatra.rb b/lib/webrat/sinatra.rb index 310868e..400cc29 100644 --- a/lib/webrat/sinatra.rb +++ b/lib/webrat/sinatra.rb @@ -16,4 +16,6 @@ module Webrat end end -end \ No newline at end of file +end + +Webrat.configuration.mode = :sinatra \ No newline at end of file diff --git a/spec/webrat/core/configuration_spec.rb b/spec/webrat/core/configuration_spec.rb index 1120278..8734e92 100755 --- a/spec/webrat/core/configuration_spec.rb +++ b/spec/webrat/core/configuration_spec.rb @@ -9,11 +9,12 @@ describe Webrat::Configuration do Webrat.reset_for_test end - it "should have a default config" do - Webrat.configuration.should be_an_instance_of(Webrat::Configuration) + it "should default to Rails mode" do + config = Webrat.configuration + config.mode.should == :rails end - it "should set default values" do + it "should open error files by default" do config = Webrat.configuration config.open_error_files.should == true end @@ -22,6 +23,7 @@ describe Webrat::Configuration do Webrat.configure do |config| config.open_error_files = false end + config = Webrat.configuration config.open_error_files.should == false end diff --git a/spec/webrat/mechanize/mechanize_session_spec.rb b/spec/webrat/mechanize/mechanize_session_spec.rb index 452d3b7..81d234e 100644 --- a/spec/webrat/mechanize/mechanize_session_spec.rb +++ b/spec/webrat/mechanize/mechanize_session_spec.rb @@ -27,10 +27,10 @@ describe Webrat::MechanizeSession do end it "should flatten model post data" do - mechanize = mock :mechanize + mechanize = mock(:mechanize) + WWW::Mechanize.stub!(:new => mechanize) mechanize.should_receive(:post).with(url, flattened_data) - - Webrat::MechanizeSession.new(mechanize).post(url, data) + Webrat::MechanizeSession.new.post(url, data) end end end \ No newline at end of file diff --git a/spec/webrat/rails/rails_session_spec.rb b/spec/webrat/rails/rails_session_spec.rb index 4729d9e..4e00802 100644 --- a/spec/webrat/rails/rails_session_spec.rb +++ b/spec/webrat/rails/rails_session_spec.rb @@ -1,12 +1,6 @@ require File.expand_path(File.dirname(__FILE__) + '/helper') describe Webrat::RailsSession do - it "should require a Rails Integration session to be initialized" do - lambda { - Webrat::RailsSession.new - }.should raise_error - end - it "should delegate response_body to the session response body" do response = mock("response", :body => "") integration_session = mock("integration session", :response => response)