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()
| Aspect | Reporter.log() | System.out.println() |
|---|---|---|
| Appears in TestNG report | Yes | No |
| Appears in console | Only if second arg is true | Yes |
| Per-test association | Yes — tied to current test | No |
| HTML formatting | Yes | No |
| ExtentReports | Separate — use .info() | No |
