How to fill_in datepicker using Capybara, Rails, MiniTest spec

问题描述 投票:0回答:7
ruby-on-rails-3 capybara minitest
7个回答
28
投票

我有同样的问题。在阅读了水豚的文档后,我的解决方案是使用 page.execute_script() 直接为输入设置一个值。

## fill_in "person_birthdate, with: "21/12/1980"
page.execute_script("$('#person_birthdate').val('21/12/1980')")

我觉得这有点脏,但对我有用。

陷阱:

  • 我使用了 Selenium 驱动器。
  • 我用 js 运行示例:true
  • 我用了一个叫做zebra的插件来生成日期选择器,但是原理是一样的

编辑:这也适用于 Poltergeist


9
投票

我喜欢这个解决方案:

page.execute_script("$('#show_sale_date').datepicker('setDate', '01/01/2010')")

取自这里http://api.jqueryui.com/datepicker/#method-setDate


7
投票

如果你想模拟 UI 点击,这对我有用(使用 javascript 驱动程序)。

当我的日期选择器的 ID 是“target_date”时,导航到下个月,然后选择 15 号。

page.execute_script %Q{ $('#target_date').trigger('focus') }
page.execute_script %Q{ $('a.ui-datepicker-next').trigger('click') }
page.execute_script %Q{ $('a.ui-state-default:contains("15")').trigger('click') }

1
投票

这对我有用。日期采用该格式很重要,否则日期选择器不会解析它。

page.execute_script %Q{ $("md-datepicker[name='datepicker_mini'] input").val('2010-1-1').trigger('input') }


0
投票

上述解决方案对我不起作用。也许是因为我正在为其编写测试的应用程序上没有输入字段。我的解决方案更丑陋,但它会选择日期并在仅 GUI 的日期选择器中验证选择。

##selects 'Custom' option, then specified dates, and verifies that specified dates are displayed.
Then(/^select "Custom" values, "([^"]*)" to "([^"]*)" and verify selection$/) do |arg1,arg2|
  step %{select 'Custom' from timespan drop-down}
  start = Date.strptime("#{arg1}",'%m/%d/%Y')
  start_day = start.day
  start_month = start.strftime('%b')
  start_year = start.year
  stop = Date.strptime("#{arg2}",'%m/%d/%Y')
  stop_day = stop.day
  stop_month = stop.strftime('%b')
  stop_year = stop.year
  within('.left.hasDatepicker') do
    find('.ui-datepicker-year').click
    find('option', :text => start_year).click
    find('.ui-datepicker-month').click
    find('option', :text => start_month).click
    find('.ui-state-default', :text => start_day, :match => :prefer_exact).click
  end
  within('.right.customdatepicker') do
    find('.ui-datepicker-year').click
    find('option', :text => stop_year).click
    find('.ui-datepicker-month').click
    find('option', :text => stop_month).click
    find('.ui-state-default', :text => stop_day, :match => :prefer_exact).click
  end
  find('input[value="Go"]').click
  date_picker = find('.timespanperiod').text
  start_date, end_date = date_picker.split(" - ")
  start_formatted = Date.strptime("#{start_date}",'%d/%m/%Y')
  end_formatted = Date.strptime("#{end_date}",'%d/%m/%Y')
  start_formatted.should eq (Date.strptime("#{arg1}",'%m/%d/%Y'))
  end_formatted.should eq (Date.strptime("#{arg2}",'%m/%d/%Y'))
end

0
投票

find(".active.day").click
为我工作后添加
fill_in

within("#dates_form") do
  fill_in "datepicker_mini", :with => "01/01/2010"
  find(".active.day").click
  fill_in "datepicker_maxi", :with => "01/01/2020"
  find(".active.day").click
end

0
投票

我将 Minitest 与 Capybara 一起使用,并且能够执行以下操作:

fill_in "Starts at", with: "2023-04-22T10:00:00"

当您使用浏览器的

Inspect
工具时,格式与自动显示为字段值的内容相匹配。

© www.soinside.com 2019 - 2024. All rights reserved.