How can I capture JavaScript errors using WebDriver for Chrome and IE drivers?
1 Answer
You can execute custom JavaScript to hook into window.onerror.
You can tell your JavaScript to return your data back to Selenium, but because you are asking about errors, I suggest not doing that. Depending on the error, the error could break JavaScript and might prevent it from returning.
A more robust way might be to create a server exposing a handler to the web, to request from your JS.
So your JS to execute from Selenium (before the errors occur) might look like this:
window.onerror = function(message, file, line) {
var asyncHR = new XMLHttpRequest();
var URL = "https://www.yourserver.com/errorlogger?file=" + file + "&line=" + line + "&message=" + message;
asyncHR.open("GET", URL, true);
asyncHR.send();
};
And from there, let the server take care of the logging - write to file, DB, etc...
3 Comments
user2428471
Hi Pat Meeker,Thanks for your quick reply the above is working for FF browser only but it is not working for IE and Chrome browsers . could you please help me to handle those JS on IE and Chrome???
Dingredient
It seems to be working for me... Try opening this fiddle in your various browsers: jsfiddle.net/YfXZU/3 Perhaps there is an issue with your ChromeDriver and IEDriver. Are your Selenium tests able to otherwise run successfully in those browsers?
Vincent Robert
You can use
python -m SimpleHTTPServer 8080 to create a simple HTTP server on the command line if you just want to see the errors for yourself (for debugging) without actually logging anything.