Test Overview
This test demonstrates how separating test data from test logic helps improve test coverage by allowing multiple data sets to be tested easily without changing the test code.
This test demonstrates how separating test data from test logic helps improve test coverage by allowing multiple data sets to be tested easily without changing the test code.
import org.junit.jupiter.api.Test; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import static org.junit.jupiter.api.Assertions.assertEquals; public class LoginTest { // Test data separated from test logic private static final String[][] testData = { {"user1", "pass1", "Welcome user1!"}, {"user2", "pass2", "Welcome user2!"}, {"user3", "wrongpass", "Invalid credentials"} }; @Test public void testLoginWithMultipleData() { System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver"); WebDriver driver = new ChromeDriver(); for (String[] data : testData) { String username = data[0]; String password = data[1]; String expectedMessage = data[2]; driver.get("https://example.com/login"); WebElement userField = driver.findElement(By.id("username")); WebElement passField = driver.findElement(By.id("password")); WebElement loginButton = driver.findElement(By.id("loginBtn")); userField.clear(); userField.sendKeys(username); passField.clear(); passField.sendKeys(password); loginButton.click(); WebElement message = driver.findElement(By.id("message")); assertEquals(expectedMessage, message.getText()); } driver.quit(); } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts and Chrome browser opens | Chrome browser window is open and ready | - | PASS |
| 2 | Navigates to https://example.com/login for first data set | Login page is loaded with username and password fields | - | PASS |
| 3 | Finds username and password input fields and login button | Input fields and button are visible and interactable | - | PASS |
| 4 | Enters username 'user1' and password 'pass1', clicks login | Form submitted, waiting for response message | - | PASS |
| 5 | Finds message element and verifies text equals 'Welcome user1!' | Message displayed on page | Assert message text == 'Welcome user1!' | PASS |
| 6 | Repeats navigation and input for second data set: 'user2', 'pass2' | Login page loaded again | - | PASS |
| 7 | Enters username 'user2' and password 'pass2', clicks login | Form submitted, waiting for response message | - | PASS |
| 8 | Verifies message text equals 'Welcome user2!' | Message displayed on page | Assert message text == 'Welcome user2!' | PASS |
| 9 | Repeats navigation and input for third data set: 'user3', 'wrongpass' | Login page loaded again | - | PASS |
| 10 | Enters username 'user3' and password 'wrongpass', clicks login | Form submitted, waiting for response message | - | PASS |
| 11 | Verifies message text equals 'Invalid credentials' | Error message displayed on page | Assert message text == 'Invalid credentials' | PASS |
| 12 | Test ends and browser closes | Browser window closed | - | PASS |