0
0
Selenium Pythontesting~10 mins

Browser console log capture in Selenium Python - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test opens a web page using Selenium in Python, captures the browser console logs, and verifies that there are no severe errors in the logs.

Test Code - unittest
Selenium Python
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
import unittest

class TestBrowserConsoleLog(unittest.TestCase):
    def setUp(self):
        chrome_options = Options()
        chrome_options.set_capability('goog:loggingPrefs', {'browser': 'ALL'})
        self.driver = webdriver.Chrome(service=Service(), options=chrome_options)

    def test_console_log_no_severe_errors(self):
        self.driver.get('https://example.com')
        logs = self.driver.get_log('browser')
        severe_errors = [entry for entry in logs if entry['level'] == 'SEVERE']
        self.assertEqual(len(severe_errors), 0, f"Found severe errors in console logs: {severe_errors}")

    def tearDown(self):
        self.driver.quit()

if __name__ == '__main__':
    unittest.main()
Execution Trace - 6 Steps
StepActionSystem StateAssertionResult
1Test starts and Chrome browser is launched with logging preferences set to capture browser console logsChrome browser window opens with logging enabled for browser console-PASS
2Navigates to https://example.comBrowser displays the Example Domain webpage-PASS
3Retrieves browser console logs using driver.get_log('browser')Console logs are collected as a list of log entries-PASS
4Filters logs to find entries with level 'SEVERE'Filtered list contains only severe error log entries (empty if none found)Check that the number of severe errors is zeroPASS
5Assertion checks that no severe errors exist in console logsTest verifies no severe errors in browser consoleassertEqual(len(severe_errors), 0)PASS
6Browser is closed and test endsBrowser window closes, resources cleaned up-PASS
Failure Scenario
Failing Condition: There are one or more severe errors in the browser console logs
Execution Trace Quiz - 3 Questions
Test your understanding
What does the test check in the browser console logs?
AThat the browser window is maximized
BThat the page title is correct
CThat there are no severe errors
DThat the page contains a specific element
Key Result
Capturing and checking browser console logs helps catch JavaScript errors early, improving test reliability and debugging.