If you’ve used webrat with cucumber-rails, you’ll know that you have access to steps like:
When I select "[time]" as the date and time When I select "[datetime]" as the "[datetime_label]" date and time When I select "[time]" as the time When I select "[time]" as the "[time_label]" time When I select "[date]" as the date When I select "[date]" as the "[date_label]" date
Cucumber doesn’t have these steps available for Capybara (** as far as I can see) at this point. Some quick googling led me to creating the following step_definitions file:
When /^(?:|I )select "([^\"]*)" as the date$/ do |date|
date = Chronic.parse(date)
prefix = "date"
select date.year.to_s, :from => "#{prefix}_#{dt_suffix[:year]}"
select date.strftime('%B'), :from => "#{prefix}_#{dt_suffix[:month]}"
select date.day.to_s, :from => "#{prefix}_#{dt_suffix[:day]}"
end
When /^(?:|I )select "([^\"]*)" as the "([^\"]*)" date$/ do |date, prefix|
date = Chronic.parse(date)
select date.year.to_s, :from => "#{prefix}_#{dt_suffix[:year]}"
select date.strftime('%B'), :from => "#{prefix}_#{dt_suffix[:month]}"
select date.day.to_s, :from => "#{prefix}_#{dt_suffix[:day]}"
end
When /^(?:|I )select "([^\"]*)" as the date and time$/ do |date|
date = Chronic.parse(date)
prefix = "date"
select date.year.to_s, :from => "#{prefix}_#{dt_suffix[:year]}"
select date.strftime('%B'), :from => "#{prefix}_#{dt_suffix[:month]}"
select date.day.to_s, :from => "#{prefix}_#{dt_suffix[:day]}"
select date.hour.to_s, :from => "#{prefix}_#{dt_suffix[:hour]}"
select date.min.to_s, :from => "#{prefix}_#{dt_suffix[:minute]}"
end
When /^(?:|I )select "([^\"]*)" as the "([^\"]*)" date and time$/ do |date, prefix|
date = Chronic.parse(date)
select date.year.to_s, :from => "#{prefix}_#{dt_suffix[:year]}"
select date.strftime('%B'), :from => "#{prefix}_#{dt_suffix[:month]}"
select date.day.to_s, :from => "#{prefix}_#{dt_suffix[:day]}"
select date.hour.to_s, :from => "#{prefix}_#{dt_suffix[:hour]}"
select date.min.to_s, :from => "#{prefix}_#{dt_suffix[:minute]}"
end
def dt_suffix
{
:year => '1i',
:month => '2i',
:day => '3i',
:hour => '4i',
:minute => '5i'
}
end
So now you can test the rails date-time select helpers with the above. I’m sure there is probably a better way, but this works and was easy to implement. You’ll need to add the Chronic gem to your test group in Bundler for it to work.
It was inspired by jdfren’s code on github.
