Capturing JavaScript error in Selenium

前端 未结 12 732
盖世英雄少女心
盖世英雄少女心 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条回答
  •  旧时难觅i
    2020-12-02 08:18

    I use the following TestCase.tearDown() in my Python Selenium tests that makes the test fail in case of JavaScript errors:

    def tearDown(self):
        browser_logs = driver.get_log("browser")
        errors = [logentry['message'] for logentry in browser_logs if logentry['level'] == 'SEVERE']
        if errors:
            self.fail(f'The following JavaScript errors occurred: {"; ".join(errors)}')
    

    This is inspired by @kleptog and @d3ming answers.

提交回复
热议问题