Skip to content

Instantly share code, notes, and snippets.

@ragulka
Last active August 29, 2015 13:59
Show Gist options
  • Save ragulka/10462921 to your computer and use it in GitHub Desktop.
Save ragulka/10462921 to your computer and use it in GitHub Desktop.
Log browser javascript errors in Selenium tests with camme/webdriverjs
<!-- In your HTML
We need to catch errors globally.
This example shows how to do it on test environment only, in EJS on node.js.
The principle remains the same regardless of backend or templating language
-->
<% if (env === 'test') { %>
<!-- Error logging for selenium -->
<script type="text/javascript">
"use strict";
window.jsErrors = [];
window.onerror = function (errorMessage, url, lineNumber) {
window.jsErrors.push({ message: errorMessage, url: url, lineNumber: lineNumber });
return false;
};
</script>
<% } %>
<!-- If you are using cross-domain scripts, make sure to add crossorigin="anonymous"
You also have to make sure that CORS is enabled on the server.
http://blog.errorception.com/2012/12/catching-cross-domain-js-errors.html
-->
<script type="text/javascript" src="http://some.other.domain.com/script.js" crossorigin="anonymous"></script>
// In your test suite (probably when setting up the suite and before running any tests)
// On client error event, we are checking if thare are any browser JS errors that have been
// caught. If there are some, we are logging them.
client.on('error', function() {
client.execute(function() {
return window.jsErrors;
}, [], function (err, result) {
if (!err && result.value && result.value.length) {
result.value.forEach(function(error) {
console.log('Client-side JS error: ' + error.message + ' [' + error.url + ':' + error.lineNumber + ']');
});
}
}).call();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment