Below is the code I am using to detect JavaScript errors while performing Selenium tests:
public static void AssertNoJavaScriptErrorsInLog(this RemoteWebDriver driver)
{
var errorStrings = new List<string> { "SyntaxError", "EvalError", "ReferenceError", "RangeError", "TypeError", "URIError" };
var jsErrors = driver.Manage().Logs.GetLog(LogType.Browser).Where(x => errorStrings.Any(e => x.Message.Contains(e))).ToList();
if (jsErrors.Any())
{
Assert.Fail("JavaScript error(s):" + Environment.NewLine + jsErrors.Aggregate("", (s, entry) => s + entry.Message + Environment.NewLine));
}
}
During one of my tests, an error appears intermittently:
Assert.Fail failed. JavaScript error(s):
TypeError: doc.documentElement is null
TypeError: doc.documentElement is null
I attempted to check the FireFox browser console after the test failure, but it was empty!
Why are the JavaScript errors not visible in the browser console?