Use of Logging in Test Automation
Logging is the practice of recording test execution events, actions, and errors to an output destination (console, file, or database).
Primary purposes:
1. Debugging Test Failures When a test fails in CI/CD, you can''t always reproduce it locally. Logs capture exactly what happened:
INFO - Navigating to: https://example.com/login
INFO - Entering username: testuser
ERROR - Element not found: By.id("loginBtn") — NoSuchElementException
INFO - Screenshot captured: screenshots/loginTest_2024-01-15.png
2. Audit Trail of Test Execution Logs provide a timestamped history of what each test did:
log.info("Test started: " + testName);
log.info("Browser launched: Chrome");
log.info("URL navigated: " + url);
log.info("Test result: PASSED");
3. Performance Monitoring Track how long operations take:
long start = System.currentTimeMillis();
driver.findElement(By.id("search")).sendKeys("testNG");
log.debug("Search input took: " + (System.currentTimeMillis() - start) + "ms");
4. Filtering in CI Environments Use log levels to control verbosity:
- ✓Development:
DEBUGlevel (verbose) - ✓CI pipeline:
INFOorWARNlevel (less noise) - ✓Production monitoring:
ERRORlevel only
Best practice in Selenium framework:
public class BaseTest {
protected static final Logger log = LogManager.getLogger(BaseTest.class);
@BeforeMethod
public void setup(Method method) {
log.info("=== Starting test: " + method.getName() + " ===");
driver = new ChromeDriver();
}
@AfterMethod
public void teardown(ITestResult result) {
if (result.getStatus() == ITestResult.FAILURE) {
log.error("Test FAILED: " + result.getName());
log.error("Cause: " + result.getThrowable().getMessage());
captureScreenshot(result.getName());
}
driver.quit();
log.info("=== Test finished: " + result.getName() + " ===");
}
}
Summary:
- ✓Logging helps debug tests when failures occur
- ✓Provides a storage of test runtime behavior
- ✓Essential for diagnosing intermittent failures in CI/CD
- ✓Works alongside TestNG Listeners for comprehensive reporting
