Capturing JavaScript error in Selenium

前端 未结 12 720
盖世英雄少女心
盖世英雄少女心 2020-12-02 08:15

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 exam

12条回答
  •  栀梦
    栀梦 (楼主)
    2020-12-02 08:28

    Here my solution inspiring by jhanifen's response:

    // common.js - js file common to the entire app
    globalError = []
    window.onerror = function (msg, url, line, col, error) {
        globalError.push({msg:msg, url:url, line:line})
    };
    
    # tests.py
    def tearDown(driver):
        # assert on js error 
        ret = driver.selenium.execute_script("return globalError ")
        driver.assertFalse(ret, "errors %r " % ret)
        # ret will be a dict looking like 
        # {'line': 50, 'url': 'http://localhost:8081/static/js/common.js', 'msg': 'Uncaught ReferenceError: s is not defined'}
    

提交回复
热议问题