DEV Community


Posted on • Updated on

capybara find.text method ignore newline (convert to a space)

🔗 Parent Note

🤔 Situation

The find method automatically convert the '\n' to a space, so test will be false.

target = 'hello\nworld'

p target
#=> "hello\\nworld"

#=> "hello world"

expect(find('').text).to eq target
#=> false 😅

🎉 Solution

There is Capybara::RSpecMatchers#have_content method.

  • ⭕️ It judges the page or the node has the text.
  • ❌ It ignores HTML tag, so if you want to test include HTML tag, you shouldn't use this.
expect(find('')).to have_content(target)

📚 Capybara::RSpecMatchers

Top comments (0)