← Back to list

From Login to Verified: Mastering TestNG & Annotations | Part 4

In Part 3, we created our first Selenium script, it filled the login form, clicked the button, and executed successfully. It felt exciting.

Maheshika Dissanayaka · 2026-04-19 10:51 · 0 claps · 3.3 min read
#testng #testng-annotations #test-automation #software-testing #selenium
Open on Medium ↗
Wiki topics: 🔧 · Data Engineering

From Login to Verified: Mastering TestNG & Annotations | Part 4

In Part 3, we created our first Selenium script, it filled the login form, clicked the button, and executed successfully. It felt exciting.

But an important question remains: **Did the test actually pass, or did it just run?** Running a script is not the same as validating a test.

In this part, we address that by introducing TestNG.

𝕎𝕙𝕒𝕥 𝕚𝕤 𝕋𝕖𝕤𝕥ℕ𝔾, 𝕒𝕟𝕕 𝕨𝕙𝕪 𝕕𝕠 𝕨𝕖 𝕟𝕖𝕖𝕕 𝕚𝕥?

Think of it like this: Selenium is like a robot. It can type in forms and click buttons. But it doesn’t know if the result is correct or not.

TestNG is like a supervisor standing next to the robot. It prepares everything, watches what happens, checks if the result is correct, and creates a report.

TestNG (Test Next Generation) is a testing framework for Java. When we use it with Selenium, it helps us to:

  • Run tests in an organized way — we can control which tests run and in what order
  • Check results using assertions — make sure the application works correctly
  • Use annotations — write setup and test steps in a clean and easy way
  • Generate reports — get automatic pass/fail reports after each test run

𝔸𝕕𝕕𝕚𝕟𝕘 𝕋𝕖𝕤𝕥ℕ𝔾 𝕥𝕠 𝕪𝕠𝕦𝕣 𝕡𝕣𝕠𝕛𝕖𝕔𝕥

Open your pom.xml and add this inside <dependencies>:

<!-- https://mvnrepository.com/artifact/org.testng/testng -->
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>7.10.2</version>
            <scope>test</scope>
        </dependency>

𝕎𝕙𝕒𝕥 𝕒𝕣𝕖 𝕒𝕟𝕟𝕠𝕥𝕒𝕥𝕚𝕠𝕟𝕤?

Imagine we are directing a play. We have actors (our methods), and we need to tell them when to start, what to do, and when to finish.

Annotations are like those instructions. They are written above a method and tell TestNG what to do ,like “run this before each test” or “this is the main test.”

They guide the flow without changing the actual method code.

𝕎𝕙𝕖𝕟 𝕒𝕟𝕕 𝕎𝕙𝕪 𝕥𝕠 𝕌𝕤𝕖 𝕋𝕖𝕤𝕥ℕ𝔾 𝔸𝕟𝕟𝕠𝕥𝕒𝕥𝕚𝕠𝕟𝕤

@𝙱𝚎𝚏𝚘𝚛𝚎𝚂𝚞𝚒𝚝𝚎

Runs once before all tests start. Use this for common setup for all tests, like reading a config file, starting a server, or setting up logs. Even if you have many test classes, this runs only once.

@𝙰𝚏𝚝𝚎𝚛𝚂𝚞𝚒𝚝𝚎

Runs once after all tests finish. Use this for final tasks like closing connections, creating summary reports, or sending results by email or Slack.

@𝙱𝚎𝚏𝚘𝚛𝚎𝙲𝚕𝚊𝚜𝚜

Runs once before the first test in a class. Use this to open the browser. If a class has many tests, the browser opens only once here.

@BeforeClass
public void setUp() {
    driver = new ChromeDriver();
    driver.manage().window().maximize();
}

@𝙰𝚏𝚝𝚎𝚛𝙲𝚕𝚊𝚜𝚜

Runs once after all tests in a class finish. Use this to close the browser, log out, or clean test data.

@AfterClass
public void tearDown() {
    if (driver != null) driver.quit();
}

