Ruby on rails 3 如何使用Capybara、Rails和MiniTest规范在datepicker中填充_

Ruby on rails 3 如何使用Capybara、Rails和MiniTest规范在datepicker中填充_,ruby-on-rails-3,capybara,minitest,Ruby On Rails 3,Capybara,Minitest,我有一个form_标记,它生成以下HTML: <form accept-charset="UTF-8" action="http://www.example.com/product_page" id="dates_form" method="get"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" /><

我有一个form_标记,它生成以下HTML:

<form accept-charset="UTF-8" action="http://www.example.com/product_page" id="dates_form" method="get"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" /></div>
  Min date: <input id="datepicker_mini" name="datepicker_mini" type="text" />
  Max date: <input id="datepicker_maxi" name="datepicker_maxi" type="text" />
</form>
任何帮助都将不胜感激


谢谢。

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

## fill_in "person_birthdate, with: "21/12/1980"
page.execute_script("$('#person_birthdate').val('21/12/1980')")
我觉得这有点脏,但对我来说很管用

格查斯:

  • 我用的是Selenium drive
  • 我使用js:true运行该示例
  • 我使用了一个名为zebra的插件来生成日期选择器,但原理是一样的
编辑:这也适用于恶鬼

我喜欢这个解决方案:

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

从这里开始

上述解决方案对我不起作用。可能是因为我正在为其编写测试的应用程序上没有输入字段。我的解决方案要难看得多,但它会在一个仅为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

如果您想模拟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') }

这对我有用。重要的是,日期采用这种格式,否则它将不会被日期选择器解析。
page.execute_script%Q{$(“md datepicker[name='datepicker\u mini']input”).val('2010-1-1')。trigger('input')}

您能发布测试代码吗?添加了测试代码。如果你需要进一步的信息,请告诉我。谢谢。页面上可能有一个同名的隐藏字段吗?我本以为自己没有阅读Stackoverflow上的另一个线程,但我搜索了生成的HTML,却找不到datepicker_min的另一个实例。我已从视图中删除了所有部分和布局,以使代码尽可能基本,并对其进行了更新新生成的HTML的问题。我仍然无法弄清这一点。这很好,因为它确保了数据选择器的加载和正常工作。
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') }