[rspec-users] Selecting a date with Webrat

Ben Mabey ben at benmabey.com
Thu Nov 13 14:14:52 EST 2008


Mike Sassak wrote:
> Hi,
>
> I'm writing a scenario that needs to select date values from a form
> created with Rails' form_for() method, and I'm looking for a clean way
> to do that by specifying only the label (in this case "Date"), rather
> than by selecting from each select list one by one.
>
> What I'd like to be able to write is:
>
> When I select "2008-November-13" from "Date"
>
> (or something similar), but this fails because the Date label doesn't
> really apply to the individual select_lists. If I write instead
>
> When I select "2008" from "id_from_year_select_1i"
>
> it works, but then I not only need to do that once for each select,
> but I need to specify the id, which could change if my model changes.
> The first problem could be gotten around by writing a new webrat step
> (corresponding to, say, "When I select the date "2008-November-13"
> from "Date") but I would still need to specify the ids from each
> select. Is there is a better way to do this?
>
> Thanks,
> Mike
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>   

Hey Mike,
What I have done in the past is used the following helper in my stories:

def selects_time(id_prefix, time)
  selects time.year, :from => "#{id_prefix}_1i"
  selects time.strftime('%B'), :from => "#{id_prefix}_2i" # month name,
  selects time.day, :from => "#{id_prefix}_3i"
  selects time.hour.to_s.rjust(2,'0'), :from => "#{id_prefix}_4i"
  selects time.min.to_s.rjust(2,'0'), :from => "#{id_prefix}_5i"
end

I would use Time.parse to convert the string from the scenario to feed 
it into the helper.  In my particular case the id prefix could be 
inferred easily from th  the step, but your case might be different.  
That should hopefully get you started though.

-Ben


More information about the rspec-users mailing list