Date selectors for Capybara November 04th, 2010 by Rick Salsa, View Comments

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.

Share:
  • Twitter
  • Suggest to Techmeme via Twitter
  • Facebook
  • Posterous
  • del.icio.us
  • Digg
  • StumbleUpon
  • Technorati
  • LinkedIn
  • Identi.ca
  • Google Bookmarks
  • blogmarks
 
  • http://chesmart.in ches

    `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

blog comments powered by Disqus