This Java Selenium test reads login data from a JSON file and uses it to fill a login form. It then checks if login succeeded by looking at the page title.
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.json.JSONObject;
import java.nio.file.Files;
import java.nio.file.Paths;
public class JsonTestDataExample {
public static void main(String[] args) throws Exception {
// Read JSON file content
String content = new String(Files.readAllBytes(Paths.get("testdata.json")));
JSONObject json = new JSONObject(content);
// Extract test data
String username = json.getString("username");
String password = json.getString("password");
// Setup WebDriver (assumes chromedriver is in system PATH)
WebDriver driver = new ChromeDriver();
driver.get("https://example.com/login");
// Fill login form using JSON data
driver.findElement(By.id("usernameInput")).sendKeys(username);
driver.findElement(By.id("passwordInput")).sendKeys(password);
driver.findElement(By.id("loginButton")).click();
// Simple check: page title contains 'Dashboard' after login
String title = driver.getTitle();
if (title.contains("Dashboard")) {
System.out.println("Test Passed: Login successful.");
} else {
System.out.println("Test Failed: Login unsuccessful.");
}
driver.quit();
}
}