Joeman29
Joeman29

Reputation: 722

Rspec Capybara find field not functioning

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?

Upvotes: 6

Views: 7465

Answers (4)

Anissa
Anissa

Reputation: 21

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,

Upvotes: 0

Joeman29
Joeman29

Reputation: 722

OK, after much meandering through Capybara's source files I found the problem. It seems that the #find_field method doesn't work properly when using Capybara-webkit. The method only failed on examples that had the js: true argument, so that should have been the first clue. Anyway it seems that the cause of this is some method naming conflict between capybara and capybara-webkit, but I didn't analyze it too closely and so I can't be sure.

I changed the find_field('from_date') to find('#from_date') and everything works now. It also worked when changing to the :rack_test driver, but since I need webkit that's what I'll stick too. Is this issue documented anywhere??

Upvotes: 9

Peter Alfvin
Peter Alfvin

Reputation: 29439

I placed your HTML fragment within an HTML page and was able to successfully do a find_field('From:') on it, which suggests the problem lies within the containing HTML. My "idea" would be to strip down the page until the find_field succeeds as a means of isolating the offending content.

Upvotes: 0

Paulo Fidalgo
Paulo Fidalgo

Reputation: 22331

As for capybara documentation:

"Find a form field on the page. The field can be found by its name, id or label text."

so use this code instead:

page.find_field('form_date').set(Date.today - 1.month)

So you are selecting the field by it's id.

Upvotes: 1

Related Questions