0
0
Selenium Javatesting~15 mins

Data providers for parameterization in Selenium Java - Build an Automation Script

Choose your learning style9 modes available
Login functionality test with multiple user credentials
Preconditions (2)
Step 1: Navigate to 'https://example.com/login'.
Step 2: For each set of credentials:
Step 3: - Enter the username in the username input field with id 'username'.
Step 4: - Enter the password in the password input field with id 'password'.
Step 5: - Click the login button with id 'loginBtn'.
Step 6: - Verify the login result:
Step 7: * If credentials are valid, verify the URL changes to 'https://example.com/dashboard'.
Step 8: * If credentials are invalid, verify an error message with id 'errorMsg' is displayed.
✅ Expected Result: The login succeeds for valid credentials and the dashboard page is shown. For invalid credentials, an error message is displayed.
Automation Requirements - TestNG with Selenium WebDriver
Assertions Needed:
Verify URL after login attempt matches expected URL for valid credentials.
Verify error message is displayed for invalid credentials.
Best Practices:
Use @DataProvider annotation to supply multiple sets of credentials.
Use explicit waits to wait for page load or error message visibility.
Use Page Object Model to separate page interactions from test logic.
Use meaningful assertion messages.
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.DataProvider;
import org.testng.annotations.Test;

import java.time.Duration;

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

    @BeforeClass
    public void setUp() {
        driver = new ChromeDriver();
        wait = new WebDriverWait(driver, Duration.ofSeconds(10));
    }

    @DataProvider(name = "loginData")
    public Object[][] loginData() {
        return new Object[][] {
            {"validUser1", "validPass1", true},
            {"validUser2", "validPass2", true},
            {"invalidUser", "wrongPass", false}
        };
    }

    @Test(dataProvider = "loginData")
    public void testLogin(String username, String password, boolean isValid) {
        driver.get("https://example.com/login");

        WebElement usernameInput = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("username")));
        usernameInput.clear();
        usernameInput.sendKeys(username);

        WebElement passwordInput = driver.findElement(By.id("password"));
        passwordInput.clear();
        passwordInput.sendKeys(password);

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

        if (isValid) {
            boolean urlChanged = wait.until(ExpectedConditions.urlToBe("https://example.com/dashboard"));
            Assert.assertTrue(urlChanged, "Expected to be on dashboard page after valid login.");
        } else {
            WebElement errorMsg = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("errorMsg")));
            Assert.assertTrue(errorMsg.isDisplayed(), "Error message should be displayed for invalid login.");
        }
    }

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

This test class uses Selenium WebDriver with TestNG to automate login tests with multiple credentials.

setUp(): Initializes ChromeDriver and WebDriverWait before tests.

@DataProvider loginData(): Supplies three sets of username, password, and a boolean indicating if credentials are valid.

testLogin(): Runs once per data set. It navigates to the login page, enters username and password, clicks login, then verifies the result:

  • If credentials are valid, waits until URL changes to dashboard and asserts success.
  • If invalid, waits for error message visibility and asserts it is shown.

tearDown(): Closes the browser after all tests.

Explicit waits ensure elements are ready before interaction. Assertions have clear messages to help understand failures. Using @DataProvider allows easy parameterization of tests.

Common Mistakes - 4 Pitfalls
Hardcoding credentials inside the test method instead of using @DataProvider
Using Thread.sleep() instead of explicit waits
Not clearing input fields before sending keys
Using brittle locators like absolute XPath
Bonus Challenge

Now add data-driven testing with 3 different sets of user credentials including edge cases like empty username or password.

Show Hint