0
0
Selenium Pythontesting~10 mins

Logging setup in Selenium Python - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test sets up logging for Selenium WebDriver actions and verifies that a log message is correctly recorded when the browser opens and navigates to a URL.

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

class TestLoggingSetup(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        # Configure logging
        logging.basicConfig(filename='test_log.log', level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
        cls.logger = logging.getLogger()

        # Setup Chrome driver
        cls.service = Service()
        cls.driver = webdriver.Chrome(service=cls.service)

    def test_open_google_and_log(self):
        self.logger.info('Test started: Opening Google homepage')
        self.driver.get('https://www.google.com')
        self.logger.info('Navigated to Google homepage')

        # Verify page title contains 'Google'
        title = self.driver.title
        self.logger.info(f'Page title is: {title}')
        self.assertIn('Google', title)

    @classmethod
    def tearDownClass(cls):
        cls.driver.quit()
        cls.logger.info('Test finished: Browser closed')

if __name__ == '__main__':
    unittest.main()
Execution Trace - 7 Steps
StepActionSystem StateAssertionResult
1Configure logging to write INFO level messages to 'test_log.log'Logging is set up with format including timestamp, level, and message-PASS
2Start Chrome WebDriverChrome browser window opens-PASS
3Log message: 'Test started: Opening Google homepage'Log file records the start message-PASS
4Navigate browser to 'https://www.google.com'Google homepage loads in browser-PASS
5Log message: 'Navigated to Google homepage'Log file records navigation message-PASS
6Get page title and log itPage title is retrieved and loggedVerify page title contains 'Google'PASS
7Close browser and log message: 'Test finished: Browser closed'Browser window closes, log file records finish message-PASS
Failure Scenario
Failing Condition: Page title does not contain 'Google' or browser fails to open
Execution Trace Quiz - 3 Questions
Test your understanding
What does the test verify after navigating to the Google homepage?
AThat the page title contains 'Google'
BThat the page URL contains 'google.com'
CThat the page has a search box
DThat the browser window is maximized
Key Result
Setting up logging in tests helps track test progress and diagnose issues by recording key actions and results.