Test Overview
This test reads user login data from a JSON file and verifies the login functionality on a web page using Selenium in Java.
This test reads user login data from a JSON file and verifies the login functionality on a web page using Selenium in Java.
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.Assert; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import java.io.File; public class LoginTestWithJson { WebDriver driver; JsonNode testData; @BeforeClass public void setUp() throws Exception { System.setProperty("webdriver.chrome.driver", "path/to/chromedriver"); driver = new ChromeDriver(); driver.manage().window().maximize(); ObjectMapper mapper = new ObjectMapper(); testData = mapper.readTree(new File("src/test/resources/loginData.json")); } @Test public void testLogin() { String url = testData.get("url").asText(); String username = testData.get("credentials").get("username").asText(); String password = testData.get("credentials").get("password").asText(); driver.get(url); WebElement usernameField = driver.findElement(By.id("username")); WebElement passwordField = driver.findElement(By.id("password")); WebElement loginButton = driver.findElement(By.id("loginBtn")); usernameField.sendKeys(username); passwordField.sendKeys(password); loginButton.click(); WebElement welcomeMessage = driver.findElement(By.id("welcomeMsg")); Assert.assertTrue(welcomeMessage.isDisplayed(), "Welcome message should be visible after login"); Assert.assertEquals(welcomeMessage.getText(), "Welcome, " + username + "!"); } @AfterClass public void tearDown() { if (driver != null) { driver.quit(); } } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test setup: ChromeDriver initialized and JSON test data loaded from file | Browser window opened and maximized; JSON data with URL and credentials loaded | - | PASS |
| 2 | Browser navigates to URL from JSON data | Browser displays login page at specified URL | - | PASS |
| 3 | Find username, password fields and login button by their IDs | All elements located successfully on the login page | - | PASS |
| 4 | Enter username and password from JSON data into input fields | Username and password fields filled with test data | - | PASS |
| 5 | Click login button | Login form submitted; page transitions to user dashboard | - | PASS |
| 6 | Find welcome message element by ID | Welcome message element present on dashboard page | - | PASS |
| 7 | Assert welcome message is displayed and text matches expected username | Welcome message visible with text 'Welcome, testuser!' | Assert.assertTrue(welcomeMessage.isDisplayed()) and Assert.assertEquals(welcomeMessage.getText(), 'Welcome, testuser!') | PASS |
| 8 | Test teardown: Browser closed | Browser window closed, WebDriver quit | - | PASS |