Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capturing JavaScript error in Selenium

Is there a way to capture errors occurring in the DOM in Selenium and probably flag the same as an error in the page?

To give a brief example, let's say I'm trying to bind an event on a non-existing HTML control, my browser throws an error saying:

element abcd not found in the console.

Now, if I want the same error to fail my selenium tests and the message that is shown on the browser is shown as the error message.

Is it possible to do something like this?

like image 773
Baz1nga Avatar asked Nov 15 '10 22:11

Baz1nga


4 Answers

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));
    }
}
like image 164
magnusarinell Avatar answered Oct 20 '22 12:10

magnusarinell


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>
like image 40
jhanifen Avatar answered Oct 20 '22 12:10

jhanifen


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.0 Linux Firefox 43.0

like image 20
kleptog Avatar answered Oct 20 '22 13:10

kleptog


Here's the python webdriver solution I use:

from selenium.common.exceptions import WebDriverException
import logging


def check_browser_errors(driver):
    """
    Checks browser for errors, returns a list of errors
    :param driver:
    :return:
    """
    try:
        browser_logs = driver.get_log('browser')
    except (ValueError, WebDriverException) as e:
        # Some browsers does not support getting logs
        logging.debug("Could not get browser logs for driver %s due to exception: %s",
                     driver, e)
        return []

    errors = [entry for entry in browser_logs if entry['level'] == 'SEVERE']

    return errors
like image 9
d3ming Avatar answered Oct 20 '22 12:10

d3ming