Posted about 1 year ago. Visible to the public. Repeats.

Heads up: Capybara 3's text matchers no longer squish whitespace by default

Until Capybara 2, node finders that accept a text option were able to find nodes based on rendered text, even if it spans over multiple elements in the HTML. Imagine a page that includes this HTML:

Copy
<div class='haystack'> Hi! <br> Try to match me. </div>

Even though the text is separated by a <br> tag in the HTML, it is matched until Capybara 2 which used to "squish" text prior to the comparison.

Copy
# Capyabara 1 or 2 page.find('.haystack', text: "Hi! Try to match me.") => #<Capybara::Node::Element tag="div" path=...

If you rely on this behavior in Capybara 3, affected features will fail. The gem no longer normalizes whitespace when finding elements based on their text.

Copy
# Capybara 3 page.find('.haystack', text: "Hi! Try to match me.") # => Capybara::ElementNotFound: Unable to find visible css ".haystack" with text "Hi! Try to match me."

Re-enabling Cabyara 2 behavior in Capybara 3

After digging in the documentation, you might figure out that you can switch to painfully slow RegExp text matching or add the normalize_ws: true option to every call of a Capybara finder.
I suggest that you rather configure it globally, which also affects gem dependencies like Spreewald.

Copy
# (Add this to features/support/capybara.rb) # Capbybara 3 does no longer match DOM Elements with text spanning over # multiple lines. This configuration re-enables this behavior. Capybara.default_normalize_ws = true

Please note that the changed behavior can also be observed in Capybara's RSpec matchers like have_text.

Does your version of Ruby on Rails still receive security updates?
Rails LTS provides security patches for old versions of Ruby on Rails (3.2 and 2.3).

Owner of this card:

Avatar
Michael Leimstädtner
Last edit:
12 months ago
by Henning Koch
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Michael Leimstädtner to makandra dev
This website uses short-lived cookies to improve usability.
Accept or learn more