diff --git a/lib/browser/client-scripts/gemini.js b/lib/browser/client-scripts/gemini.js index 11db4640e..86b5f40c0 100644 --- a/lib/browser/client-scripts/gemini.js +++ b/lib/browser/client-scripts/gemini.js @@ -31,12 +31,21 @@ function prepareScreenshotUnsafe(selectors, opts) { return rect; } - window.scrollTo(rect.left, rect.top); - var viewportHeight = window.innerHeight || document.documentElement.clientHeight, + viewportWidth = window.innerWidth || document.documentElement.clientWidth, documentHeight = document.documentElement.scrollHeight, documentWidth = document.documentElement.scrollWidth, - coverage; + coverage, + viewPort = new Rect({ + left: util.getScrollLeft(), + top: util.getScrollTop(), + width: viewportWidth, + height: viewportHeight + }); + + if (!viewPort.rectInside(rect)) { + window.scrollTo(rect.left, rect.top); + } if (opts.coverage) { coverage = require('./gemini.coverage').collectCoverage(rect); diff --git a/lib/browser/client-scripts/rect.js b/lib/browser/client-scripts/rect.js index e75cf94a8..610c70b80 100644 --- a/lib/browser/client-scripts/rect.js +++ b/lib/browser/client-scripts/rect.js @@ -48,7 +48,7 @@ Rect.prototype = { }, rectInside: function(rect) { - return util.every(this._keyPoints(), function(point) { + return util.every(rect._keyPoints(), function(point) { return this.pointInside(point[0], point[1]); }, this); }, diff --git a/test/rect.test.js b/test/rect.test.js new file mode 100644 index 000000000..8d1066cca --- /dev/null +++ b/test/rect.test.js @@ -0,0 +1,67 @@ +'use strict'; + +var assert = require('chai').assert, + Rect = require('../lib/browser/client-scripts/rect').Rect; + +describe('Rect', function() { + describe('constructor', function() { + it('should create instance using width/height properties', function() { + assert.doesNotThrow(function() { + return new Rect({ + top: 10, + left: 20, + width: 100, + height: 100 + }); + }); + }); + + it('should create instance using bottom/right properties', function() { + assert.doesNotThrow(function() { + return new Rect({ + top: 10, + left: 20, + bottom: 100, + right: 100 + }); + }); + }); + + it('should fail when there are no bottom/right or width/height properties', function() { + assert.throws(function() { + return new Rect({top: 10, left: 20}); + }); + }); + }); + + describe('rectInside', function() { + var rect = new Rect({ + top: 10, + left: 20, + width: 100, + height: 100 + }); + + it('should return true when rect is inside', function() { + assert.isTrue(rect.rectInside( + new Rect({ + top: rect.top + 10, + left: rect.left + 10, + width: rect.width - 50, + height: rect.height - 50 + }) + )); + }); + + it('should return false when rect is not inside', function() { + assert.isFalse(rect.rectInside( + new Rect({ + top: rect.top - 5, + left: rect.left - 5, + width: rect.width, + height: rect.width + }) + )); + }); + }); +});