Add #select_date for quickly filling out Rails-style date fields (Alex Lang)

This commit is contained in:
Bryan Helmkamp 2008-10-21 22:44:39 -04:00
parent 1b51de8f0f
commit 9671c4256c
5 changed files with 131 additions and 4 deletions

View File

@ -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

View File

@ -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

View File

@ -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.
#

View File

@ -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

View File

@ -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
<form method="post" action="/login">
<label for="created_at">Created at</label><br />
<select id="created_at_1i" name="created_at(1i)">
<option value="2002">2002</option>
<option value="2003">2003</option>
</select>
<select id="created_at_2i" name="created_at(2i)">
<option value="11">November</option>
<option value="12">December</option>
</select>
<select id="created_at_3i" name="created_at(3i)">
<option value="1">1</option>
<option value="2">2</option>
</select>
<input type="button" value="submit"/>
</form>
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
<form method="get" action="/login">
<select name="month"><option value="1">January</option></select>
</form>
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
<form method="post" action="/login">
<label for="updated_at">Created at</label><br />
<select id="updated_at_1i" name="updated_at(1i)">
<option value=""></option>
<option value="2003">2003</option>
</select>
<select id="updated_at_2i" name="updated_at(2i)">
<option value=""></option>
<option value="12">December</option>
</select>
<select id="updated_at_3i" name="updated_at(3i)">
<option value=""></option>
<option value="1">1</option>
</select>
<label for="created_at">Created at</label><br />
<select id="created_at_1i" name="created_at(1i)">
<option value="2003">2003</option>
</select>
<select id="created_at_2i" name="created_at(2i)">
<option value="12">December</option>
</select>
<select id="created_at_3i" name="created_at(3i)">
<option value="1">1</option>
</select>
<input type="button" value="submit"/>
</form>
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
<form method="post" action="/login">
<label for="created_at">Created at</label><br />
<select id="created_at_1i" name="created_at(1i)">
<option value="2003">2003</option>
</select>
<select id="created_at_2i" name="created_at(2i)">
<option value="1">January</option>
<option value="12">December</option>
</select>
<select id="created_at_3i" name="created_at(3i)">
<option value="1">1</option>
<option value="12">12</option>
</select>
<input type="button" value="submit"/>
</form>
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