diff --git a/.gitignore b/.gitignore index f07c0b3..1e26df0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ coverage +.DS_Store pkg doc ri -email.txt \ No newline at end of file +email.txt diff --git a/History.txt b/History.txt index 70f95ac..b952507 100644 --- a/History.txt +++ b/History.txt @@ -10,7 +10,8 @@ * Add support for file fields via #attaches_file method (Kyle Hargraves) * Minor enhancements - + + * Add Webrat.root method for cross-framework support (Krzysztof Zylawy) * Add #select_date for quickly filling out Rails-style date fields (Alex Lang) * Support selecting options by their values (Alex Lang) * Support for clicking areas of an image map (Alex Lang) diff --git a/lib/webrat.rb b/lib/webrat.rb index 76acd40..af0c4dc 100644 --- a/lib/webrat.rb +++ b/lib/webrat.rb @@ -1,5 +1,8 @@ module Webrat - VERSION = '0.2.1' + VERSION = '0.2.2' + def self.root + defined?(RAILS_ROOT) ? RAILS_ROOT : Merb.root + end end require "rubygems" diff --git a/lib/webrat/core/field.rb b/lib/webrat/core/field.rb index 05cec00..42710cc 100644 --- a/lib/webrat/core/field.rb +++ b/lib/webrat/core/field.rb @@ -1,3 +1,5 @@ +require "cgi" + module Webrat class Field @@ -6,15 +8,14 @@ module Webrat if %w[submit image].include?(element["type"]) field_class = "button" else - field_class = element["type"] || "text" + field_class = element["type"] || "text" #default type; 'type' attribute is not mandatory end else field_class = element.name end - Webrat.const_get("#{field_class.capitalize}Field") rescue NameError - raise "Invalid field element: #{element.inspect}" + raise "Invalid field element: #{element.inspect}" end def initialize(form, element) @@ -120,10 +121,10 @@ module Webrat def param_parser if defined?(CGIMethods) CGIMethods - else - require "action_controller" - require "action_controller/integration" + elsif defined?(ActionController::AbstractRequest) ActionController::AbstractRequest + else + Webrat::ParamParser #used for Merb end end @@ -149,10 +150,6 @@ module Webrat def matches_text?(text) @element.innerHTML =~ /#{Regexp.escape(text.to_s)}/i end - - # def matches_id?(id) - # @element["id"] =~ /^\W*#{Regexp.escape(id.to_s)}/i - # end def matches_value?(value) @element["value"] =~ /^\W*#{Regexp.escape(value.to_s)}/i || matches_text?(value) || matches_alt?(value) @@ -183,7 +180,7 @@ module Webrat else checkbox_with_same_name = @form.find_field(name, CheckboxField) - if checkbox_with_same_name.to_param.nil? + if checkbox_with_same_name.to_param.blank? super else nil diff --git a/lib/webrat/core/form.rb b/lib/webrat/core/form.rb index 0dbf3db..de36093 100644 --- a/lib/webrat/core/form.rb +++ b/lib/webrat/core/form.rb @@ -103,7 +103,7 @@ module Webrat def merge_hash_values(a, b) # :nodoc: a.keys.each do |k| if b.has_key?(k) - case [a[k], b[k]].map(&:class) + case [a[k], b[k]].map{|value| value.class} when [Hash, Hash] a[k] = merge_hash_values(a[k], b[k]) b.delete(k) diff --git a/lib/webrat/core/logging.rb b/lib/webrat/core/logging.rb index 0d1a5fd..6634e96 100644 --- a/lib/webrat/core/logging.rb +++ b/lib/webrat/core/logging.rb @@ -3,7 +3,7 @@ module Webrat def debug_log(message) # :nodoc: return unless logger - logger.debug(message) + logger.debug message end def logger # :nodoc: diff --git a/lib/webrat/merb.rb b/lib/webrat/merb.rb index 992c0ba..9d1dc8d 100644 --- a/lib/webrat/merb.rb +++ b/lib/webrat/merb.rb @@ -1,3 +1,7 @@ +Dir[File.join(File.dirname(__FILE__), "merb", "*.rb")].sort.each do |file| + require File.expand_path(file) +end + module Webrat class Session include Merb::Test::RequestHelper @@ -27,13 +31,13 @@ module Webrat def response_code @response.status end - + protected def do_request(url, data, headers, method) - @response = request(url, :params => data, :headers => headers, :method => method) + @response = request(url, :params => (data && data.any?) ? data : nil, :headers => headers, :method => method) self.get(@response.headers['Location'], nil, @response.headers) if @response.status == 302 end - + end end diff --git a/lib/webrat/merb/param_parser.rb b/lib/webrat/merb/param_parser.rb index 534e1ff..0ee2e5f 100644 --- a/lib/webrat/merb/param_parser.rb +++ b/lib/webrat/merb/param_parser.rb @@ -1,3 +1,5 @@ +require "cgi" + module Webrat class ParamParser def self.parse_query_parameters(query_string) diff --git a/lib/webrat/merb/url_encoded_pair_parser.rb b/lib/webrat/merb/url_encoded_pair_parser.rb index 8e668c7..63c74f0 100644 --- a/lib/webrat/merb/url_encoded_pair_parser.rb +++ b/lib/webrat/merb/url_encoded_pair_parser.rb @@ -1,3 +1,6 @@ +require "cgi" +require "strscan" + class UrlEncodedPairParser < StringScanner #:nodoc: attr_reader :top, :parent, :result diff --git a/spec/api/fills_in_spec.rb b/spec/api/fills_in_spec.rb index 99682f6..226eeb7 100644 --- a/spec/api/fills_in_spec.rb +++ b/spec/api/fills_in_spec.rb @@ -144,6 +144,18 @@ describe "fills_in" do @session.fills_in "user[email]", :with => "foo@example.com" @session.clicks_button end + + it "should work if the input type is not set" do + @session.response_body = <<-EOS +
+ + +
+ EOS + @session.should_receive(:post).with("/login", "user" => {"email" => "foo@example.com"}) + @session.fills_in "user[email]", :with => "foo@example.com" + @session.clicks_button + end it "should work with symbols" do @session.response_body = <<-EOS diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index feb04ac..015d1e8 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -16,8 +16,6 @@ else require File.join(File.dirname(__FILE__), "webrat", "#{ENV["TEST_MODE"]}", "helper.rb") end - - Spec::Runner.configure do |config| # Nothing to configure yet end \ No newline at end of file diff --git a/spec/webrat/merb/indifferent_access_spec.rb b/spec/webrat/merb/indifferent_access_spec.rb new file mode 100644 index 0000000..9a92c6d --- /dev/null +++ b/spec/webrat/merb/indifferent_access_spec.rb @@ -0,0 +1,48 @@ +require File.expand_path(File.dirname(__FILE__) + "/../../spec_helper") +require File.expand_path(File.dirname(__FILE__) + "/helper") + +describe HashWithIndifferentAccess do + it "should not update constructor when not a hash" do + HashWithIndifferentAccess.should_receive(:update).never + HashWithIndifferentAccess.new('test') + end + + it "should get the default for key" do + h = HashWithIndifferentAccess.new(:test => 'me') + h.should_receive(:super).never + + h.default(:test).should == 'me' + end + + context "a hash with a test value applied" do + + setup do + @h = HashWithIndifferentAccess.new + @h[:test] = 'me' + end + + it "should assign a new value" do + @h[:test].should == 'me' + end + + it "should return true if asked for existing key" do + @h.key?(:test).should be_true + end + + it "should return array of values for keys" do + @h.values_at(:test).should == ['me'] + end + + it "should merge with another hash" do + another = HashWithIndifferentAccess.new(:value => 'test') + @h.merge(another).values_at(:test, :value).should == ['me','test'] + end + + it "should delete the key" do + @h.delete(:test) + @h.any?.should be_false + @h[:test].should be_nil + end + + end +end \ No newline at end of file diff --git a/spec/webrat/merb/session_spec.rb b/spec/webrat/merb/session_spec.rb new file mode 100644 index 0000000..237413d --- /dev/null +++ b/spec/webrat/merb/session_spec.rb @@ -0,0 +1,43 @@ +require File.expand_path(File.dirname(__FILE__) + "/../../spec_helper") +require File.expand_path(File.dirname(__FILE__) + "/helper") + +describe Webrat::Session do + + it "should not pass empty params if data is and empty hash" do + session = Webrat::Session.new + response = OpenStruct.new + response.status = 200 + session.should_receive(:request).with('url', {:params=> nil, :method=>"GET", :headers=>nil}).and_return(response) + session.get('url', {}, nil) + end + + %w{post put delete}.each do |request_method| + it "should call do request with method #{request_method.upcase} for a #{request_method} call" do + session = Webrat::Session.new + response = OpenStruct.new + response.status = 200 + + session.should_receive(:request).with('url', {:params=>nil, :method=>request_method.upcase, :headers=>nil}).and_return(response) + session.send(request_method, 'url', {}, nil) + end + end + + context "a session with a response" do + + setup do + @session = Webrat::Session.new + @response = OpenStruct.new + @response.status = 200 + @response.body = 'test response' + @session.instance_variable_set(:@response, @response) + end + + it "should return body of a request as a response_body" do + @session.response_body.should == @response.body + end + + it "should return status of a request as a response_code" do + @session.response_code.should == @response.status + end + end +end \ No newline at end of file