Capturing browser logs with Selenium WebDriver using Java

后端 未结 9 1982
闹比i
闹比i 2020-11-28 03:01

Is there a way to capture browser logs while running automated test cases with Selenium? I found an article on how to capture JavaScript errors in Selenium. But that is just

9条回答
  •  时光取名叫无心
    2020-11-28 03:30

    As a non-java selenium user, here is the python equivalent to Margus's answer:

    from selenium import webdriver
    from selenium.webdriver.common.desired_capabilities import DesiredCapabilities    
    
    class ChromeConsoleLogging(object):
    
        def __init__(self, ):
            self.driver = None
    
        def setUp(self, ):
            desired = DesiredCapabilities.CHROME
            desired ['loggingPrefs'] = { 'browser':'ALL' }
            self.driver = webdriver.Chrome(desired_capabilities=desired)
    
        def analyzeLog(self, ):
            data = self.driver.get_log('browser')
            print(data)
    
        def testMethod(self, ):
            self.setUp()
            self.driver.get("http://mypage.com")
            self.analyzeLog()
    

    Reference

    Edit: Keeping Python answer in this thread because it is very similar to the Java answer and this post is returned on a Google search for the similar Python question

提交回复
热议问题