@𝙱𝚎𝚏𝚘𝚛𝚎𝙼𝚎𝚝𝚑𝚘𝚍

Runs before every test method. This is commonly used. Use it to reset the test state, like opening the login page before each test.

@BeforeMethod
public void navigateToLogin() {
    driver.get("https://opensource-demo.orangehrmlive.com");
}

@𝙰𝚏𝚝𝚎𝚛𝙼𝚎𝚝𝚑𝚘𝚍

Runs after every test method (pass or fail). Use this to take screenshots on failure or do cleanup after each test. It runs even if the test fails, so you don’t miss important details.

@𝙱𝚎𝚏𝚘𝚛𝚎𝚃𝚎𝚜𝚝

Runs before a group of tests starts (based on the <test> in testng.xml). It runs only once and is used to prepare things like test data, configurations, or connections.

@𝙰𝚏𝚝𝚎𝚛𝚃𝚎𝚜𝚝

Runs after that group of tests finishes. It also runs only once and is used to clean up, like closing connections or resetting data.

@𝚃𝚎𝚜𝚝

Marks a method as a test case. This is the most important annotation in TestNG. It tells TestNG: “This method is a test that should be executed.”

Any method with @Test will be treated as a test case and will run when we execute the test class.

In simple…..

🧩 Suite level → runs once for all tests 📦 Test level → runs once for each <test> in testng.xml 🏷️ Class level → runs once per class 🔁 Method level → runs before and after each test method ⭐ @Test level → marks the actual test case that will be executed

𝕋𝕙𝕖 𝕖𝕩𝕖𝕔𝕦𝕥𝕚𝕠𝕟 𝕠𝕣𝕕𝕖𝕣

@BeforeSuite
  → @BeforeTest
    → @BeforeClass
      → @BeforeMethod → @Test → @AfterMethod
      → @BeforeMethod → @Test → @AfterMethod
    → @AfterClass
  → @AfterTest
@AfterSuite

ℙ𝕦𝕥𝕥𝕚𝕟𝕘 𝕀𝕥 𝔸𝕝𝕝 𝕋𝕠𝕘𝕖𝕥𝕙𝕖𝕣

Let me show you exactly how annotations work with a simple example.

import org.testng.annotations.*;

public class LoginTest {
    @BeforeSuite
    public void initSuite() {
        System.out.println("1. BeforeSuite - read config, setup environment");
    }
    @BeforeTest
    public void beforeTest() {
        System.out.println("2. BeforeTest - initialize test-level setup");
    }
    @BeforeClass
    public void openBrowser() {
        System.out.println("3. BeforeClass - open browser");
    }
    @BeforeMethod
    public void goToLoginPage() {
        System.out.println("4. BeforeMethod - navigate to login page");
    }
    @Test
    public void testValidLogin() {
        System.out.println("5. Test - enter valid credentials and verify");
    }
    @AfterMethod
    public void afterEachTest() {
        System.out.println("6. AfterMethod - take screenshot if failed");
    }
    @AfterClass
    public void closeBrowser() {
        System.out.println("7. AfterClass - close browser");
    }
    @AfterTest
    public void afterTest() {
        System.out.println("8. AfterTest - clean up test-level data");
    }
    @AfterSuite
    public void endSuite() {
        System.out.println("9. AfterSuite - generate report, send notification");
    }
}

In the next part, we’ll focus on assertions and how to effectively validate test results.


메타데이터
post_id
4bd3500da1b7
slug
from-login-to-verified-mastering-testng-annotations-4bd3500da1b7
url
https://medium.com/@maheshikamaduwanthi995/from-login-to-verified-mastering-testng-annotations-4bd3500da1b7
canonical_url
https://medium.com/@maheshikamaduwanthi995/from-login-to-verified-mastering-testng-annotations-4bd3500da1b7
author_url
https://medium.com/@maheshikamaduwanthi995
status
ok
fetched_at
2026-07-14 20:56:14