Posted 20 days ago. Visible to the public.

Matching multiline text in Capybara 3

Until Capybara 2, node finders that accept a text option were able to find nodes based on visible text, even if it spans over multiple lines in the HTML or even different child nodes. If you relied on this behavior, affected features will fail:

Copy
<span>Hi! Try to match me.</span>
Copy
page.find(text: "Hi! Try to match me.") # => Capybara::ElementNotFound

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
# features/support/capybara.rb:15 # 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 bahavior can also be observed in Capybara's RSpec matchers like have_text.

makandra has been working exclusively with Ruby on Rails since 2007. Our laser focus on a single technology has made us a leader in this space.

Owner of this card:

Avatar
Michael Leimstädtner
Last edit:
18 days ago
by Michael Leimstädtner
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 cookies to improve usability and analyze traffic.
Accept or learn more