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
Related
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 :)
I'm working with PhantomJS and everything that I've done works fine, but I don't know what happened bit time ago. The URL doesn't open in PhantomJS. The URL is domain and the code is as follows:
var page = require('webpage').create(),fs = require('fs'), args=require('system').args;
// var url = 'http://fotogena.co';
var url = 'https://www.procuraduria.gov.co/CertWEB/Certificado.aspx?tpo=1';
page.viewportSize = { width: 400, height: 400 };
page.open(url, function (status) {
if(status==='success'){
setTimeout(loadScripts, 1500)
}else
console.log(
"Error opening url \"" + page.reason_url
+ "\": " + page.reason
);
});
page.onResourceError = function(resourceError) {
console.error('-----',resourceError.url + ': ' + resourceError.errorString, 'faber acá toy');
phantom.exit(1);
};
function loadScripts(){
setTimeout(function(){
page.render('./probandoAntecedentes.jpg')
phantom.exit(1);
},1000)
}
The console displays the following:
https://www.procuraduria.gov.co/CertWEB/Certificado.aspx?tpo=1: Connection closed
I searched for possible solutions and put --ssl-protocol=any and other options, but they didn't work.
I'm using PhantomJS 2.1.1
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
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.
I'am trying to catch site console.log and console.error by casperjs. In the case of console.log I have working code:
casper.on('remote.message', function(message) {
this.echo('remote message caught: ' + message);
});
But I can't figure out how to catch console.error. I need this for catching any resources error (like images not found).
There's also the page.error handler:
casper.on("page.error", function(msg, trace) {
this.echo("Error: " + msg, "ERROR");
});
Depending on which errors you need to catch, this one may be better.
Ok it's weird to answer my own question but I found a solution on a coderwall blog posted by dpashkevich:
casper.on('resource.received', function(resource) {
var status = resource.status;
if(status >= 400) {
casper.log('Resource ' + resource.url + ' failed to load (' + status + ')', 'error');
resourceErrors.push({
url: resource.url,
status: resource.status
});
}
});
Works brilliant
You can use the following event to get remote errors:
casper.on("resource.error", function(resourceError) {
this.echo("Resource error: " + "Error code: "+resourceError.errorCode+" ErrorString: "+resourceError.errorString+" url: "+resourceError.url+" id: "+resourceError.id, "ERROR");
});
Works like charm!