From e4f7b810d733d8233b1f9f48739a3288095478b1 Mon Sep 17 00:00:00 2001 From: Daniel Senff Date: Thu, 8 Sep 2016 09:53:35 +0200 Subject: [PATCH] add target url as parameter to beforeCaptureJS If the website to be captured requires login, often it would redirect to a login-page. This redirect would change the value page.url available in the beforeCaptureJS function. The login could be handled through phantomjs or casperjs, but there is no way to get the original target url. So this change adds the target url as parameter and makes it available in the beforeCaptureJS. It's added as a new parameter at the end so it preserveres backwards-compatibility. --- lib/wraith/javascript/casper.js | 11 ++++++----- lib/wraith/javascript/phantom.js | 9 +++++---- templates/javascript/cookies_and_headers--casper.js | 4 ++-- templates/javascript/cookies_and_headers--phantom.js | 2 +- templates/javascript/disable_javascript--casper.js | 4 ++-- templates/javascript/disable_javascript--phantom.js | 4 ++-- templates/javascript/interact--casper.js | 4 ++-- templates/javascript/interact--phantom.js | 4 ++-- templates/javascript/wait--casper.js | 4 ++-- templates/javascript/wait--phantom.js | 4 ++-- 10 files changed, 26 insertions(+), 24 deletions(-) diff --git a/lib/wraith/javascript/casper.js b/lib/wraith/javascript/casper.js index 5236de0b..c2da9c34 100644 --- a/lib/wraith/javascript/casper.js +++ b/lib/wraith/javascript/casper.js @@ -59,15 +59,16 @@ casper.viewport(currentDimensions.viewportWidth, currentDimensions.viewportHeigh casper.then(function() { var self = this; if (globalBeforeCaptureJS && pathBeforeCaptureJS) { - require(globalBeforeCaptureJS)(self, function thenExecuteOtherBeforeCaptureFile() { - require(pathBeforeCaptureJS)(self, captureImage); - }); + var thenExecuteOtherBeforeCaptureFile = function() { + require(pathBeforeCaptureJS)(self, captureImage, url); + }; + require(globalBeforeCaptureJS)(self, thenExecuteOtherBeforeCaptureFile, url); } else if (globalBeforeCaptureJS) { - require(globalBeforeCaptureJS)(self, captureImage); + require(globalBeforeCaptureJS)(self, captureImage, url); } else if (pathBeforeCaptureJS) { - require(pathBeforeCaptureJS)(self, captureImage); + require(pathBeforeCaptureJS)(self, captureImage, url); } else { captureImage(); diff --git a/lib/wraith/javascript/phantom.js b/lib/wraith/javascript/phantom.js index 78f1a89b..3fac055f 100644 --- a/lib/wraith/javascript/phantom.js +++ b/lib/wraith/javascript/phantom.js @@ -95,15 +95,16 @@ function markPageAsLoaded() { function runSetupJavaScriptThen(callback) { setupJavaScriptRan = true; if (globalBeforeCaptureJS && pathBeforeCaptureJS) { - require(globalBeforeCaptureJS)(page, function thenExecuteOtherBeforeCaptureFile() { + var thenExecuteOtherBeforeCaptureFile = function () { require(pathBeforeCaptureJS)(page, callback); - }); + }; + require(globalBeforeCaptureJS)(page, thenExecuteOtherBeforeCaptureFile, url); } else if (globalBeforeCaptureJS) { - require(globalBeforeCaptureJS)(page, callback); + require(globalBeforeCaptureJS)(page, callback, url); } else if (pathBeforeCaptureJS) { - require(pathBeforeCaptureJS)(page, callback); + require(pathBeforeCaptureJS)(page, callback, url); } else { callback(); diff --git a/templates/javascript/cookies_and_headers--casper.js b/templates/javascript/cookies_and_headers--casper.js index d3eb340d..f3138918 100644 --- a/templates/javascript/cookies_and_headers--casper.js +++ b/templates/javascript/cookies_and_headers--casper.js @@ -2,7 +2,7 @@ // This is an example module provided by Wraith. // Feel free to amend for your own requirements. // ###################################################### -module.exports = function (casper, ready) { +module.exports = function (casper, ready, url) { // reload page with headers set casper.open(casper.page.url, { method: 'get', @@ -13,4 +13,4 @@ module.exports = function (casper, ready) { casper.then(function () { setTimeout(ready, 1000); }); -} \ No newline at end of file +} diff --git a/templates/javascript/cookies_and_headers--phantom.js b/templates/javascript/cookies_and_headers--phantom.js index 47b6e525..ab507d5d 100644 --- a/templates/javascript/cookies_and_headers--phantom.js +++ b/templates/javascript/cookies_and_headers--phantom.js @@ -2,7 +2,7 @@ // This is an example module provided by Wraith. // Feel free to amend for your own requirements. // ###################################################### -module.exports = function (phantom, ready) { +module.exports = function (phantom, ready, url) { page.customHeaders = { 'SOME-HEADER': 'fish' diff --git a/templates/javascript/disable_javascript--casper.js b/templates/javascript/disable_javascript--casper.js index 04832ab4..73a40fee 100644 --- a/templates/javascript/disable_javascript--casper.js +++ b/templates/javascript/disable_javascript--casper.js @@ -2,10 +2,10 @@ // This is an example module provided by Wraith. // Feel free to amend for your own requirements. // ###################################################### -module.exports = function (casper, ready) { +module.exports = function (casper, ready, url) { // disable JavaScript casper.options.pageSettings.javascriptEnabled = false; // reload the page without JS enabled casper.thenOpen(casper.page.url); -} \ No newline at end of file +} diff --git a/templates/javascript/disable_javascript--phantom.js b/templates/javascript/disable_javascript--phantom.js index 723bfbaf..5d7f447b 100644 --- a/templates/javascript/disable_javascript--phantom.js +++ b/templates/javascript/disable_javascript--phantom.js @@ -2,7 +2,7 @@ // This is an example module provided by Wraith. // Feel free to amend for your own requirements. // ###################################################### -module.exports = function (phantom, ready) { +module.exports = function (phantom, ready, url) { // disable JavaScript phantom.settings.javascriptEnabled = false; @@ -10,4 +10,4 @@ module.exports = function (phantom, ready) { phantom.open(phantom.url, function () { setTimeout(ready, 5000); }); -} \ No newline at end of file +} diff --git a/templates/javascript/interact--casper.js b/templates/javascript/interact--casper.js index 6af229a0..ddda33f8 100644 --- a/templates/javascript/interact--casper.js +++ b/templates/javascript/interact--casper.js @@ -2,10 +2,10 @@ // This is an example module provided by Wraith. // Feel free to amend for your own requirements. // ###################################################### -module.exports = function (casper, ready) { +module.exports = function (casper, ready, url) { // test interaction on the page casper.wait(2000, function() { casper.click('.ns-panel__hotspot--2'); ready(); }); -} \ No newline at end of file +} diff --git a/templates/javascript/interact--phantom.js b/templates/javascript/interact--phantom.js index 66c89ba3..62b3917b 100644 --- a/templates/javascript/interact--phantom.js +++ b/templates/javascript/interact--phantom.js @@ -2,7 +2,7 @@ // This is an example module provided by Wraith. // Feel free to amend for your own requirements. // ###################################################### -module.exports = function (phantom, ready) { +module.exports = function (phantom, ready, url) { // test interaction on the page phantom.evaluate(function(){ @@ -14,4 +14,4 @@ module.exports = function (phantom, ready) { }); ready(); -} \ No newline at end of file +} diff --git a/templates/javascript/wait--casper.js b/templates/javascript/wait--casper.js index dda32220..91b5138c 100644 --- a/templates/javascript/wait--casper.js +++ b/templates/javascript/wait--casper.js @@ -2,7 +2,7 @@ // This is an example module provided by Wraith. // Feel free to amend for your own requirements. // ###################################################### -module.exports = function (casper, ready) { +module.exports = function (casper, ready, url) { // make Wraith wait a bit longer before taking the screenshot casper.wait(2000, ready); // you MUST call the ready() callback for Wraith to continue -} \ No newline at end of file +} diff --git a/templates/javascript/wait--phantom.js b/templates/javascript/wait--phantom.js index bc660713..b363619b 100644 --- a/templates/javascript/wait--phantom.js +++ b/templates/javascript/wait--phantom.js @@ -2,7 +2,7 @@ // This is an example module provided by Wraith. // Feel free to amend for your own requirements. // ###################################################### -module.exports = function (phantom, ready) { +module.exports = function (phantom, ready, url) { // make Wraith wait a bit longer before taking the screenshot setTimeout(ready, 2000); // you MUST call the ready() callback for Wraith to continue -} \ No newline at end of file +}