0
0
Selenium Javatesting~10 mins

JSON test data in Selenium Java - Test Execution Trace

Choose your learning style9 modes available
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.

Test Code - TestNG with Selenium WebDriver
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.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();
        }
    }
}
Execution Trace - 8 Steps
StepActionSystem StateAssertionResult
1Test setup: ChromeDriver initialized and JSON test data loaded from fileBrowser window opened and maximized; JSON data with URL and credentials loaded-PASS
2Browser navigates to URL from JSON dataBrowser displays login page at specified URL-PASS
3Find username, password fields and login button by their IDsAll elements located successfully on the login page-PASS
4Enter username and password from JSON data into input fieldsUsername and password fields filled with test data-PASS
5Click login buttonLogin form submitted; page transitions to user dashboard-PASS
6Find welcome message element by IDWelcome message element present on dashboard page-PASS
7Assert welcome message is displayed and text matches expected usernameWelcome message visible with text 'Welcome, testuser!'Assert.assertTrue(welcomeMessage.isDisplayed()) and Assert.assertEquals(welcomeMessage.getText(), 'Welcome, testuser!')PASS
8Test teardown: Browser closedBrowser window closed, WebDriver quit-PASS
Failure Scenario
Failing Condition: If the JSON file path is incorrect or JSON structure is invalid, or if elements are not found on the page
Execution Trace Quiz - 3 Questions
Test your understanding
What is the source of the username and password used in the test?
AThey are entered manually during test execution
BThey are read from a JSON file before the test runs
CThey are hardcoded in the test code
DThey are fetched from a database during the test
Key Result
Using JSON test data separates test logic from test data, making tests easier to maintain and reuse with different inputs.