How to select a date in a Rails date_field form helper using Capybara
Asked Answered
V

4

8

I have a reservation search form that contains the following date_field helper:

<%= date_field(:reservation, :arrival_date) %>

How do I select a specific date for this date_field tag using Capybara?

I tried select "2014-01-01", from: "reservation_arrival_date" in my spec but I get a Capybara::ElementNotFound: error when running the spec.

Thanks in advance!

Vouge answered 18/2, 2014 at 9:56 Comment(2)
Have you looked at this? - <#6730286>Judicatory
@user880828 Yes I did and I tried the answers, the date_field tag doesn't involve i1, i2, and i3 segments.Vouge
V
2

Instead of finding a way to click and select a date in the date_field tag, I instead did the following:

page.find('#reservation_arrival_date').set("2014-01-01")

This works really well and I find this approach very simple. But I'm not sure how "awkward" this may seem for an integration test.

Vouge answered 18/2, 2014 at 17:43 Comment(0)
K
10

As stated in documentation, date_field will not yield a <select> tag but an <input type='date'> this is why you cannot use Capybara::Node::Actions#select.

Instead just treat it as a regular input field:

fill_in "reservation_arrival_date", with: "2014/01/01"
Kessler answered 23/1, 2015 at 13:45 Comment(1)
I had to write the date as "2014-01-01" for it to work.Potentilla
P
3

In 2020:

fill_in :reservation_arrival_date, with: '2020-01-01'
Poliard answered 24/9, 2020 at 9:14 Comment(0)
V
2

Instead of finding a way to click and select a date in the date_field tag, I instead did the following:

page.find('#reservation_arrival_date').set("2014-01-01")

This works really well and I find this approach very simple. But I'm not sure how "awkward" this may seem for an integration test.

Vouge answered 18/2, 2014 at 17:43 Comment(0)
S
-1

In 2023:

fill_in "Reservation arrival date", with: Date.today
Soldiery answered 4/10, 2023 at 12:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.