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

后端 未结 11 1475
梦如初夏
梦如初夏 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 10:52

    Given the following Formtastic code renders Rails default date selector:

    = f.input :born_on, end_year: Time.now.year, start_year: 60.years.ago.year
    

    In your spec, break the date into separate calls to each individual select tag:

    select '1956', from: 'person_born_on_1i'
    select 'July', from: 'person_born_on_2i'
    select '9', from: 'person_born_on_3i'
    

    I don't like that this code is so aware of the HTML, but it does work with the versions of gems at this time.

    Gems:

    • Capybara 2.1.0
    • Formtastic 2.2.1
    • Rails 3.2.13
    • RSpec 2.13.0
    0 讨论(0)
  • 2020-12-05 11:00

    It looks like this one has been sufficiently covered, but see Capybara's docs for an official answer. You can select by name, id, or label text.

    0 讨论(0)
  • 2020-12-05 11:01

    A slight adaption of Markus's answer:

    def select_date(date, options = {})  
      raise ArgumentError, 'from is a required option' if options[:from].blank?
      field = options[:from].to_s
      select date.year.to_s,               :from => "#{field}_1i"
      select Date::MONTHNAMES[date.month], :from => "#{field}_2i"
      select date.day.to_s,                :from => "#{field}_3i"
    end
    
    0 讨论(0)
  • 2020-12-05 11:07

    In my particular situation, I'm adding potentially multiple date select fields to the page with accepts_nested_attributes_for functionality. This means, I'm not sure what the full id or name of the fields are going to be.

    Here's the solution I came up with in case it helps anyone else Googling this:

    I'm wrapping the date select field in a container div with a class:

    <div class='date-of-birth-container'>
      <%= f.date_select :date_of_birth %>
    </div>
    

    Then in my feature spec:

    within '.date-of-birth-container' do
      find("option[value='1']", text: 'January').select_option
      find("option[value='1']", text: '1').select_option
      find("option[value='1955']").select_option
    end
    

    Here's a helper method I wrote for it:

    def select_date_within_css_selector(date, css_selector)
      month_name = Date::MONTHNAMES.fetch(date.month)
      within css_selector do
        find("option[value='#{date.month}']", text: month_name).select_option
        find("option[value='#{date.day}']", text: date.day.to_s).select_option
        find("option[value='#{date.year}']").select_option
      end
    end
    

    Then using the helper:

    select_date_within_css_selector(Date.new(1955, 1, 1), '.date-of-birth-container')
    
    0 讨论(0)
  • 2020-12-05 11:09

    Thanks to Dylan for pointing it out, but in case anyone is looking for the cucumber version, you can use this:

    select_date("Date of birth", :with => "1/1/2011")
    

    For more information, see select_date.

    0 讨论(0)
  • 2020-12-05 11:09

    For Rails 4, in case somebody gets to this question without being limited to Rails 3.

      select '2020',  from: 'field_name_{}_1i'
      select 'January',  from: 'field_name_{}_2i'
      select '1', from: 'field_name_{}_3i'
    

    You can of course extract this to a helper and make it dynamic.

    0 讨论(0)
提交回复
热议问题