22

How I can check html content on the page with capybara?

page.should have_text('this is <b>bold</b> "text"')

output:

Failure/Error: page.should have_text('this is <b>bold</b> "text"')
  expected there to be this is <b>bold</b> \"text\" in "....this is bold \"text\"....."
ole
  • 5,166
  • 5
  • 29
  • 57

1 Answers1

29

Your code doesn't work as Capybara's has_text? (and thus have_text) method checks text, not html source of the element it is invoked on.

If you use driver that supports Javascript evaluation (e.g. Selenium, Poltergeist or Capybara-Webkit) you can get inner HTML of the element using Javascript:

html = page.evaluate_script("document.getElementById('answer-15988092').innerHTML")
html.should include('this is <b>bold</b> "text"')

If you want to assert that the entire HTML of the page contains an element you can use html method that returns page source:

page.html.should include('this is <b>bold</b> "text"')

Usually I prefer javascript evaluation as it's more restrictive.

Andrei Botalov
  • 20,686
  • 11
  • 89
  • 123
  • I have tried a number of valid elements up to and including `id`, `div#id`, and `body`. In each case, I get this rejection `unknown error: Cannot read property innerHTML of null` – TangibleDream Apr 22 '14 at 19:38