0
0
Selenium Javatesting~15 mins

Why reports communicate test results in Selenium Java - Automation Benefits in Action

Choose your learning style9 modes available
Verify test report shows correct test results after login test
Preconditions (2)
Step 1: Open the login page
Step 2: Enter 'admin@test.com' in the email field
Step 3: Enter 'Pass123!' in the password field
Step 4: Click the Login button
Step 5: Verify the dashboard page is displayed
Step 6: Generate the test report after test execution
✅ Expected Result: The test report should show the login test as passed with details of steps executed
Automation Requirements - Selenium with TestNG
Assertions Needed:
Verify dashboard page URL after login
Verify test report contains the login test result as passed
Best Practices:
Use explicit waits to wait for dashboard page
Use Page Object Model for page interactions
Generate and save test report automatically after test run
Automated Solution
Selenium Java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import java.time.Duration;

public class LoginTest {
    private WebDriver driver;
    private WebDriverWait wait;

    @BeforeClass
    public void setUp() {
        System.setProperty("webdriver.chrome.driver", "./chromedriver");
        driver = new ChromeDriver();
        wait = new WebDriverWait(driver, Duration.ofSeconds(10));
    }

    @Test
    public void testValidLogin() {
        driver.get("https://example.com/login");

        WebElement emailField = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("email")));
        emailField.sendKeys("admin@test.com");

        WebElement passwordField = driver.findElement(By.id("password"));
        passwordField.sendKeys("Pass123!");

        WebElement loginButton = driver.findElement(By.id("loginBtn"));
        loginButton.click();

        // Wait for dashboard page URL
        wait.until(ExpectedConditions.urlContains("/dashboard"));

        String currentUrl = driver.getCurrentUrl();
        Assert.assertTrue(currentUrl.contains("/dashboard"), "Dashboard page should be displayed after login");
    }

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

This test script uses Selenium WebDriver with TestNG framework to automate the login test.

setUp() initializes the ChromeDriver and explicit wait.

testValidLogin() opens the login page, enters the given email and password, clicks login, and waits until the dashboard page URL appears. Then it asserts the URL contains '/dashboard' to confirm successful login.

tearDown() closes the browser after the test.

TestNG automatically generates a test report after execution showing if the test passed or failed. This report communicates the test result clearly to stakeholders.

Common Mistakes - 3 Pitfalls
Using Thread.sleep() instead of explicit waits
Hardcoding XPath selectors that are brittle
Not closing the browser after test
Bonus Challenge

Now add data-driven testing with 3 different sets of login credentials (valid and invalid) to verify login behavior.

Show Hint