Checking browser errors (including Javascript errors) is an important use case in testing a website. Javascript errors are hard to QA and can cause bad user experience. Here is sample python pytest code using Selenium and PhantomJS which can be used to check browser errrors.
import pytest from selenium import webdriver def test_jserror(): url = 'https://infoheap.com/demo/page_having_js_member_errors.html' driver = webdriver.PhantomJS("/usr/local/bin/phantomjs") driver.get(url); log_arr = driver.get_log('browser') log_arr = filter(lambda x : x['level'] != 'INFO', log_arr) assert len(log_arr) == 0, "Page has %s browser errors. First error: %s" % (len(log_arr), log_arr[0]['message']) driver.close() driver.quit()
Note that we are ignoring browser info logs. Now assuming you have pytest and phantomjs, python selenium binding installed run the following
$ py.test test_browser_error.py
Here is sample outcome from a page having javascript errors.
================================================================== test session starts =================================================================== platform darwin -- Python 2.7.11, pytest-2.8.6, py-1.4.31, pluggy-0.3.1 rootdir: /Users/pkjain/tmp3/qa, inifile: collected 1 items test_browser_error.py F ======================================================================== FAILURES ======================================================================== ______________________________________________________________________ test_jserror ______________________________________________________________________ def test_jserror(): url = 'https://infoheap.com/demo/page_having_js_member_errors.html' driver = webdriver.PhantomJS("/usr/local/bin/phantomjs") driver.get(url); log_arr = driver.get_log('browser') log_arr = filter(lambda x : x['level'] != 'INFO', log_arr) > assert len(log_arr) == 0, "Page has %s browser errors. First error: %s" % (len(log_arr), log_arr[0]['message']) E AssertionError: Page has 1 browser errors. First error: ReferenceError: Can't find variable: b E global code (https://infoheap.com/demo/page_having_js_member_errors.html:7) E assert 1 == 0 E + where 1 = len([{'level': 'WARNING', 'message': "ReferenceError: Can't find variable: b\n global code (https://infoheap.com/demo/page_having_js_member_errors.html:7)", 'timestamp': 1456428469263}]) test_browser_error.py:10: AssertionError ================================================================ 1 failed in 2.94 seconds ================================================================