Test Overview
This test demonstrates how to use test groups in Selenium with TestNG. It runs a login test that belongs to the 'smoke' group and verifies successful login by checking the page title.
This test demonstrates how to use test groups in Selenium with TestNG. It runs a login test that belongs to the 'smoke' group and verifies successful login by checking the page title.
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.Test; public class LoginTest { WebDriver driver; @BeforeClass public void setUp() { driver = new ChromeDriver(); driver.manage().window().maximize(); } @Test(groups = {"smoke"}) public void testValidLogin() { driver.get("https://example.com/login"); WebElement username = driver.findElement(By.id("username")); WebElement password = driver.findElement(By.id("password")); WebElement loginButton = driver.findElement(By.id("loginBtn")); username.sendKeys("user1"); password.sendKeys("pass123"); loginButton.click(); String expectedTitle = "Dashboard"; String actualTitle = driver.getTitle(); Assert.assertEquals(actualTitle, expectedTitle, "Page title should be Dashboard after login"); } @AfterClass public void tearDown() { if (driver != null) { driver.quit(); } } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | TestNG initializes and prepares to run tests | - | PASS |
| 2 | Browser opens Chrome and maximizes window | Chrome browser window is open and maximized | - | PASS |
| 3 | Navigates to https://example.com/login | Login page is loaded with username, password fields and login button | - | PASS |
| 4 | Finds username input field by id 'username' | Username input field is located | - | PASS |
| 5 | Finds password input field by id 'password' | Password input field is located | - | PASS |
| 6 | Finds login button by id 'loginBtn' | Login button is located | - | PASS |
| 7 | Enters username 'user1' and password 'pass123' | Username and password fields are filled | - | PASS |
| 8 | Clicks login button | Login form submitted, page navigates to dashboard | - | PASS |
| 9 | Checks page title equals 'Dashboard' | Dashboard page is displayed | Assert.assertEquals(actualTitle, expectedTitle) | PASS |
| 10 | Test ends and browser closes | Browser window is closed | - | PASS |