0
0
Selenium Javatesting~15 mins

JSON test data in Selenium Java - Build an Automation Script

Choose your learning style9 modes available
Login form test using JSON test data
Preconditions (2)
Step 1: Open the login page URL
Step 2: Read the username and password from the JSON file
Step 3: Enter the username into the username input field with id 'username'
Step 4: Enter the password into the password input field with id 'password'
Step 5: Click the login button with id 'loginBtn'
Step 6: Wait for the page to load and verify the URL contains '/dashboard'
✅ Expected Result: User is successfully logged in and redirected to the dashboard page
Automation Requirements - Selenium WebDriver with Java
Assertions Needed:
Verify the current URL contains '/dashboard' after login
Best Practices:
Use explicit waits to wait for page load or element visibility
Use Page Object Model to separate page elements and actions
Read test data from external JSON file using a JSON parsing library
Use meaningful element locators like By.id or By.name
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 com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File;
import java.time.Duration;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class LoginTest {
    public static void main(String[] args) throws Exception {
        // Setup WebDriver
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        WebDriver driver = new ChromeDriver();
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));

        try {
            // Open login page
            driver.get("http://example.com/login");

            // Read JSON test data
            ObjectMapper mapper = new ObjectMapper();
            JsonNode testData = mapper.readTree(new File("src/test/resources/loginData.json"));
            String username = testData.get("username").asText();
            String password = testData.get("password").asText();

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

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

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

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

            // Assert URL contains /dashboard
            String currentUrl = driver.getCurrentUrl();
            assertTrue(currentUrl.contains("/dashboard"), "User should be redirected to dashboard after login");

            System.out.println("Test passed: User logged in successfully.");
        } finally {
            driver.quit();
        }
    }
}

This test script uses Selenium WebDriver with Java to automate the login test using JSON test data.

First, it sets up the ChromeDriver and opens the login page URL.

Then it reads the username and password from an external JSON file using Jackson's ObjectMapper.

It waits explicitly for the username input to be visible before entering the username and password.

After clicking the login button, it waits until the URL contains '/dashboard' to confirm successful login.

Finally, it asserts that the current URL contains '/dashboard' and prints a success message.

The driver quits in the finally block to ensure cleanup.

Common Mistakes - 4 Pitfalls
Using Thread.sleep() instead of explicit waits
Hardcoding test data inside the test code
Using brittle XPath locators instead of stable IDs
Not closing the WebDriver after test execution
Bonus Challenge

Now add data-driven testing with 3 different username and password combinations from JSON array

Show Hint