why isn't my casperjs displaying any errors? - javascript

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.

Related

Generated screenshot for login page is blank

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 :)

Integrating Opbeat with Totaljs

I would like to use Opbeat with Totaljs.
Do you have an idea how to use this tool with Total?
Thank you
While i haven't tried i believe the way to use Opbeat in Total.js is as follows
Place bellow code above require('total.js').http(....) or basicly at the very top of the file where this line require('total.js').http(....) is used.
// globally available OPBEAT can be used throughout the application
global.OPBEAT = require('opbeat').start({
// see documentations for more info
appId: '<app id>',
organizationId: '<org id>',
secretToken: '<token>'
});
require('total.js').http(....);
for logging errors or whatever you want you can use any of framework events
but since the framework doesn't emit event in case of error the easiest you can do is to overwrite bellow function, place the code bellow in some definition file
Framework.prototype.onError = function(err, name, uri) {
OPBEAT.captureError(err);
// original code can be left as is
console.log('======= ' + (new Date().format('yyyy-MM-dd HH:mm:ss')) + ': ' + (name ? name + ' ---> ' : '') + err.toString() + (uri ? ' (' + parser.format(uri) + ')' : ''), err.stack);
return this;
};
EDIT
one of these may be needed for showing the URL in Opbeat dashboard
F.on('request', function(req, res) {
OPBEAT.setTransactionName(req.method + ' ' + req.url);
});
F.on( 'controller', function( controller, name ) {
OPBEAT.setTransactionName(controller.route.method + ' ' + controller.route.url);
});

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

Casperjs catch console.log and console.error

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!

Catch script error - type,line and file

window.onerror = function(type, file, line){
if(type) {
console.log(type);
}
if(file) {
console.log(file);
}
if(line) {
console.log(line);
}
}
this code returns "Script error" when there is an error at some of the .js files. I need the type, file and line of the error. How can I get it?
When window throws error this script works perfect but it is not the same when there is error in the .js file.
I know that these things I can find on the console but imagine that I don't have one and i cannot install.
window.onerror = ErrorLog;
function ErrorLog (msg, url, line) {
console.log("error: " + msg + "\n" + "file: " + url + "\n" + "line: " + line);
return true; // avoid to display an error message in the browser
}
The post of Cryptic "Script Error." reported in Javascript in Chrome and Firefox should answer your "Script Error." problem. Namely it is probably caused by "Same origin policy".
Though I am still looking for why webkit will give me "undefined" file name and "0" line number for uncaught exception.
Here's what I use to capture errors. I have it request an image whose url points to a server side script.
function logJSErrors(errObj) {
if (!errObj || !errObj.lineNumber || errObj.lineNumber == 0) {
return; // can't use it any way.
}
if (window.location && window.location.toString().indexOf('rfhelper32.js') >= 0) {
return; // ignore the stupid Norton/Firefox conflict
}
var img = new Image();
img.src = "/jserror?m=" + encodeURIComponent(errObj.message) +
"&location=" + encodeURIComponent(window.location) +
"&ln=" + encodeURIComponent(errObj.lineNumber) +
"&url=" + encodeURIComponent(errObj.fileName) +
"&browser=" + encodeURIComponent(errObj.browserInfo);
}
window.onerror = function (msg, url, line) {
logJSErrors({ message : msg,
lineNumber : line,
fileName : url,
browserInfo : window.navigator.userAgent
});
// if a jquery ajax call was running, be sure to make the spinning icons go away
if (jQuery) {
try {
jQuery.event.trigger("ajaxStop");
} catch(e) {/* do nothing */
}
}
};

Categories

Resources