</>

Technology

Selenium

Difficulty

Intermediate

Interview Question

What is the use of logging in test automation?

Logging captures the runtime behavior of tests, helps debug failures, and creates an audit trail of test execution without re-running tests.

Answer

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:

CODE
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:

Java
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:

Java
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: DEBUG level (verbose)
  • CI pipeline: INFO or WARN level (less noise)
  • Production monitoring: ERROR level only

Best practice in Selenium framework:

Java
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

Follow AutomateQA

Related Topics