Oleg Strokatyy
Oleg Strokatyy

Reputation: 641

Is it possible to catch and log all JS errors that could happen through my test runs in Google Chrome

I work with Selenium WedDriver in C#.

Is there a way to catch and log all JS errors that could happen through my test runs in Google Chrome?

Such as "JSErrorCollector" for Firefox - http://mguillem.wordpress.com/2011/10/11/webdriver-capture-js-errors-while-running-tests/

Upvotes: 1

Views: 1665

Answers (4)

Henry Wilson
Henry Wilson

Reputation: 3351

As mentioned by Simeon Sinichkin, ChromeJSErrorCollector is immensely useful here. For anyone else in C#, here's some sample code to grab JS Errors and log them to a file/the console:

var options = new ChromeOptions();
options.AddExtension("ChromeJSErrorCollector.crx");
var driver = new ChromeDriver(options);

// run tests...

var javascriptDriver = driver as IJavaScriptExecutor;
var errors = javascriptDriver.ExecuteScript("return window.JSErrorCollector_errors ? window.JSErrorCollector_errors.pump() : []");
var writer = new StreamWriter("jsErrors.log");
var collection = errors as ReadOnlyCollection<object>;
foreach (var item in collection)
{
    var errorObject = item as Dictionary<string, object>;
    foreach (var field in errorObject)
    {
        Console.WriteLine(field.Key + " - " + field.Value);
        writer.WriteLine(field.Key + " - " + field.Value);
    }

    Console.WriteLine("-------------------");
    writer.WriteLine("-------------------");
}
writer.Flush();
writer.Close();

Output looks like this:

errorMessage - Error: Script error
http://requirejs.org/docs/errors.html#scripterror
lineNumber - 1795
pageUrl - http://someURL
sourceName - http://someURL/Scripts/require.js
-------------------

Upvotes: 2

Oleg Strokatyy
Oleg Strokatyy

Reputation: 641

Now we have the same plug-in for Chrome. It's here - https://github.com/dharrya/ChromeJSErrorCollector

It is very similar to JSErrorCollector.

Upvotes: 2

Tom G
Tom G

Reputation: 3650

Use chrome developer tools(ctrl+shift+i) and look at the console tab for errors.

Chrome also lets you set breakpoints on certain lines of your code and see all of the values of your variables.

Upvotes: 0

mbeasley
mbeasley

Reputation: 4874

I may be off base here as I've never worked with Selenium and therefore may not understand the context fully: but you can use window.onerror to catch any errors that occur in the browser and then do something with them (send them to a database or log file, perhaps).

Note, this is not strictly limited to JavaScript errors and can also include some HTTP errors as well.

Upvotes: 0

Related Questions