0
0
Selenium Javatesting~10 mins

CSV data reading in Selenium Java - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test reads login data from a CSV file and uses it to perform login attempts on a web page. It verifies that the login page loads correctly and that the login button is clickable.

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.DataProvider;
import org.testng.annotations.Test;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;

public class CsvDataLoginTest {
    WebDriver driver;

    @BeforeClass
    public void setup() {
        driver = new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
    }

    @DataProvider(name = "loginData")
    public Object[][] readCsvData() throws IOException {
        String path = "src/test/resources/login_data.csv";
        BufferedReader br = new BufferedReader(new FileReader(path));
        List<String[]> records = new ArrayList<>();
        String line;
        while ((line = br.readLine()) != null) {
            String[] fields = line.split(",");
            records.add(fields);
        }
        br.close();
        Object[][] data = new Object[records.size()][2];
        for (int i = 0; i < records.size(); i++) {
            data[i][0] = records.get(i)[0]; // username
            data[i][1] = records.get(i)[1]; // password
        }
        return data;
    }

    @Test(dataProvider = "loginData")
    public void testLogin(String username, String password) {
        driver.get("https://example.com/login");
        WebElement usernameInput = driver.findElement(By.id("username"));
        WebElement passwordInput = driver.findElement(By.id("password"));
        WebElement loginButton = driver.findElement(By.id("loginBtn"));

        Assert.assertTrue(usernameInput.isDisplayed(), "Username input should be visible");
        Assert.assertTrue(passwordInput.isDisplayed(), "Password input should be visible");

        usernameInput.clear();
        usernameInput.sendKeys(username);
        passwordInput.clear();
        passwordInput.sendKeys(password);

        Assert.assertTrue(loginButton.isEnabled(), "Login button should be enabled");
        loginButton.click();

        // For this example, we only verify that login button was clicked without error
    }

    @AfterClass
    public void teardown() {
        if (driver != null) {
            driver.quit();
        }
    }
Execution Trace - 9 Steps
StepActionSystem StateAssertionResult
1Test starts and WebDriver ChromeDriver instance is createdBrowser window opens, ready for navigation-PASS
2Reads CSV file 'login_data.csv' line by line to extract username and password pairsCSV file is read successfully, data stored in memory-PASS
3Navigates browser to 'https://example.com/login'Login page is loaded with username, password inputs and login button visibleCheck username and password input fields are displayedPASS
4Finds username input field and clears it, then enters username from CSVUsername input field contains the username value-PASS
5Finds password input field and clears it, then enters password from CSVPassword input field contains the password value-PASS
6Finds login button and verifies it is enabledLogin button is enabled and clickableLogin button is enabledPASS
7Clicks the login buttonLogin button clicked, page may navigate or respond-PASS
8Repeats steps 3-7 for each username/password pair from CSVAll login attempts performed-PASS
9Test ends and browser is closedBrowser window closed-PASS
Failure Scenario
Failing Condition: CSV file path is incorrect or file is missing
Execution Trace Quiz - 3 Questions
Test your understanding
What does the test verify before entering login credentials?
AThat username and password input fields are visible
BThat the login button is clicked
CThat the page URL contains 'dashboard'
DThat the browser window is maximized
Key Result
Using a DataProvider to read CSV data allows running the same test multiple times with different inputs, improving test coverage and maintainability.