0
0
Selenium Javatesting~15 mins

Logging test steps in Selenium Java - Build an Automation Script

Choose your learning style9 modes available
Verify login functionality with step logging
Preconditions (2)
Step 1: Open the login page URL
Step 2: Log step: 'Opened login page'
Step 3: Enter 'testuser@example.com' in the email input field
Step 4: Log step: 'Entered email address'
Step 5: Enter 'Test@1234' in the password input field
Step 6: Log step: 'Entered password'
Step 7: Click the login button
Step 8: Log step: 'Clicked login button'
Step 9: Verify that the user is redirected to the dashboard page
Step 10: Log step: 'Verified dashboard page is displayed'
✅ Expected Result: User successfully logs in and dashboard page is displayed with all steps logged
Automation Requirements - Selenium WebDriver with TestNG
Assertions Needed:
Verify current URL contains '/dashboard'
Verify page title is 'Dashboard'
Best Practices:
Use explicit waits to wait for elements
Use meaningful locators (By.id, By.name)
Log each test step clearly using a logger
Use TestNG assertions for validation
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;
import java.util.logging.Logger;

public class LoginTestWithLogging {
    private WebDriver driver;
    private WebDriverWait wait;
    private final Logger logger = Logger.getLogger(LoginTestWithLogging.class.getName());

    @BeforeClass
    public void setUp() {
        // Set path to chromedriver if needed
        driver = new ChromeDriver();
        wait = new WebDriverWait(driver, Duration.ofSeconds(10));
        logger.info("Starting test setup");
    }

    @Test
    public void testLoginWithStepLogging() {
        driver.get("https://example.com/login");
        logger.info("Opened login page");

        WebElement emailInput = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("email")));
        emailInput.sendKeys("testuser@example.com");
        logger.info("Entered email address");

        WebElement passwordInput = driver.findElement(By.id("password"));
        passwordInput.sendKeys("Test@1234");
        logger.info("Entered password");

        WebElement loginButton = driver.findElement(By.id("loginBtn"));
        loginButton.click();
        logger.info("Clicked login button");

        wait.until(ExpectedConditions.urlContains("/dashboard"));
        logger.info("Verified dashboard page is displayed");

        String currentUrl = driver.getCurrentUrl();
        Assert.assertTrue(currentUrl.contains("/dashboard"), "URL does not contain '/dashboard'");

        String pageTitle = driver.getTitle();
        Assert.assertEquals(pageTitle, "Dashboard", "Page title is not 'Dashboard'");
    }

    @AfterClass
    public void tearDown() {
        if (driver != null) {
            driver.quit();
            logger.info("Closed browser and ended test");
        }
    }
}

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

We start by opening the login page and log this step using logger.info.

We wait explicitly for the email input to be visible before entering the email, then log the step.

We locate the password input and enter the password, logging the step.

We click the login button and log that action.

We wait until the URL contains '/dashboard' to confirm navigation, then log the verification.

Assertions check that the URL and page title are correct to confirm successful login.

The @BeforeClass and @AfterClass methods set up and clean up the browser session.

Logging each step helps track test progress and makes debugging easier if something fails.

Common Mistakes - 4 Pitfalls
Using Thread.sleep() instead of explicit waits
Using brittle XPath locators like absolute paths
Not logging test steps
Mixing assertion styles or not asserting at all
Bonus Challenge

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

Show Hint