Rspec Capybara find field not functioning

Viewed 7025

I'm running Rails 4 using capybara and rspec (OS is Ubuntu 13.10). I'm having a problem -- when I run rspec my specs work, including those that use capybara's fill_in methods. However in one spec I need to use capybara's find_field method, and it is not functioning at all. It gives me the following error:

Failure/Error: page.find_field('From:').set(Date.today - 1.month)
 Capybara::ElementNotFound:
   Unable to find field "From:"

Now, I have inserted a "puts page.html" line immediately before the page.find_field... line and the html it prints includes the following lines:

<div class="date-range-picker">
<span class="from-date"><label for="from_date">From:</label> <input id="from_date"    name="from_date" type="date" value="2013-12-23" /></span>
 <span class="to-date"><label for="to_date">To:</label> <input id="to_date"  name="to_date" type="date" value="2013-12-30" /></span>
 </div>

So the element is there, but not being picked up by the find_field method. Any ideas?

4 Answers

I found this on another site and it slightly worked for me:

Add this to "support/select_from_chosen.rb":

module SelectFromChosen
  # select_from_chosen('Option', from: 'id_of_field')
  def select_from_chosen(item_text, options)
    field = find_field(options[:from], :visible => false)
    find("##{field[:id]}_chosen").click
    find("##{field[:id]}_chosen ul.chosen-results li", :text => item_text).click
  end
end

RSpec.configure do |config|
  config.include SelectFromChosen, type: :feature
end

I was able to do:

select_from_chosen('Blue', from: 'car_colors')
find('#submit-button').click
expect(page).to have_content 'Blue'
select_from_chosen('Red', from: 'car_colors')
find('#submit-button').click
expect(page).to have_content 'Red'

What I was actually trying to do was validate that the options in the chosen results where available for some users and not for others so I wound up using:

find('#car_colors_chosen').click
expect(page).to have_content 'Blue'
expect(page).not_to have_content 'Gold'

Just wanted to add this incase it helped someone else,

Related