Skip to content

Instantly share code, notes, and snippets.

@jxson
Created February 1, 2012 02:26
Show Gist options
  • Select an option

  • Save jxson/1714672 to your computer and use it in GitHub Desktop.

Select an option

Save jxson/1714672 to your computer and use it in GitHub Desktop.
Using cucumber, capybara, and net/http to check dynamically injected content
Then /^I should see the API documentation$/ do
uri = URI.parse('http://api.spire.io')
request = Net::HTTP::Get.new(uri.request_uri)
request['Accept'] = 'text/html'
response = Net::HTTP.start(uri.host, uri.port) {|http|
http.request(request)
}
page.should have_css('.api-reference')
# ugh.
script = 'document.getElementsByClassName("api-reference")[0].innerHTML'
actual = page.evaluate_script(script)
script = <<-SCRIPT
var div = document.createElement('div')
div.innerHTML = #{ response.body.inspect };
return div.innerHTML;
SCRIPT
expected = page.execute_script(script)
actual.should_not be_nil
actual.should == expected
end
@jxson
Copy link
Copy Markdown
Author

jxson commented Feb 1, 2012

actual is the content that was injected by the application's javascript

The reason for the crazy expected = page.execute_script(script) on line 24 for ensuring that any transformations on the actual injected content caused by sticking it into the DOM are applied the same to the expected html content. Without this step problems with whitespace and escaped html will cause some serious head scratching...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment