Logging setup in Selenium Python - Build an Automation Script
import unittest import logging import os from selenium import webdriver from selenium.webdriver.chrome.service import Service from selenium.webdriver.chrome.options import Options class TestLoggingSetup(unittest.TestCase): @classmethod def setUpClass(cls): # Configure logging logging.basicConfig( filename='test.log', level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s', filemode='w' ) def setUp(self): chrome_options = Options() chrome_options.add_argument('--headless') # Run headless for test automation service = Service() self.driver = webdriver.Chrome(service=service, options=chrome_options) def test_logging(self): logging.info('Test started') self.driver.get('https://example.com') logging.info('Page opened successfully') self.assertIn('Example Domain', self.driver.title) def tearDown(self): self.driver.quit() logging.info('Test finished') @classmethod def tearDownClass(cls): # Verify log file content assert os.path.exists('test.log'), 'Log file does not exist' with open('test.log', 'r') as f: log_content = f.read() assert 'Test started' in log_content, 'Missing "Test started" log' assert 'Page opened successfully' in log_content, 'Missing "Page opened successfully" log' assert 'Test finished' in log_content, 'Missing "Test finished" log' if __name__ == '__main__': unittest.main()
This test script uses Python's unittest framework to organize the test.
In setUpClass, logging is configured to write INFO level messages to test.log with timestamps.
The setUp method starts a headless Chrome browser for automation.
The test method logs 'Test started', opens the webpage, logs 'Page opened successfully', and asserts the page title contains 'Example Domain' to confirm navigation.
The tearDown method closes the browser and logs 'Test finished'.
Finally, tearDownClass verifies the log file exists and contains all expected messages in order.
This structure ensures logging captures key test steps and the test verifies the logging output as expected.
Now add data-driven testing with 3 different URLs and verify logging for each
Practice
Solution
Step 1: Understand logging purpose
Logging is used to keep track of events and errors that happen during tests.Step 2: Compare options
Only recording events and errors matches logging's main role; speeding up or fixing code is unrelated.Final Answer:
To record important events and errors during test execution -> Option AQuick Check:
Logging = Record events/errors [OK]
- Thinking logging speeds up tests
- Confusing logging with test fixing tools
- Assuming logging creates reports automatically
Solution
Step 1: Recall logging setup syntax
Use logging.basicConfig() with level=logging.DEBUG to set debug level.Step 2: Validate options
Only logging.basicConfig(level=logging.DEBUG) uses correct function and syntax; others are invalid or incorrect method calls.Final Answer:
logging.basicConfig(level=logging.DEBUG) -> Option CQuick Check:
basicConfig + level=logging.DEBUG = Correct [OK]
- Using logging.setLevel instead of basicConfig
- Passing level as string without logging prefix
- Calling non-existent logging.start() method
import logging
logging.basicConfig(level=logging.INFO)
logging.debug('Debug message')
logging.info('Info message')Solution
Step 1: Understand logging level effect
Setting level to INFO means messages below INFO (like DEBUG) are ignored.Step 2: Check which messages print
DEBUG message is ignored; INFO message is printed.Final Answer:
Info message -> Option DQuick Check:
Level INFO hides DEBUG, shows INFO [OK]
- Expecting DEBUG messages to show at INFO level
- Confusing logging levels order
- Assuming all messages print regardless of level
import logging
logging.basicConfig(level='DEBUG')
logging.debug('Test message')Solution
Step 1: Check logging.basicConfig level argument type
The level argument must be a logging level constant like logging.DEBUG, not a string.Step 2: Validate other options
logging.debug() can be called after basicConfig; filename is optional; code errors due to string level.Final Answer:
The level argument should be an integer, not a string -> Option BQuick Check:
Level must be logging.DEBUG, not 'DEBUG' string [OK]
- Passing level as string instead of logging constant
- Thinking filename is mandatory in basicConfig
- Calling logging.debug before configuration causes error
test.log with timestamps and message levels. Which code snippet correctly sets this up?Solution
Step 1: Identify correct parameters for file logging
Use filename='test.log', level=logging.INFO, and correct format string with asctime and levelname.Step 2: Check each option for correctness
logging.basicConfig(filename='test.log', level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') uses correct parameter names and format; A uses wrong keys and strings; C uses non-existent logging.setup; D misses timestamps and uses DEBUG level.Final Answer:
logging.basicConfig(filename='test.log', level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') -> Option AQuick Check:
filename + level=logging.INFO + correct format = logging.basicConfig(filename='test.log', level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') [OK]
- Using wrong parameter names like file instead of filename
- Passing level as string instead of logging constant
- Missing timestamp in format string
Ready to prove it?
Pass the test and this pattern moves unknown
