{"id":4730,"date":"2016-11-28T00:59:16","date_gmt":"2016-11-28T05:59:16","guid":{"rendered":"https:\/\/qxf2.com\/blog\/?p=4730"},"modified":"2018-04-27T10:07:59","modified_gmt":"2018-04-27T14:07:59","slug":"get-browser-console-log","status":"publish","type":"post","link":"https:\/\/qxf2.com\/blog\/get-browser-console-log\/","title":{"rendered":"Selenium to get browser console log"},"content":{"rendered":"<p>The browser console log, along with debugging also helps us capture errors that occurred when using a web application. Your developers will thank you when you include console errors in your bug reports. As part of running your GUI automation, it is a good habit to check and report the errors present in the browser&#8217;s console. In this post, I would like to share my learning on how to use Selenium to read the browser logs so that it would help others who are looking around for this info.<\/p>\n<hr \/>\n<p>We&#8217;ll perform the following steps in this post:<br \/>\n1. Learn the Selenium API method to obtain the browser log<br \/>\n2. Get the console log<br \/>\n3. Examine a sample log message<br \/>\n4. Filter the log based on message type<br \/>\n5. Put it all together<\/p>\n<hr>\n<p><strong>1. Selenium API to get the console log<\/strong><br \/>\n<a href=\"http:\/\/selenium-python.readthedocs.io\/api.html\">Selenium<\/a> provides a <code>get_log(log_type)<\/code> method which helps you get the log for a given log type. So in order to get the browser console logs we need to use log type as <em>browser <\/em><\/p>\n<pre lang=\"python\">\r\ndriver.get_log(\u2018browser\u2019)\r\n<\/pre>\n<hr \/>\n<p><strong>2. Get the console log<\/strong><br \/>\nYou can use the below method to get the console log<\/p>\n<pre lang=\"python\">\r\ndef get_browser_console_log(self):\r\n        \"Get the browser console log\"\r\n        try:\r\n            log = self.driver.get_log('browser')\r\n            return log\r\n        except Exception,e:\r\n            print(\"Exception when reading Browser Console log\")\r\n            print(str(e))\r\n<\/pre>\n<hr \/>\n<p><strong>3. Sample log message<\/strong><br \/>\nA sample log message would look like this<br \/>\n<code>[{u'source': u'javascript', u'message': u'https:\/\/qxf2.com\/selenium-tutorial-main 303:9 Uncaught TypeError: $(...) is not a function', u'timestamp': 1475757107472L, u'level': u'SEVERE'}]<\/code><\/p>\n<hr \/>\n<p><strong>4. Filter the log based on message type<\/strong><br \/>\nYou would want to filter out the log messages based on the level and get just the message from the result set. You can use the following piece of code to do this<\/p>\n<pre lang=\"python\">log = self.read_browser_console_log()\r\nlog_errors = []\r\nfor entry in log:\r\n                if entry['level']=='SEVERE':\r\n                    log_errors.append(entry['message'])\r\n\r\n<\/pre>\n<hr \/>\n<p><strong>5. Putting it all together<\/strong><br \/>\nI have created two methods <em>get_browser_console_log()<\/em> and <em>check_errors_console_log()<\/em> which would help you to get the browser console logs and print them out. <\/p>\n<pre lang=\"python\">\r\ndef check_errors_console_log(self,url):\r\n        \"Function to get the browser's console log errors\"\r\n        self.driver.get(url)\r\n        current_console_log_errors = []\r\n        #As IE driver does not support retrieval of any logs,\r\n        #we are bypassing the get_browser_console_log() method\r\n        if \"ie\" not in str(self.driver): \r\n            log_errors = []\r\n            new_errors = []\r\n            log = self.get_browser_console_log()\r\n            print \"Console Log: \",log\r\n            for entry in log:\r\n                if entry['level']=='SEVERE':\r\n                    log_errors.append(entry['message'])\r\n\r\n            if current_console_log_errors != log_errors:\r\n                #Find the difference\r\n                new_errors = list(set(log_errors) - set(current_console_log_errors))\r\n                #Set current_console_log_errors = log_errors\r\n                current_console_log_errors = log_errors\r\n\r\n            if len(new_errors)>0:\r\n                print(\"\\nBrowser console error on url: %s\\nConsole error(s):%s\"%(self.driver.current_url,'\\n----'.join(new_errors)))\r\n                \r\n\r\n    def get_browser_console_log(self):\r\n        \"Get the browser console log\"\r\n        try:\r\n            log = self.driver.get_log('browser')\r\n            return log\r\n        except Exception,e:\r\n            print(\"Exception when reading Browser Console log\")\r\n            print(str(e))\r\n<\/pre>\n<p>At <a href=\"http:\/\/www.qxf2.com\/?utm_source=browser_console&#038;utm_medium=click&#038;utm_campaign=From%20blog\">Qxf2<\/a>, we converted the above code to be a decorator and use it in our framework. We will not able to share that code until we get time to write about our framework. But if you are comfortable enough with Python, we suggest you convert this method into a decorator.<\/p>\n<p><strong>Note:<\/strong>Internet Explorer driver does not support retrieval of logs of any kind.<\/p>\n<p>And now browser console errors also are not to be spared :). Enjoy testing!<\/p>\n<p><strong>If you liked what you read, know more <a href=\"https:\/\/qxf2.com\/blog\/about-qxf2\/\">about Qxf2<\/a>.<\/strong><\/p>\n<hr \/>\n","protected":false},"excerpt":{"rendered":"<p>The browser console log, along with debugging also helps us capture errors that occurred when using a web application. Your developers will thank you when you include console errors in your bug reports. As part of running your GUI automation, it is a good habit to check and report the errors present in the browser&#8217;s console. In this post, I [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[113,18,30],"tags":[],"class_list":["post-4730","post","type-post","status-publish","format-standard","hentry","category-browser-console-logs","category-python","category-selenium"],"_links":{"self":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/4730","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/comments?post=4730"}],"version-history":[{"count":22,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/4730\/revisions"}],"predecessor-version":[{"id":5108,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/4730\/revisions\/5108"}],"wp:attachment":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/media?parent=4730"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/categories?post=4730"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/tags?post=4730"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}