In a recent discussion, Simeon Sinichkin highlighted the immense utility of ChromeJSErrorCollector in this context. For those working with C#, below is a snippet of sample code that demonstrates how to capture JS Errors and log them either to a file or the console:
var options = new ChromeOptions();
options.AddExtension("ChromeJSErrorCollector.crx");
var driver = new ChromeDriver(options);
// execute 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();
The resulting output will resemble the following format:
errorMessage - Error: Script error
http://requirejs.org/docs/errors.html#scripterror
lineNumber - 1795
pageUrl - http://someURL
sourceName - http://someURL/Scripts/require.js
-------------------