Forum: RSpec Selecting a date with Webrat

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
B6adc9e099cf9c257e5e29d8be8ebe4c?d=identicon&s=25 Mike Sassak (Guest)
on 2008-11-13 19:51
(Received via mailing list)
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
C694a032be7518a0d704318895f8fe1d?d=identicon&s=25 Ben Mabey (mabes)
on 2008-11-13 20:17
(Received via mailing list)
Mike Sassak wrote:
>
> select. Is there is a better way to do this?
>
> Thanks,
> Mike
> _______________________________________________
> rspec-users mailing list
> rspec-users@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
5d38ab152e1e3e219512a9859fcd93af?d=identicon&s=25 David Chelimsky (Guest)
on 2008-11-13 20:32
(Received via mailing list)
On Thu, Nov 13, 2008 at 1:14 PM, Ben Mabey <ben@benmabey.com> wrote:
>>
>> (corresponding to, say, "When I select the date "2008-November-13"
>
> 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

Somebody should wrap that up in a plugin! I've done that before too. I
can do it in March sometime, so if any of you can do it sooner, that'd
rock :)
B6adc9e099cf9c257e5e29d8be8ebe4c?d=identicon&s=25 Mike Sassak (Guest)
on 2008-11-13 20:44
(Received via mailing list)
It sure does. Thanks!
0be0e4aa42aacd9a8a95c792de273ca7?d=identicon&s=25 aslak hellesoy (Guest)
on 2008-11-13 22:38
(Received via mailing list)
On Thu, Nov 13, 2008 at 8:20 PM, David Chelimsky <dchelimsky@gmail.com>
wrote:
>>> What I'd like to be able to write is:
>>> The first problem could be gotten around by writing a new webrat step
>>>
>> end
>
> Somebody should wrap that up in a plugin! I've done that before too. I
> can do it in March sometime, so if any of you can do it sooner, that'd
> rock :)
>

Or better yet - add it to webrat proper and ask Bryan to pull it.
(It's rails specific, so it should only be in the rails part of
webrat).

Aslak
C694a032be7518a0d704318895f8fe1d?d=identicon&s=25 Ben Mabey (mabes)
on 2008-11-14 05:50
(Received via mailing list)
aslak hellesoy wrote:
>>>> to do that by specifying only the label (in this case "Date"), rather
>>>>
>>>> rspec-users mailing list
>>>  selects time.day, :from => "#{id_prefix}_3i"
> Or better yet - add it to webrat proper and ask Bryan to pull it.
> (It's rails specific, so it should only be in the rails part of
> webrat).
>
> Aslak
>
>

Good idea, guys!  And yes, I can take a hint. :)  Here is what I was
able to get done tonight:

http://github.com/bmabey/webrat/commit/80d3eba59e3...

There are still some things I need to address and test more but the
functionality is mostly there.

I'll report back if/when Bryan merges it into webrat and I'll create a
step for it in cucumber's webrat_steps.rb.  You can also follow the
progress on webrat's lighthouse:
http://webrat.lighthouseapp.com/projects/10503-web...

-Ben
This topic is locked and can not be replied to.