InfoHeap
Tech
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

Selenium tutorials

  • How to test a site using pytest
  • Python selenium webdriver - quick start
  • Pytest - using selenium with PhantomJS
  • Python selenium - execute javascript code
  • Python selenium - print browser log
  • Selenium Phantomjs - check browser errors using pytest
 
  • Home
  • > Tutorials
  • > CI
  • > Selenium

Selenium Phantomjs – check browser errors using pytest

By admin | Last updated on Jun 6, 2020

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 ================================================================

Suggested posts:

  1. PHP – Regex OR (alternation) examples using pipe
  2. Gmail – how to send email from different address
  3. Python pytest – using selenium with PhantomJS
  4. How to use phantomjs to create site/url snapshot thumbnail
  5. Apache – list loaded modules on Ubuntu
  6. NodeJS – npm beginner tutorial
  7. WordPress how to check if a post is being viewed by admin
  8. How to install phantomjs on Mac
Share this article: share on facebook share on linkedin tweet this submit to reddit
Posted in Tutorials | Tagged CI, PhantomJS, pytest, Python, Python Selenium, Selenium, Test Automation, Tutorials
  • Browse content
  • Article Topics
  • Article archives
  • Contact Us
Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | Company results | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries | InfoHeap Money

Copyright © 2025 InfoHeap.

Powered by WordPress