0

I have a page that injects some text into the DOM: 'Success!'.

The Javascript code works because I see the expected text in the screenshot, and the spec passes:

page.visit '/'

save_and_open_screenshot

expect( page).to have_content 'Success!'

puts page.html

However, the page.html is not updated. It does not have the injected text.

How do I get the HTML for the updated DOM?

EDIT: I found that the issue is caused by an iframe. The iframe is not added to the page.html, but it is added to the page.

EDIT #2: It turns out that the 'Success!' content is not in the iframe. So maybe the context is switching to the iframe.

B Seven
  • 44,484
  • 66
  • 240
  • 385

2 Answers2

1

Found one workaround which is OK:

html = page.evaluate_script( 'document.documentElement.innerHTML' )

I guess one could use JS or jQuery finder to find the expected <div>.

B Seven
  • 44,484
  • 66
  • 240
  • 385
1

For the entire page body you can do this:

page.body

For any element in particular

page.find(".my-div").base.inner_html

Check out the full API here: https://github.com/thoughtbot/capybara-webkit/blob/master/lib/capybara/webkit/node.rb

Peter P.
  • 3,221
  • 2
  • 25
  • 31
  • I think I tried this. I don't think it works when JS updates the DOM after the page loads. – B Seven Jan 17 '16 at 16:51
  • It works with the things I'm doing with lots of dynamic js dom updates. There may be something else going on. I've dug through the Webkit source and I can see it invokes a getInnerHTML call to the Webkit server, which delegates to capybara.js call of this.getNodes[index].innerHTML. So as long as the node is there in the dom, it should work. – Peter P. Jan 17 '16 at 20:43
  • Also, another tip: For some reason in our app, we had automatic_reload = false. When we recently upgraded to the latest(1.4.x->1.7.x) we had all sorts of weird issues that were ultimately solved by removing this setting, which defaults to true I believe. This setting affects whether you can have unattached nodes in the dom. – Peter P. Jan 17 '16 at 20:48