Reputation: 49
How can I capture JavaScript errors using WebDriver for Chrome and IE drivers?
Upvotes: 4
Views: 1657
Reputation: 2199
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...
Upvotes: 1