Test Overview
This test opens a web page, clicks a login button, and verifies the login form appears. It shows how TestNG structures test steps and assertions to organize test execution clearly.
This test opens a web page, clicks a login button, and verifies the login form appears. It shows how TestNG structures test steps and assertions to organize test execution clearly.
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 public void testLoginButtonShowsForm() { driver.get("https://example.com"); WebElement loginButton = driver.findElement(By.id("login-btn")); loginButton.click(); WebElement loginForm = driver.findElement(By.id("login-form")); Assert.assertTrue(loginForm.isDisplayed(), "Login form should be visible after clicking login button"); } @AfterClass public void tearDown() { driver.quit(); } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | TestNG @BeforeClass runs: opens Chrome browser and maximizes window | Browser window is open and maximized, ready for navigation | - | PASS |
| 2 | TestNG @Test runs: navigates to https://example.com | Browser displays the homepage of example.com | - | PASS |
| 3 | Finds login button by id 'login-btn' using driver.findElement | Login button element is located on the page | - | PASS |
| 4 | Clicks the login button | Login form appears on the page after click | - | PASS |
| 5 | Finds login form by id 'login-form' | Login form element is located and visible | Assert that login form is displayed | PASS |
| 6 | TestNG @AfterClass runs: closes the browser | Browser is closed, test session ends | - | PASS |