DEV Community

n350071πŸ‡―πŸ‡΅
n350071πŸ‡―πŸ‡΅

Posted on β€’ Edited on

1 1

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"

find('div.target').text
#=> "hello world"

expect(find('div.target').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('div.target')).to have_content(target)

πŸ“š Capybara::RSpecMatchers

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay