0
0
Selenium Javatesting~10 mins

Why TestNG structures test execution in Selenium Java - Test Execution Impact

Choose your learning style9 modes available
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.

Test Code - TestNG
Selenium Java
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();
    }
}
Execution Trace - 6 Steps
StepActionSystem StateAssertionResult
1TestNG @BeforeClass runs: opens Chrome browser and maximizes windowBrowser window is open and maximized, ready for navigation-PASS
2TestNG @Test runs: navigates to https://example.comBrowser displays the homepage of example.com-PASS
3Finds login button by id 'login-btn' using driver.findElementLogin button element is located on the page-PASS
4Clicks the login buttonLogin form appears on the page after click-PASS
5Finds login form by id 'login-form'Login form element is located and visibleAssert that login form is displayedPASS
6TestNG @AfterClass runs: closes the browserBrowser is closed, test session ends-PASS
Failure Scenario
Failing Condition: Login form element with id 'login-form' is not found or not visible after clicking login button
Execution Trace Quiz - 3 Questions
Test your understanding
What does the @BeforeClass method do in this TestNG test?
AOpens and prepares the browser before tests run
BClicks the login button
CVerifies the login form is visible
DCloses the browser after tests
Key Result
TestNG structures test execution by clearly separating setup, test actions, assertions, and cleanup using annotations. This makes tests organized, readable, and easier to maintain.