Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capybara: should have html content

Tags:

rspec

capybara

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\"....."
like image 204
ole Avatar asked Apr 12 '13 22:04

ole


1 Answers

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.

like image 69
Andrei Botalov Avatar answered Nov 05 '22 08:11

Andrei Botalov