TestNG Tutorial: Annotations & Framework in Selenium

TestNG’s really shaken things up in the software testing world, offering developers a strong framework for automated testing. In this tutorial, we’ll dive into the basics of TestNG, understand how it works, the key annotations you need to know, and how it’s used with Selenium.

Table of Contents

What is TestNG?

TestNG is an open-source test automation framework developed by Cedric Beust. It builds on the principles of JUnit and NUnit but adds more advanced features, making it super efficient and adaptable for today’s testing needs. The name “TestNG” stands for “Next Generation,” hinting that it aims to outdo its predecessors.

Why Use TestNG?

Here are a few reasons why TestNG is a go-to choice:

  • Advanced Annotations: TestNG has a bunch of annotations that let you control the order and execution of your test methods. Some key ones are @Test, @BeforeTest, @AfterTest, @BeforeSuite, and more, giving you tons of flexibility.
  • Parallel Testing: You can run tests in parallel with TestNG, cutting down on execution time and boosting efficiency.
  • Data-Driven Testing: TestNG supports data-driven testing with test data providers, so you can run tests with different data sets without writing multiple methods.
  • Reports and Logs: TestNG offers detailed reports and logs, which are super helpful for debugging and spotting issues.

Annotations in TestNG

Annotations are a big deal in TestNG:

  • @Test: This defines a test method and is used to run a specific test. Every method marked with @Test is treated as a separate test case.
  • @BeforeTest: Used to set up the environment before a test runs. It kicks in before the first @Test method in a class.
  • @AfterTest: Cleans up the environment after a test runs. It’s executed after the last @Test method in a class.
  • @BeforeSuite and @AfterSuite: These run before and after a suite of tests, respectively. They provide common setup and cleanup for all the tests in the suite.
  • @BeforeClass and @AfterClass: These run before and after a class of tests, offering common setup and cleanup for all the methods in a class.
  • @BeforeMethod and @AfterMethod: These run before and after each test method, offering extra setup and cleanup if needed.

Writing Your First TestNG Test

To write your first TestNG test, follow these steps:

  1. Set up the environment: Install TestNG and set up your IDE.
  2. Create a TestNG class: Create a new Java class for your TestNG tests.
  3. Add annotations: Add the necessary annotations to your test methods.
  4. Write your test method: Create your test method that includes the actual test and any necessary assertions.
  5. Run your test: Execute your TestNG test to check the results.

Example TestNG Test

Here’s an example to show how TestNG works:


package firsttestngpackage;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;

public class firsttestngfile {
    public String baseUrl = "https://www.browserstack.com/";
    String driverPath = "D:\\Selenium\\chromedriver.exe";
    public WebDriver driver;

    @BeforeTest
    public void launchBrowser() {
        System.out.println("launching Chrome browser");
        System.setProperty("webdriver.chrome.driver", driverPath);
        driver = new ChromeDriver();
        driver.get(baseUrl);
    }

    @Test
    public void verifyHomepageTitle() {
        String expectedTitle = "Most Reliable App & Cross Browser Testing Platform | BrowserStack";
        String actualTitle = driver.getTitle();
        Assert.assertEquals(actualTitle, expectedTitle);
    }

    @AfterTest
    public void terminateBrowser() {
        driver.close();
    }
}

Selenium Integration

To hook up Selenium with TestNG:

  1. Install Selenium WebDriver: Download and set up the Selenium WebDriver for your chosen browser (like ChromeDriver, GeckoDriver).
  2. Import Selenium Libraries: Pull in the necessary Selenium libraries in your TestNG class.
  3. Create a WebDriver: Initialize a WebDriver instance for your selected browser.
  4. Use Selenium Methods: Use Selenium methods to interact with the web app (like navigating to a page, clicking on elements).
  5. Verify Results: Use assertions to confirm your test results.

Conclusion

TestNG is a powerful tool for automated testing, offering numerous benefits and features. By mastering its annotations and integrating it with Selenium, developers can streamline their testing processes and ensure software quality. Leveraging TestNG leads to more maintainable, scalable, and efficient automated tests, making it a top pick for developers aiming to up their testing game and ensure solid software quality assurance.

References

  1. BrowserStack. (2023, April 23). How to Automate TestNG in Selenium.
  2. Guru99. (2024, March 23). TestNG Tutorial: What is Annotations & Framework in Selenium.
  3. LambdaTest. (2019, April 24). TestNG Annotations Tutorial With Examples For Selenium Automation.
  4. BrowserStack. (2023, February 15). TestNG Annotations in Selenium Webdriver.
0 CommentsClose Comments

Leave a comment

Newsletter Subscribe

Get the Latest Posts & Articles in Your Email

We Promise Not to Send Spam:)