简体   繁体   English

如何使用 Capybara 在 Rails date_field 表单助手中选择日期

[英]How to select a date in a Rails date_field form helper using Capybara

I have a reservation search form that contains the following date_field helper:我有一个预订搜索表单,其中包含以下 date_field 助手:

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

How do I select a specific date for this date_field tag using Capybara?如何使用 Capybara 为此 date_field 标签选择特定日期?

I tried select "2014-01-01", from: "reservation_arrival_date" in my spec but I get a Capybara::ElementNotFound: error when running the spec.我尝试在规范中select "2014-01-01", from: "reservation_arrival_date" ,但在运行规范时出现Capybara::ElementNotFound:错误。

Thanks in advance!提前致谢!

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 .正如文档中所述date_field不会产生<select>标签,而是产生<input type='date'>这就是为什么你不能使用Capybara::Node::Actions#select

Instead just treat it as a regular input field:而是将其视为常规输入字段:

fill_in "reservation_arrival_date", with: "2014/01/01"

Instead of finding a way to click and select a date in the date_field tag, I instead did the following:我没有找到在 date_field 标记中单击并选择日期的方法,而是执行以下操作:

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.但是我不确定对于集成测试来说这看起来有多“尴尬”。

2020年:

fill_in :reservation_arrival_date, with: '2020-01-01'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM