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.
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.
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(); } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts and WebDriver ChromeDriver instance is created | Browser window opens, ready for navigation | - | PASS |
| 2 | Reads CSV file 'login_data.csv' line by line to extract username and password pairs | CSV file is read successfully, data stored in memory | - | PASS |
| 3 | Navigates browser to 'https://example.com/login' | Login page is loaded with username, password inputs and login button visible | Check username and password input fields are displayed | PASS |
| 4 | Finds username input field and clears it, then enters username from CSV | Username input field contains the username value | - | PASS |
| 5 | Finds password input field and clears it, then enters password from CSV | Password input field contains the password value | - | PASS |
| 6 | Finds login button and verifies it is enabled | Login button is enabled and clickable | Login button is enabled | PASS |
| 7 | Clicks the login button | Login button clicked, page may navigate or respond | - | PASS |
| 8 | Repeats steps 3-7 for each username/password pair from CSV | All login attempts performed | - | PASS |
| 9 | Test ends and browser is closed | Browser window closed | - | PASS |