Capturing JavaScript error in Selenium Capturing JavaScript error in Selenium selenium selenium

Capturing JavaScript error in Selenium


I'm doing this to capture JavaScript errors:

[TestCleanup]public void TestCleanup(){    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)));    if (jsErrors.Any())    {        Assert.Fail("JavaScript error(s):" + Environment.NewLine + jsErrors.Aggregate("", (s, entry) => s + entry.Message + Environment.NewLine));    }}


Put this script on your page and then check in Selenium for the JSError:

<script type="text/javascript">    window.onerror=function(msg){        $("body").attr("JSError",msg);    }</script>


Not sure when this changed, but right now this works for me in Python. The file is a simple page with a javascript error.

In [11]: driver.get("file:///tmp/a.html")In [12]: driver.get_log("browser")Out[12]: [{u'level': u'SEVERE',  u'message': u'ReferenceError: foo is not defined',  u'timestamp': 1450769357488,  u'type': u''}, {u'level': u'INFO',  u'message': u'The character encoding of the HTML document was not declared. The document will render with garbled text in some browser configurations if the document contains characters from outside the US-ASCII range. The character encoding of the page must be declared in the document or in the transfer protocol.',  u'timestamp': 1450769357498,  u'type': u''}]

Python-Selenium version 2.48.0Linux Firefox 43.0