Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 411
Joeman29 Avatar asked Dec 30 '13 18:12

Joeman29


2 Answers

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??

like image 75
Joeman29 Avatar answered Nov 05 '22 15:11

Joeman29


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.

like image 26
Paulo Fidalgo Avatar answered Nov 05 '22 16:11

Paulo Fidalgo