0
0
Selenium Javatesting~15 mins

TestNG default reports in Selenium Java - Build an Automation Script

Choose your learning style9 modes available
Verify TestNG default report generation after test execution
Preconditions (2)
Step 1: Run the TestNG test suite using the testng.xml or from the IDE
Step 2: Wait for the test execution to complete
Step 3: Locate the TestNG default report folder named 'test-output' in the project directory
Step 4: Open the 'index.html' file inside the 'test-output' folder in a web browser
Step 5: Verify that the report shows the test suite name, test results summary, and details of passed and failed tests
✅ Expected Result: TestNG default report is generated in 'test-output/index.html' showing correct test execution summary and details
Automation Requirements - TestNG with Selenium WebDriver
Assertions Needed:
Verify test method executed successfully
Verify test report folder 'test-output' is created
Verify 'index.html' file exists inside 'test-output'
Verify test result summary contains expected test method name and status
Best Practices:
Use TestNG annotations for setup and teardown
Use assertions from TestNG Assert class
Use explicit waits for Selenium actions if needed
Keep test methods independent
Run tests via TestNG suite XML or IDE runner
Automated Solution
Selenium Java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.io.File;

public class TestNGDefaultReportTest {
    WebDriver driver;

    @BeforeClass
    public void setup() {
        // Set path to chromedriver executable if needed
        // System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        driver = new ChromeDriver();
    }

    @Test
    public void openGoogleAndCheckTitle() {
        driver.get("https://www.google.com");
        String title = driver.getTitle();
        Assert.assertTrue(title.contains("Google"), "Title should contain 'Google'");
    }

    @AfterClass
    public void teardown() {
        if (driver != null) {
            driver.quit();
        }
    }

    @Test
    public void verifyTestNGReportExists() {
        // This test verifies that TestNG default report is generated after tests run
        File reportFolder = new File("test-output");
        Assert.assertTrue(reportFolder.exists() && reportFolder.isDirectory(), "TestNG report folder 'test-output' should exist");

        File indexFile = new File(reportFolder, "index.html");
        Assert.assertTrue(indexFile.exists() && indexFile.isFile(), "TestNG report 'index.html' should exist inside 'test-output'");
    }
}

The code defines a TestNG test class with Selenium WebDriver.

setup() method initializes ChromeDriver before tests.

openGoogleAndCheckTitle() navigates to Google and asserts the page title contains 'Google'.

teardown() quits the browser after tests.

verifyTestNGReportExists() checks if the 'test-output' folder and 'index.html' report file exist after test execution.

This confirms that TestNG default reports are generated automatically after running tests.

Assertions use TestNG's Assert class to validate conditions.

Running this test class via TestNG will produce the default HTML report in 'test-output' folder.

Common Mistakes - 4 Pitfalls
Not quitting the WebDriver after tests
Checking for report files before test execution completes
{'mistake': 'Hardcoding absolute paths for report folder', 'why_bad': 'Makes tests less portable and harder to run on different machines', 'correct_approach': "Use relative paths like 'test-output' which TestNG creates in project root"}
Not using TestNG assertions for verification
Bonus Challenge

Now add data-driven testing with 3 different URLs and verify their page titles

Show Hint