Date selectors for Capybara November 04th, 2010 by Rick Salsa, 1 Comment

If you’ve used webrat with cucumber-rails, you’ll know that you have access to steps like:

[ruby gutter=”false”]
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
[/ruby]

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:

[ruby gutter=”false”]
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
[/ruby]

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.

 

ches Says:

November 27, 2010 @ 3:40 pm

`Date.parse` should work just fine instead of adding a Chronic dependency, unless you’re getting really fancy about the way you’re expressing dates in writing your features. As you can see there’s a lot of duplication that can be factored out too — here’s something I came up with after seeing your post and some inspiration from others (in the fork history of this gist):nnhttps://gist.github.com/966d9623fa08cb177c8c

Post your comment

You must be logged in to post a comment.