Skip to content

Commit

Permalink
started working on #318
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisBAshton committed May 22, 2016
1 parent fc23aaf commit ed33047
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 13 deletions.
2 changes: 1 addition & 1 deletion lib/wraith/javascript/_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = function (commandLineDimensions) {
dimensions = /(\d*)x?((\d*))?/i.exec(dimensions);
return {
'viewportWidth': parseInt(dimensions[1]),
'viewportHeight': parseInt(dimensions[2] || 1500)
'viewportHeight': parseInt(dimensions[2] || false)
};
}

Expand Down
20 changes: 19 additions & 1 deletion lib/wraith/javascript/phantom-helpers/default_settings.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
module.exports = function (page) {
module.exports = function (page, currentDimensions) {
page.settings = { loadImages: true, javascriptEnabled: true};
page.settings.userAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.17';

var height = 1500;

if (currentDimensions.viewportHeight) {
height = currentDimensions.viewportHeight;
console.log('Loading ' + url + ' at dimensions: ' + currentDimensions.viewportWidth + 'x' + height);
}
else {
console.log('Loading ' + url + ' at width: ' + currentDimensions.viewportWidth);
}

page.viewportSize = {
width: currentDimensions.viewportWidth,
// what matters is the clipRect, not the viewport height.
// And if it DOES matter, users can set it explicitly.
height: height
};

}
39 changes: 28 additions & 11 deletions lib/wraith/javascript/phantom.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,16 @@ var url = system.args[1],
globalBeforeCaptureJS = system.args[5],
pathBeforeCaptureJS = system.args[6],
dimensionsProcessed = 0,
currentDimensions = dimensions;
currentDimensions = dimensions,
documentHeight = 1500;

if (helper.takingMultipleScreenshots(dimensions)) {
currentDimensions = dimensions[0];
image_name = helper.replaceImageNameWithDimensions(image_name, currentDimensions);
}

console.log('Loading ' + url + ' at dimensions: ' + currentDimensions.viewportWidth + 'x' + currentDimensions.viewportHeight);
page.viewportSize = {
width: currentDimensions.viewportWidth,
height: currentDimensions.viewportHeight
};

// set up default `page` object
require('./phantom-helpers/default_settings.js')(page);
require('./phantom-helpers/default_settings.js')(page, currentDimensions);

// functions
var waitForPageToLoad = require('./phantom-helpers/wait_for_page_to_load.js'),
Expand All @@ -36,6 +31,19 @@ page.open(url, function(status) {
exit_phantom('Error with page ' + url);
}
waitForPageToLoad(page, function markPageAsLoaded() {

// dynamic height calculation not possible in the following situations:
// * `resize` mode
// * non-JavaScript mode
documentHeight = page.evaluate(function () {
return document.body.offsetHeight;
}) || false;

if (!documentHeight) {
documentHeight = 1500;
console.log('Could not dynamically determine document height.');
}

handleBeforeCaptureHooks({
config_level_js: globalBeforeCaptureJS,
path_level_js: pathBeforeCaptureJS,
Expand All @@ -50,6 +58,15 @@ function captureImage() {
dimensionsProcessed++;
if (helper.takingMultipleScreenshots(dimensions) && dimensionsProcessed < dimensions.length) {
currentDimensions = dimensions[dimensionsProcessed];

if (currentDimensions.viewportHeight) {
console.log('Resizing ' + url + ' to: ' + currentDimensions.viewportWidth + 'x' + currentDimensions.viewportHeight);
}
else {
currentDimensions.viewportHeight = 1500;
console.log('Resizing ' + url + ' to width: ' + currentDimensions.viewportWidth);
}

image_name = helper.replaceImageNameWithDimensions(image_name, currentDimensions);
resizeAndCaptureImage();
} else {
Expand All @@ -58,7 +75,7 @@ function captureImage() {
}

function resizeAndCaptureImage() {
console.log('Resizing ' + url + ' to: ' + currentDimensions.viewportWidth + 'x' + currentDimensions.viewportHeight);
console.log('Resizing to: ' + currentDimensions.viewportWidth + 'x' + currentDimensions.viewportHeight);
page.viewportSize = {
width: currentDimensions.viewportWidth,
height: currentDimensions.viewportHeight
Expand All @@ -67,11 +84,11 @@ function resizeAndCaptureImage() {
}

function takeScreenshot() {
console.log('Snapping ' + url + ' at: ' + currentDimensions.viewportWidth + 'x' + currentDimensions.viewportHeight);
console.log('Snapping ' + url + ' at: ' + currentDimensions.viewportWidth + 'x' + documentHeight);
page.clipRect = {
top: 0,
left: 0,
height: currentDimensions.viewportHeight,
height: documentHeight,
width: currentDimensions.viewportWidth
};
page.render(image_name);
Expand Down

0 comments on commit ed33047

Please sign in to comment.