Opinionated Programmer - Jo Liss's musings on enlightened software development.

<input> tag in Capybara: find_field.value is nil vs. ""

This just in:

Given an <input id="foo" name="..." type="text" /> tag (without a value attribute), find_field('foo').value in Capybara will return nil with the (default) Rack::Test driver, but "" with the Selenium driver.

The reason is that when Selenium opens the page, Firefox automatically adds an empty value="" attribute to the DOM (you can easily check this by calling save_and_open_page in your test), and Selenium rightly reports the value in the DOM, not in the original source. Rack::Test on the other hand reports nil, because that’s what the HTML source says. (It’s perfectly valid HTML to omit the value attribute, in case you were wondering.)

To make your tests pass in both Selenium and Rack::Test, test for find_field('foo').value.blank? (this works for both nil and ""), or write (find_field('foo').value || '') if you’re testing for equality with pre-calculated variables.