diff --git a/History.txt b/History.txt index 303cb50..26eb924 100644 --- a/History.txt +++ b/History.txt @@ -10,6 +10,7 @@ * Minor enhancements + * 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) * Add should_see and should_not_see for verifying HTML response bodys diff --git a/lib/webrat/core/form.rb b/lib/webrat/core/form.rb index 847b159..0dbf3db 100644 --- a/lib/webrat/core/form.rb +++ b/lib/webrat/core/form.rb @@ -17,14 +17,12 @@ module Webrat nil end - def find_select_option(option_text) + def find_select_option(option_text, field_name_pattern = nil) select_fields = fields_by_type([SelectField]) - - select_fields.each do |select_field| + select_fields.select{|field| field_name_pattern.nil? || field.matches_name?(field_name_pattern) || field.matches_id?(field_name_pattern)}.each do |select_field| result = select_field.find_option(option_text) return result if result end - nil end diff --git a/lib/webrat/core/scope.rb b/lib/webrat/core/scope.rb index 99b95c6..600a7b0 100644 --- a/lib/webrat/core/scope.rb +++ b/lib/webrat/core/scope.rb @@ -12,6 +12,22 @@ module Webrat @selector = selector end + def selects_date(date_string, options = {}) + id_or_name = options[:from] + date = Date.parse date_string + + year_option = find_select_option(date.year.to_s, /#{id_or_name.to_s}.*1i/) + month_option = find_select_option(date.month.to_s, /#{id_or_name.to_s}.*2i/) + day_option = find_select_option(date.day.to_s, /#{id_or_name.to_s}.*3i/) + + flunk("Could not find date picker for #{date_string}") if year_option.nil? || month_option.nil? || day_option.nil? + year_option.choose + month_option.choose + day_option.choose + end + + alias_method :select_date, :selects_date + # Verifies an input field or textarea exists on the current page, and stores a value for # it which will be sent when the form is submitted. # diff --git a/lib/webrat/core/session.rb b/lib/webrat/core/session.rb index 04e25af..ab89e50 100644 --- a/lib/webrat/core/session.rb +++ b/lib/webrat/core/session.rb @@ -151,6 +151,7 @@ module Webrat def_delegators :current_scope, :uncheck, :unchecks def_delegators :current_scope, :choose, :chooses def_delegators :current_scope, :select, :selects + def_delegators :current_scope, :select_date, :selects_date def_delegators :current_scope, :attach_file, :attaches_file def_delegators :current_scope, :click_area, :clicks_area def_delegators :current_scope, :click_link, :clicks_link diff --git a/spec/api/selects_date_spec.rb b/spec/api/selects_date_spec.rb new file mode 100644 index 0000000..cad52ff --- /dev/null +++ b/spec/api/selects_date_spec.rb @@ -0,0 +1,111 @@ +require File.expand_path(File.dirname(__FILE__) + "/../spec_helper") + +describe "date_selects" do + before do + @session = Webrat::TestSession.new + @example_date_select = <<-EOS +
+
+ + + + +
+ EOS + end + + + it "should fail if option not found" do + @session.response_body = @example_date_select + lambda { @session.selects_date "2008-07-13"}.should raise_error + end + + it "should fail if option not found in list specified by element name" do + @session.response_body = @example_date_select + lambda { @session.selects_date "2008-07-13", :from => "created_at" }.should raise_error + end + + it "should fail if specified list not found" do + @session.response_body = <<-EOS +
+ +
+ EOS + + lambda { @session.selects_date "2003-12-01", :from => "created_at" }.should raise_error + end + + it "should send value from option" do + @session.response_body = <<-EOS +
+
+ + + +
+ + + + +
+ EOS + @session.should_receive(:post).with("/login", "created_at(1i)" => "2003", 'created_at(2i)' => '12', 'created_at(3i)' => '1', "updated_at(1i)" => "", 'updated_at(2i)' => '', 'updated_at(3i)' => '') + @session.selects_date '2003-12-01', :from => "created_at" + @session.clicks_button + end + + it "should work without specifying the field name or label" do + @session.response_body = @example_date_select + @session.should_receive(:post).with("/login", "created_at(1i)" => "2003", 'created_at(2i)' => '12', 'created_at(3i)' => '1') + @session.selects_date '2003-12-01' + @session.clicks_button + end + + it "should correctly set day and month when there are the same options available" do + @session.response_body = <<-EOS +
+
+ + + + +
+ EOS + @session.should_receive(:post).with("/login", "created_at(1i)" => "2003", 'created_at(2i)' => '12', 'created_at(3i)' => '1') + @session.selects_date '2003-12-01' + @session.clicks_button + end + +end