Skip to content

Commit

Permalink
Render full window size if document has no height
Browse files Browse the repository at this point in the history
When rendering a screenshot with `:full => true` on a document which has
a height of zero, the call fails silently with no screenshot being
created.  This situation can occur with pages that use absolute or fixed
positioning for all content, for example.

Changed to use the `documentElement`'s `clientHeight` & `clientWidth` as
a fallback, which will result in screenshots the size of the viewport in
that case.
  • Loading branch information
kevinmcconnell committed Apr 23, 2014
1 parent 1f0c785 commit 8e90854
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
(Pedro Carriço and Erik Ostrom) [Issue #432]
* Raise exception on PhantomJS "status: fail" result (i.e DNS issue) instead
of returning minimal HTML body (Dean Holdren) [Issue #473]
* Render full window size when document has no height (Kevin McConnell)

### 1.5.0 ###

Expand Down
4 changes: 2 additions & 2 deletions lib/capybara/poltergeist/client/agent.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ class PoltergeistAgent
@elements.length - 1

documentSize: ->
height: document.documentElement.scrollHeight,
width: document.documentElement.scrollWidth
height: document.documentElement.scrollHeight || document.documentElement.clientHeight,
width: document.documentElement.scrollWidth || document.documentElement.clientWidth

get: (id) ->
@nodes[id] or= new PoltergeistAgent.Node(this, @elements[id])
Expand Down
4 changes: 2 additions & 2 deletions lib/capybara/poltergeist/client/compiled/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ PoltergeistAgent = (function() {

PoltergeistAgent.prototype.documentSize = function() {
return {
height: document.documentElement.scrollHeight,
width: document.documentElement.scrollWidth
height: document.documentElement.scrollHeight || document.documentElement.clientHeight,
width: document.documentElement.scrollWidth || document.documentElement.clientWidth
};
};

Expand Down
11 changes: 11 additions & 0 deletions spec/integration/driver_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,17 @@ def session_url(path)
end
end

it 'supports rendering the entire window when documentElement has no height' do
@session.visit('/poltergeist/fixed_positioning')

create_screenshot file, full: true
File.open(file, 'rb') do |f|
expect(ImageSize.new(f.read).size).to eq(
@driver.evaluate_script('[window.innerWidth, window.innerHeight]')
)
end
end

it 'supports rendering just the selected element' do
@session.visit('/poltergeist/long_page')

Expand Down
6 changes: 6 additions & 0 deletions spec/support/views/fixed_positioning.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<!DOCTYPE html>
<body style="margin: 0; padding: 0;">
<div style="position: fixed">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
</div>
</body>

0 comments on commit 8e90854

Please sign in to comment.