How to select date from a select box using Capybar

2019-01-14 06:51发布

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.today, :from => 'Date of birth'

but the spec fails and I get error:

Failure/Error: select Date.today, :from => 'Date of birth' NoMethodError: undefined method `to_xpath' for Mon, 18 Jul 2011:Date

How to fix it?

P.S. In view file I use simple_form_for tag and the select box is generated by code:

f.input :date_of_birth

11条回答
姐就是有狂的资本
2楼-- · 2019-01-14 06:58

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')
查看更多
▲ chillily
3楼-- · 2019-01-14 07:02

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.

查看更多
Explosion°爆炸
4楼-- · 2019-01-14 07:04

You need to specify the exact value as it's in the select menu in html. So if your select has values like "2011/01/01" then you need to write:

select '2011/01/01', :from => 'Date of birth'

Your code fails because you pass a date object.

查看更多
一纸荒年 Trace。
5楼-- · 2019-01-14 07:10

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
查看更多
相关推荐>>
6楼-- · 2019-01-14 07:15

I found a clean solution for rspec and capybara to test using date and time select methods, where in your HTML you use a datetime select or date select. This works with Rails 4, RSpec 3.1 and Capybara 2.4.4.

Say in your HTML form you have the following:

<%= f.datetime_select(:start_date, {default: DateTime.now, prompt: {day: 'Choose day', month: "Choose month", year: "Choose year"}}, {class: "date-select"}) %>

the DateTime Select View helper will create 5 select fields with ids such as id="modelname_start_date_1i", where the id the is appended with 1i, 2i, 3i, 4i, 5i. By default Year, Month, Day, Hour, Minute. If you change the order of the fields, make sure to change the feature helper below.

1) Create a Feature Helper for dates and times helpers

spec/support/helpers/date_time_select_helpers.rb

module Features
  module DateTimeSelectHelpers

    def select_date_and_time(date, options = {})
      field = options[:from]
      select date.strftime('%Y'),  :from => "#{field}_1i" #year
      select date.strftime('%B'),  :from => "#{field}_2i" #month
      select date.strftime('%-d'), :from => "#{field}_3i" #day 
      select date.strftime('%H'),  :from => "#{field}_4i" #hour
      select date.strftime('%M'),  :from => "#{field}_5i" #minute
    end

    def select_date(date, options = {})
      field = options[:from]
      select date.strftime('%Y'),  :from => "#{field}_1i" #year
      select date.strftime('%B'),  :from => "#{field}_2i" #month
      select date.strftime('%-d'), :from => "#{field}_3i" #day 
    end
  end
end 

Note that for the day I use %-d that gives you a non-padded numeric value (i.e. 4) instead of %d that has a zero-padded numeric value (i.e. 04). Check the date formats with strftime

2) You then need to include your date and time helpers methods in spec/support/helpers.rb so you can use them in any spec file.

require 'support/helpers/date_time_select_helpers'
RSpec.configure do |config|
  config.include Features::DateTimeSelectHelpers, type: :feature
end

3) In your Spec file you can call your helper. For example:

feature 'New Post' do
  scenario 'Add a post' do
    visit new_post_path
    fill_in "post[name]", with: "My post"
    select_date_and_time(2.days.from_now, from:"post_start_date")
    click_button "Submit"
    expect(page).to have_content "Your post was successfully saved"
  end
end
查看更多
时光不老,我们不散
7楼-- · 2019-01-14 07:17

Had the same problem. I googled at lot and solved it this way:

  1. Wrote date select macros into /spec/request_macros.rb The select_by_id method is necessary for me, because the month is dependent on the translation

    module RequestMacros
      def select_by_id(id, options = {})
        field = options[:from]
        option_xpath = "//*[@id='#{field}']/option[#{id}]"
        option_text = find(:xpath, option_xpath).text
        select option_text, :from => field
      end
    
      def select_date(date, options = {})
        field = options[:from]
        select date.year.to_s,   :from => "#{field}_1i"
        select_by_id date.month, :from => "#{field}_2i"
        select date.day.to_s,    :from => "#{field}_3i"  
      end
    end
    
  2. Added them to my /spec/spec_helper.rb

    config.include RequestMacros, :type => :request
    

Now in my integration tests in spec/requests i can use

select_date attr[:birthday], :from => "user_birthday"

Thanks to http://jasonneylon.wordpress.com/2011/02/16/selecting-from-a-dropdown-generically-with-capybara/ and https://gist.github.com/558786 :)

查看更多
登录 后发表回答