tag in Capybara: find_field.value is nil vs. ""
阿新 • • 發佈:2018-12-29
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=""
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
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.