DEV Community

n350071๐Ÿ‡ฏ๐Ÿ‡ต
n350071๐Ÿ‡ฏ๐Ÿ‡ต

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"

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

Discussion (0)