How to select date from a select box using Capybara in Rails 3?

后端 未结 11 1515
梦如初夏
梦如初夏 2020-12-05 10:27

I\'m writing a spec for a controller in Rails 3 project using RSpec and Capybara, and I want to select current date from a select box. I tried:

select Date.t         


        
11条回答
  •  时光说笑
    2020-12-05 11:16

    with credit to Markus Hartmair for an excellent solution, I prefer to use labels as selectors because of improved readability. So my version of his helper module is:

    module SelectDateHelper
      def select_date(date, options = {})
        field = options[:from]
        base_id = find(:xpath, ".//label[contains(.,'#{field}')]")[:for]
        year, month, day = date.split(',')
        select year,  :from => "#{base_id}_1i"
        select month, :from => "#{base_id}_2i"
        select day,   :from => "#{base_id}_3i"
      end
    end
    

    call it like this:

    select_date "2012,Jan,1", :from => "From date"
    

提交回复
热议问题