Test Overview
This test uses a DataProvider to supply multiple sets of login credentials from an external source. It verifies that the login page accepts valid credentials and shows a welcome message after login.
This test uses a DataProvider to supply multiple sets of login credentials from an external source. It verifies that the login page accepts valid credentials and shows a welcome message after login.
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; public class LoginTest { WebDriver driver; @BeforeClass public void setUp() { driver = new ChromeDriver(); driver.manage().window().maximize(); } @DataProvider(name = "loginData") public Object[][] getData() { // Simulate external data source, e.g., CSV or DB return new Object[][] { {"user1", "pass1"}, {"user2", "pass2"} }; } @Test(dataProvider = "loginData") public void testLogin(String username, String password) { 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 welcomeMsg = driver.findElement(By.id("welcomeMessage")); Assert.assertTrue(welcomeMsg.isDisplayed(), "Welcome message should be displayed after login"); } @AfterClass public void tearDown() { if (driver != null) { driver.quit(); } } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts and Chrome browser opens | Chrome browser window is maximized and ready | - | PASS |
| 2 | Navigates to https://example.com/login | Login page is loaded with username, password fields and login button | - | PASS |
| 3 | Finds username and password input fields and login button | All elements are present and interactable | - | PASS |
| 4 | Enters username 'user1' and password 'pass1', then clicks login | Login form submitted | - | PASS |
| 5 | Finds welcome message element after login | Welcome message is displayed on the page | Assert welcome message is displayed | PASS |
| 6 | Repeats steps 2-5 with username 'user2' and password 'pass2' | Login page reloads and accepts second credentials | Assert welcome message is displayed | PASS |
| 7 | Test ends and browser closes | Browser window is closed | - | PASS |