Generated screenshot for login page is blank - javascript

I am trying blow code for creating screenshot using phantomJS 2.1.1 but getting blank screenshot. Can please tell me what i did wrong?
var page = require('webpage').create();
page.open('https://sigview.sigmoid.io/app/#/signIn',function({
setTimeout(function() {
page.render('sigview.png');
phantom.exit();
}, 1000);
});
I expected it will capture whole page of screenshot.

When I add:
page.onConsoleMessage = function(msg, lineNum, sourceId) {
console.log('CONSOLE: ' + msg + ' (from line #' + lineNum + ' in "' + sourceId + '")');
};
Then I get:
CONSOLE: Google Analytics Initialized (from line # in "")
CONSOLE: jQuery.Deferred exception: undefined is not a function (evaluating 'Object.assign({},d,o)') t#https://sigview.sigmoid.io/app/scripts/vendor.00e53c4a.js:1:8081643
o#https://sigview.sigmoid.io/app/scripts/scripts.e53141f8.js:1:8772726
invoke#https://sigview.sigmoid.io/app/scripts/vendor.00e53c4a.js:1:464073
https://sigview.sigmoid.io/app/scripts/vendor.00e53c4a.js:1:458160
invoke#https://sigview.sigmoid.io/app/scripts/vendor.00e53c4a.js:1:464073
https://sigview.sigmoid.io/app/scripts/vendor.00e53c4a.js:1:462631
getService#https://sigview.sigmoid.io/app/scripts/vendor.00e53c4a.js:1:460540
injectionArgs#https://sigview.sigmoid.io/app/scripts/vendor.00e53c4a.js:1:463648
instantiate#https://sigview.sigmoid.io/app/scripts/vendor.00e53c4a.js:1:464197
https://sigview.sigmoid.io/app/scripts/vendor.00e53c4a.js:1:518515
createDetachedTour#https://sigview.sigmoid.io/app/scripts/scripts.e53141f8.js:1:11736460
https://sigview.sigmoid.io/app/scripts/scripts.e53141f8.js:1:3025948
invoke#https://sigview.sigmoid.io/app/scripts/vendor.00e53c4a.js:1:464073
https://sigview.sigmoid.io/app/scripts/vendor.00e53c4a.js:1:462993
forEach#https://sigview.sigmoid.io/app/scripts/vendor.00e53c4a.js:1:427302
createInjector#https://sigview.sigmoid.io/app/scripts/vendor.00e53c4a.js:1:462942
doBootstrap#https://sigview.sigmoid.io/app/scripts/vendor.00e53c4a.js:1:438797
bootstrap#https://sigview.sigmoid.io/app/scripts/vendor.00e53c4a.js:1:439323
angularInit#https://sigview.sigmoid.io/app/scripts/vendor.00e53c4a.js:1:438068
https://sigview.sigmoid.io/app/scripts/vendor.00e53c4a.js:1:722817
mightThrow#https://sigview.sigmoid.io/app/scripts/vendor.00e53c4a.js:1:237532
https://sigview.sigmoid.io/app/scripts/vendor.00e53c4a.js:1:238173 undefined (from line # in "")
I suspect the undefined errors make PhantomJs choke.
My lazy fix would be, not to use a deprecated library like PhantomJS and try https://github.com/GoogleChrome/puppeteer :)

Related

Phantomjs not loading the correct url inside onResourceRequested

I have a simple script based off Phantomjs's Network Monitoring example that tries to log all the requests and the response headers associated with a particular website:
"use strict";
var page = require('webpage').create(),
system = require('system'),
address;
if (system.args.length === 1) {
console.log('Usage: getRequests.js <some URL>');
phantom.exit(1);
} else {
address = system.args[1];
console.log('The url entered is ' + address + '\n')
page.onResourceReceived = function(response) {
console.log('Received Request: ' + JSON.stringify(response.headers, undefined, 4) + '\n');
};
page.onResourceRequested = function (req) {
console.log('Request URL ' + JSON.stringify(req.url, undefined, 4) + "\n");
}
page.onResourceError = function(resourceError) {
console.log('Unable to load resource (#' + resourceError.id + 'URL:' + resourceError.url + ')');
console.log('Error code: ' + resourceError.errorCode + '. Description: ' + resourceError.errorString);
};
page.open(address);
}
However, when I try to run it, using ./phantomjs getRequests.js www.google.com (or any other webpage), it gives the error:
The url entered is www.google.com
Request URL "file:///home/myusername/scripts/www.google.com"
Unable to load resource (#1URL:file:///home/myusername/scripts/www.google.com)
Error code: 203. Description: Error opening /home/myusername/scripts/www.google.com: No such file or directory
Received Request: []
Basically, the url that I am entering on the terminal is being prepended with the path of the script file. The input that I am getting inside the address variable is perfectly fine i.e. google.com
Could anyone please help with this issue? I cannot understand why phantomjs maybe doing this. I am running Ubuntu 14.04 on VirtualBox. And the script was running fine before when I was just trying to output the requests for a particular page.
Thanks.
You'll probably laugh, but the correct way to use the script is
./phantomjs getRequests.js http://www.google.com

why isn't my casperjs displaying any errors?

var casper = require('casper').create({
logLevel:'deubg',
verbose:true,
});
casper.start(someurl,function(){
not_existing_function();
})
When executing above code, all I see on the screen is debug informations that means very little to me. I am expecting to see some error saying the function being called doesn't exist, but there is not.
I thought it was just the behavior, until I see this.
The question clearly indicates he has got some error message:
ReferenceError: Can't find variable: $
Why can't I see something like this on my screen?
You're likely using PhantomJS 2.x. It has a known bug where some errors are not reported. That includes the class of errors that you're describing.
Also, registering to the various error events of CasperJS/PhantomJS doesn't help in this case, but here they are just in case:
// http://phantomjs.org/api/phantom/handler/on-error.html
phantom.onError = function(msg, trace) {
var msgStack = ['PHANTOM ERROR: ' + msg];
if (trace && trace.length) {
msgStack.push('TRACE:');
trace.forEach(function(t) {
msgStack.push(' -> ' + (t.file || t.sourceURL) + ': ' + t.line + (t.function ? ' (in function ' + t.function +')' : ''));
});
}
console.error(msgStack.join('\n'));
phantom.exit(1);
};
// http://docs.casperjs.org/en/latest/events-filters.html#remote-message
casper.on("remote.message", function(msg) {
this.echo("Console: " + msg);
});
// http://docs.casperjs.org/en/latest/events-filters.html#page-error
casper.on("page.error", function(msg, trace) {
this.echo("Error: " + msg);
// maybe make it a little fancier with the code from the PhantomJS equivalent
});
// http://docs.casperjs.org/en/latest/events-filters.html#resource-error
casper.on("resource.error", function(resourceError) {
this.echo("ResourceError: " + JSON.stringify(resourceError, undefined, 4));
});
// http://docs.casperjs.org/en/latest/events-filters.html#page-initialized
casper.on("page.initialized", function(page) {
// CasperJS doesn't provide `onResourceTimeout`, so it must be set through
// the PhantomJS means. This is only possible when the page is initialized
page.onResourceTimeout = function(request) {
console.log('Response Timeout (#' + request.id + '): ' + JSON.stringify(request));
};
});
You can run something like eslint or jshint over the script to catch syntax errors and you can run your script in PhantomJS 1.9.8/1.9.7 in order to catch these sort of errors.

CasperJS/PhantomJS chokes on loading a page

I'm running the following script with phantomjs:
var casper = require('casper').create();
var url = 'https://itunesconnect.apple.com/itc/static/login?view=1&path=%2FWebObjects%2FiTunesConnect.woa%3F'
casper.on("page.error", function(msg, trace) {
this.echo("Error: " + msg, "ERROR");
this.echo("file: " + trace[0].file, "WARNING");
this.echo("line: " + trace[0].line, "WARNING");
this.echo("function: " + trace[0]["function"], "WARNING");
errors.push(msg);
});
casper.start(url, function(){
casper.wait(7000, function(){
// casper.echo(casper.getHTML());
})
})
casper.run(function() {
if (errors.length > 0) {
this.echo(errors.length + ' Javascript errors found', "WARNING");
} else {
this.echo(errors.length + ' Javascript errors found', "INFO");
}
casper.exit();
});
Until a few days ago I could access the page which loads an iframe that contains 2 form fields, to allow user login.
Now I get the following error:
Error: Error: undefined is not a constructor (evaluating '$stateParams.path.startsWith('/')')
at setupDSiFrame (https://itunesconnect.apple.com/itc/static-resources/controllers/login_cntrl.js?cache=111920151100:99:46)
at https://itunesconnect.apple.com/itc/static-resources/controllers/login_cntrl.js?cache=111920151100:19:37
at $digest (https://itunesconnect.apple.com/itc/js/compiled/lib/vendor.js?cache=111920151100:13:11750)
at $apply (https://itunesconnect.apple.com/itc/js/compiled/lib/vendor.js?cache=111920151100:13:13237)
at f (https://itunesconnect.apple.com/itc/js/compiled/lib/vendor.js?cache=111920151100:12:56414)
at r (https://itunesconnect.apple.com/itc/js/compiled/lib/vendor.js?cache=111920151100:12:65848)
at onreadystatechange (https://itunesconnect.apple.com/itc/js/compiled/lib/vendor.js?cache=111920151100:12:66409)
file: https://itunesconnect.apple.com/itc/js/compiled/lib/vendor.js?cache=111920151100
line: 12
The page loads fine using slimerjs as the engine, but when using slimerjs the login form does not get filled in because the window is not in focus.
I believe this is an issue where casper is using an old version of WebKit and chokes on loading the page. How would I fix this?
Too late to solve the problem but maybe useful for future reference if somebody finds the question searching for a problem with startsWith in PhantomJS (as I did): startsWith method was added on the ECMAScript 6 specification, which is not supported by PhantomJS.
A good polyfill for this is mathiasbynens/String.prototype.startsWith

Phantomjs does not open web page

I have started testing campus2020 site with casperjs (1.1.0-beta3) + phantomjs (1.9.8). And faced with the problem that site is not opening but instead tests just freeze. I have taken script example from phantomjs site:
var page = require('webpage').create();
page.open('http://informatik.uni-leipzig.de/campus2020', function(status) {
console.log("Status: " + status);
if(status === "success") {
page.render('example.png');
}
phantom.exit();
});
It worked fine on other sites. I have tested opening the campus2020 site with phantomjs on several environments: win 7, Ubuntu 14.04, with ghostdriver and java selenium webdriver, with phantomjs which is run in selenium grid on RHEL 6.6. All this options failed. I have tried to add userAgent option and setTimeout. Nothing changed. Also I tried to open this site using testing framework based on selenium webdriver which used phantomjs but it worked in the same way - phantomjs initialized and then freezes. Any ideas how could be this issue solved?
Update
Now my code looks like these:
var page = require('webpage').create();
console.log("Page is going to be opened...")
page.open('http://informatik.uni-leipzig.de/campus2020/', function(status) {
console.log("Status: " + status);
if(status === "success") {
page.render('example.png');
}
phantom.exit();
});
page.onConsoleMessage = function(msg, lineNum, sourceId) {
console.log('CONSOLE: ' + msg + ' (from line #' + lineNum + ' in "' + sourceId + '")');
};
page.onError = function (msg, trace) {
console.log(msg);
trace.forEach(function(item) {
console.log(' ', item.file, ':', item.line);
})
}
page.onResourceError = function(resourceError) {
console.log('Unable to load resource (#' + resourceError.id + 'URL:' + resourceError.url + ')');
console.log('Error code: ' + resourceError.errorCode + '. Description: ' + resourceError.errorString);
};
page.onResourceTimeout = function(request) {
console.log('Response (#' + request.id + '): ' + JSON.stringify(request));
};
And no errors are shown.
I don't know why there is this issue, but you can update to PhantomJS 2 and it will work. There are not yet binaries for Linux, so you will need to build it yourself.
You will also need to update your CasperJS version, because the 1.1-beta3 doesn't support PhantomJS 2, but the master branch on GitHub does.
phantomjs --debug=true phantom_test.js

Apache Cordova -- Download file from local web directory to Android/iOS directories?

having a bit of trouble with my download function for Cordova. I have a function here:
document.addEventListener("deviceready", function () {
var fileUrl = e.target.getAttribute('data-soundurl');
console.log(fileUrl);
//returns as "../www/card-sounds/sound2.m4a"
var fileOutputPath = e.target.getAttribute('data-quote') + ".m4a";
console.log(fileOutputPath);
//returns as "Sound Title.m4a"
var fileTransfer = new FileTransfer();
var output = "file:///android_asset/www/res/db/"+fileOutputPath;
fileTransfer.download(
fileUrl,
output,
function(entry) {console.log("download complete: " + entry.toURL());},
function(error) {console.log("download error source " + error.source);
console.log("download error target " + error.target);
console.log("upload error code" + error.code);
},
false,
{headers:{"Authorization": "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA=="}}
);
console.log(output);
//returns as "file:///android_asset_www_res/db/Sound Title.m4a"
}, true);
And my log output just shows:
download error source null
download error target null
upload error codenull
I can't seem to get any sort of .root part of the FileSystem to actually give me a defined string so I'm just trying to get it to download to Android for now. Am I doing something fundamentally wrong? I am so confused here.
Try this :
var output = "/res/db/"+fileOutputPath;
For your output use
var output = cordova.file.externalRootDirectory + fileOutputPath;
cordova.file.externalRootDirectory should give you something like "file:///storage/emulated/0/"

Categories

Resources