Test Overview
This test uses a data provider to run the same login test with different username and password combinations. It verifies that the login page accepts valid credentials and shows the correct welcome message.
This test uses a data provider to run the same login test with different username and password combinations. It verifies that the login page accepts valid credentials and shows the correct welcome message.
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; 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.get("https://example.com/login"); } @DataProvider(name = "loginData") public Object[][] loginData() { return new Object[][] { {"user1", "pass1"}, {"user2", "pass2"} }; } @Test(dataProvider = "loginData") public void testLogin(String username, String password) { driver.findElement(By.id("username")).clear(); driver.findElement(By.id("username")).sendKeys(username); driver.findElement(By.id("password")).clear(); driver.findElement(By.id("password")).sendKeys(password); driver.findElement(By.id("loginBtn")).click(); String welcomeText = driver.findElement(By.id("welcomeMsg")).getText(); Assert.assertTrue(welcomeText.contains(username), "Welcome message should contain username"); driver.findElement(By.id("logoutBtn")).click(); } @AfterClass public void tearDown() { driver.quit(); } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts and Chrome browser opens | Browser opened at https://example.com/login showing login form with username, password fields and login button | - | PASS |
| 2 | Test runs first iteration with username 'user1' and password 'pass1' | Login form is visible with empty username and password fields | - | PASS |
| 3 | Find username field, clear it, enter 'user1' | Username field contains 'user1' | - | PASS |
| 4 | Find password field, clear it, enter 'pass1' | Password field contains 'pass1' | - | PASS |
| 5 | Click login button | Page navigates to user dashboard showing welcome message | Check welcome message contains 'user1' | PASS |
| 6 | Click logout button to return to login page | Login page is displayed again | - | PASS |
| 7 | Test runs second iteration with username 'user2' and password 'pass2' | Login form is visible with empty username and password fields | - | PASS |
| 8 | Find username field, clear it, enter 'user2' | Username field contains 'user2' | - | PASS |
| 9 | Find password field, clear it, enter 'pass2' | Password field contains 'pass2' | - | PASS |
| 10 | Click login button | Page navigates to user dashboard showing welcome message | Check welcome message contains 'user2' | PASS |
| 11 | Click logout button to return to login page | Login page is displayed again | - | PASS |
| 12 | Test ends and browser closes | Browser closed | - | PASS |