4

How can I capture JavaScript errors using WebDriver for Chrome and IE drivers?

0

1 Answer 1

1

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...

Sign up to request clarification or add additional context in comments.

3 Comments

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???
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?
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.