Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ready for review] Snapfile refactor #439

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/wraith/gallery.rb
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,10 @@ def check_failed_shots
end

def prompt_user_to_open_gallery(dest)
gallery_url = "file://#{Dir.pwd}/#{dest}"
logger.info "\nView the gallery in your browser:"
logger.info "\t file://" + Dir.pwd + "/" + dest
logger.info "\t #{gallery_url}"
`open #{gallery_url}`
end

class ErbBinding < OpenStruct
Expand Down
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
35 changes: 29 additions & 6 deletions lib/wraith/javascript/casper.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var url = casper.cli.get(0),
globalBeforeCaptureJS = casper.cli.get(4),
pathBeforeCaptureJS = casper.cli.get(5),
dimensionsProcessed = 0,
currentDimensions;
currentDimensions = dimensions;

// functions
function requireRelative(file) {
Expand All @@ -23,11 +23,33 @@ function requireRelative(file) {
currentFilePath = fs.absolute(currentFilePath.join('/'));
return require(currentFilePath + '/' + file);
}

function setViewportHeight() {
if (!currentDimensions.viewportHeight) {
currentDimensions.viewportHeight = 1500;
}
}

function snap() {
console.log('Snapping ' + url + ' at: ' + currentDimensions.viewportWidth + 'x' + currentDimensions.viewportHeight);
// dynamic height calculation not possible in non-JavaScript mode
var documentHeight = this.evaluate(function () {
return document.body.offsetHeight;
}) || false;

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

console.log('Snapping ' + url + ' at: ' + currentDimensions.viewportWidth + 'x' + documentHeight);

if (!selector) {
this.capture(image_name);
this.capture(image_name, {
top: 0,
left: 0,
width: currentDimensions.viewportWidth,
height: documentHeight
});
}
else {
this.captureSelector(image_name, selector);
Expand All @@ -36,6 +58,7 @@ function snap() {
dimensionsProcessed++;
if (helper.takingMultipleScreenshots(dimensions) && dimensionsProcessed < dimensions.length) {
currentDimensions = dimensions[dimensionsProcessed];
setViewportHeight();
image_name = helper.replaceImageNameWithDimensions(image_name, currentDimensions);
casper.viewport(currentDimensions.viewportWidth, currentDimensions.viewportHeight);
casper.wait(300, function then () {
Expand All @@ -46,11 +69,11 @@ function snap() {

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

setViewportHeight();

// Casper can now do its magic
casper.start();
Expand Down
22 changes: 22 additions & 0 deletions lib/wraith/javascript/phantom-helpers/default_settings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
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
};

}
27 changes: 27 additions & 0 deletions lib/wraith/javascript/phantom-helpers/handle_before_captures.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module.exports = function (hooks) {

var setupJavaScriptRan = false,
globalBeforeCaptureJS = hooks.config_level_js === 'false' ? false : hooks.config_level_js,
pathBeforeCaptureJS = hooks.path_level_js === 'false' ? false : hooks.path_level_js,
callback = hooks.on_completion;

function runSetupJavaScriptThen(callback) {
setupJavaScriptRan = true;
if (globalBeforeCaptureJS && pathBeforeCaptureJS) {
require(globalBeforeCaptureJS)(page, function thenExecuteOtherBeforeCaptureFile() {
require(pathBeforeCaptureJS)(page, callback);
});
}
else if (globalBeforeCaptureJS) {
require(globalBeforeCaptureJS)(page, callback);
}
else if (pathBeforeCaptureJS) {
require(pathBeforeCaptureJS)(page, callback);
}
else {
callback();
}
}

runSetupJavaScriptThen(callback);
}
41 changes: 41 additions & 0 deletions lib/wraith/javascript/phantom-helpers/wait_for_page_to_load.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
module.exports = function (page, callback) {

var current_requests = 0;

var waitTime = 300,
maxWait = 5000,
beenLoadingFor = 0;

page.onError = function(msg, trace) {
// suppress JS errors from Wraith output
// http://stackoverflow.com/a/19538646
};

page.onResourceRequested = function(req) {
current_requests += 1;
};

page.onResourceReceived = function(res) {
if (res.stage === 'end') {
current_requests -= 1;
}
};

function checkStatusOfAssets() {
if (current_requests >= 1) {
if (beenLoadingFor > maxWait) {
// sometimes not all assets will download in an acceptable time - continue anyway.
callback();
}
else {
beenLoadingFor += waitTime;
setTimeout(checkStatusOfAssets, waitTime);
}
}
else {
callback();
}
}

setTimeout(checkStatusOfAssets, waitTime);
}
Loading