0
0
Selenium Javatesting~15 mins

Configuration management in Selenium Java - Build an Automation Script

Choose your learning style9 modes available
Verify login functionality using configuration management for test data
Preconditions (2)
Step 1: Open the browser and navigate to the login page URL
Step 2: Read username and password from the configuration file
Step 3: Enter the username into the username input field
Step 4: Enter the password into the password input field
Step 5: Click the login button
Step 6: Wait for the dashboard page to load
✅ Expected Result: User is successfully logged in and dashboard page is displayed
Automation Requirements - Selenium WebDriver with TestNG
Assertions Needed:
Verify the current URL contains '/dashboard' after login
Verify the dashboard page title is as expected
Best Practices:
Use Page Object Model to separate page locators and actions
Use explicit waits to wait for elements or page load
Load test data from an external configuration file (e.g., properties file)
Use TestNG annotations for setup and teardown
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.io.FileInputStream;
import java.io.IOException;
import java.time.Duration;
import java.util.Properties;

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

    @BeforeClass
    public void setUp() throws IOException {
        // Load configuration file
        config = new Properties();
        FileInputStream fis = new FileInputStream("config.properties");
        config.load(fis);
        fis.close();

        // Setup WebDriver
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        driver = new ChromeDriver();
        wait = new WebDriverWait(driver, Duration.ofSeconds(10));

        // Open login page
        driver.get(config.getProperty("login.url"));
    }

    @Test
    public void testLogin() {
        // Locate username field and enter username
        WebElement usernameInput = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("username")));
        usernameInput.sendKeys(config.getProperty("login.username"));

        // Locate password field and enter password
        WebElement passwordInput = driver.findElement(By.id("password"));
        passwordInput.sendKeys(config.getProperty("login.password"));

        // Click login button
        WebElement loginButton = driver.findElement(By.id("loginBtn"));
        loginButton.click();

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

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

        String expectedTitle = config.getProperty("dashboard.title");
        String actualTitle = driver.getTitle();
        Assert.assertEquals(actualTitle, expectedTitle, "Dashboard page title mismatch");
    }

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

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

Setup: The @BeforeClass method loads the configuration file config.properties which contains the login URL, username, password, and expected dashboard title. It then initializes the ChromeDriver and opens the login page.

Test: The test method waits explicitly for the username field to be visible, then enters the username and password read from the config file. It clicks the login button and waits until the URL contains '/dashboard' to confirm navigation.

Assertions: It verifies the URL contains '/dashboard' and the page title matches the expected dashboard title from the config file.

Teardown: The @AfterClass method closes the browser to clean up.

This approach follows best practices by separating configuration data, using explicit waits, and structuring code with TestNG annotations.

Common Mistakes - 4 Pitfalls
Hardcoding test data like username and password inside the test code
Using Thread.sleep() instead of explicit waits
Locating elements with brittle XPath expressions
Not closing the browser after tests
Bonus Challenge

Now add data-driven testing with 3 different username and password combinations from the configuration file

Show Hint