</>

Technology

TestNG

Difficulty

Beginner

Interview Question

What is Reporter.log() in TestNG and how do you use it?

Reporter.log() adds custom log messages to TestNG HTML reports at the test-method level — useful for step-by-step execution details in the built-in report.

Answer

Reporter.log() in TestNG

Reporter.log() writes messages that appear in TestNG''s built-in HTML report (index.html → test method details). Each log entry is tied to the current test method.

Basic Usage

Java
import org.testng.Reporter;

@Test
public void loginTest() {
    Reporter.log("Step 1: Navigating to login page");
    driver.get("https://automateqa.online/login");

    Reporter.log("Step 2: Entering username");
    driver.findElement(By.id("username")).sendKeys("admin");

    Reporter.log("Step 3: Entering password");
    driver.findElement(By.id("password")).sendKeys("secret");

    Reporter.log("Step 4: Clicking login button");
    driver.findElement(By.id("loginBtn")).click();

    Assert.assertTrue(driver.getTitle().contains("Dashboard"),
        "Login failed — expected Dashboard");

    Reporter.log("Step 5: PASSED — Dashboard loaded successfully");
}

Log to Console AND Report Simultaneously

The second argument true echoes the message to System.out as well:

Java
Reporter.log("Clicking submit button", true); // goes to report AND console
Reporter.log("Element found", false);          // goes to report only

Log with HTML Formatting

Reporter.log supports HTML:

Java
Reporter.log("<b>Step 1:</b> Login page loaded ✓");
Reporter.log("<span style='color:green'>PASS: Dashboard visible</span>");
Reporter.log("<pre>" + driver.getPageSource().substring(0, 200) + "</pre>");

Reusable Step Logger Utility

Java
public class StepLogger {

    public static void step(String stepDescription) {
        Reporter.log("<b>➤ " + stepDescription + "</b>", true);
    }

    public static void pass(String message) {
        Reporter.log("<span style='color:#28a745'>✓ PASS: " + message + "</span>", true);
    }

    public static void fail(String message) {
        Reporter.log("<span style='color:#dc3545'>✗ FAIL: " + message + "</span>", true);
    }

    public static void info(String message) {
        Reporter.log("<span style='color:#6c757d'>ℹ " + message + "</span>", true);
    }
}

// In tests:
StepLogger.step("Navigate to checkout page");
StepLogger.pass("Cart total displayed correctly");
StepLogger.fail("Payment button not clickable");

Access Logged Messages Programmatically

Java
// In a listener or reporter — get all logs for a test
List<String> logs = Reporter.getOutput(testResult);
logs.forEach(System.out::println);

Reporter.log() vs System.out.println()

AspectReporter.log()System.out.println()
Appears in TestNG reportYesNo
Appears in consoleOnly if second arg is trueYes
Per-test associationYes — tied to current testNo
HTML formattingYesNo
ExtentReportsSeparate — use .info()No

Follow AutomateQA

Related Topics