Selenium with TestNG Framework Tutorial | TECHLISTIC

What is TestNG Framework? 

TestNG is a unit testing framework developed in the lines of JUnit and NUnit, however it introduces some new functionalities that make it  more powerful and easier to use. It is designed to cover all categories  of tests − unit, functional, end-to-end, integration, etc., and it requires JDK 5 or higher. Automation Testers make use of TestNG Framework to execute our tests. And it also provides some cool features in the form of parameters of annotations.

We'll learn about all the interesting features of TestNG Framework in this TestNG Tutorial.

Table of Content

    1. TestNG Framework Features

      2. TestNG Framework Components

      3. Install TestNG in Eclipse

      4. Annotations in TestNG

      5. Annotation Parameters

      • Skip Test Case
      • Test Case Grouping

      6. Selenium with TestNG Integration

      7. Verify and Assert

      8. Creating test suite using testng.xml

      9. TestNG Page Factory

      10. TestNG Data Provider

                      1. Features of TestNG Framework

                      • Flexible test configuration 
                        • with annotations 
                        • @BeforeMethod
                        • @Test
                        • @AfterMethod
                        • @BeforeClass
                        • @AfterClass and more
                      • Support for data-driven testing 
                        • with @DataProvider
                      • Support for annotation parameters
                      • Support for method dependency 
                        • with @dependsOnMethods
                      • Support for defining execution Priority of test methods 
                        • with @priority
                      • Supported by a variety of IDE and tools like.,
                        • Eclipse
                        • IDEA
                        • Maven
                      • Support for default Test Reporting in HTML and XML formats.
                      • Default JDK functions for runtime and logging (no dependencies)

                      2. TestNG Components

                      1. Annotations 
                      2. Annotation Parameters
                      3. Verify and Assert
                      4. Page Factory
                      5. Data Provider
                      6. Creating suite using testng.xml

                      3. Install and Setup TestNG in Eclipse

                      1. Open Eclipse.
                      2. Click on the 'Help' menu option.
                      3. Click on "Eclipse Marketplace"
                      You need to install TestNG plugin in your Eclipse to work with it. Below are the steps:

                      4. Type Testng in Find text box and click on search icon. After TestNG is appeared in the search click on the Install button.

                      5. After clicking Install, it will show some other dialogues, just click accept the terms and click Finish.
                      6. Installation will take some time to complete.
                      7. Eclipse will ask you for restart, click yes.
                      8. After restart, you can verify if TestNG is installed correctly or not. Click Window -> Preferences , TestNG should display in Preferences list.

                      4. What are Annotations in TestNG?

                      TestNG annotations are the most important part, they control the execution of test and other methods in the automation script. And in this post I am going to explain some of the important annotations. So, we will be covering following
                      1. @BeforeClass
                      2. @AfterClass
                      3. @BeforeMethod
                      4. @AfterMethod
                      5. @Test
                      Let's summarize annotation:
                      • Annotation in TestNG actually is code snippet which lies just ahead of the method declaration and controls the execution of the method
                      • Annotation starts with @ sign. 
                      Sample Code:
                      import org.testng.annotations.Test;
                      public class TestngPriority {
                        @Test(priority=0) // <<<<==== This is a TestNG annotation
                        public void one() {
                         System.out.println("This is test case 1");

                      In below example, there are two methods f and g with @test annotation and every other annotation has one method each.

                      Code Example:
                      package com.techlistic.selenium;
                      import org.testng.annotations.Test;
                      import org.testng.annotations.BeforeMethod;
                      import org.testng.annotations.AfterMethod;
                      import org.testng.annotations.BeforeClass;
                      import org.testng.annotations.AfterClass;
                      public class TestNGAnnotations {
                        public void f() {
                         System.out.println("f Test Case/Method");
                        public void g() {
                         System.out.println("g Test Case/Method");
                        public void beforeMethod() {
                         System.out.println("Before Method");
                        public void afterMethod() {
                         System.out.println("After Method");
                        public void beforeClass() {
                         System.out.println("Before Class");
                        public void afterClass() {
                         System.out.println("After Class");


                      [RemoteTestNG] detected TestNG version 6.14.3
                      Before Class
                      Before Method
                      f Test Case/Method
                      After Method
                      Before Method
                      g Test Case/Method
                      After Method
                      After Class
                      PASSED: f
                      PASSED: g

                      Observe the execution order:
                      1. First of all @BeforeClass is executed.
                      2. Then @BeforeMethod
                      3. @Test for f
                      4. @AfterMethod
                      5. @BeforeMethod
                      6. @Test for g
                      7. @AfterMethod
                      8. @AfterClass
                      Now, you would be able to make out the execution flow. So, this way you can create your setup, teardown methods and use whatever before/after annotation with it as per your requirement. If you want to execute your setup only once at class level then use @BeforeClass or if you want to execute setup before every test case/method then use @BeforeMethod (Same for teardown).

                      5. Annotation Parameters

                      i. Set Test Methods Priority

                      TestNG also provides a way to set the priority of test cases. By priority I mean, priority of execution. It can be used with @Test annotation. Below is the syntax for it:


                      In below code example,
                      • We have four test methods 
                      • And we have set the priority of each method as 0,1,2,3 
                      • The method which has 0 priority will be executed first 
                      • Then method with priority 1 will be executed and so on 
                      So, this how TestNG gives us a very easy way to control the execution of our test methods/cases.
                      Example Program:
                      import org.testng.annotations.Test;
                      public class TestngPriority {
                        public void one() {
                         System.out.println("This is test case 1");
                        public void two() {
                         System.out.println("This is test case 2");
                        public void three() {
                         System.out.println("This is test case 3");
                        public void four() {
                         System.out.println("This is test case 4");

                      This is test case 1
                      This is test case 3
                      This is test case 2
                      This is test case 4

                      ii. Set Dependency of Test Methods

                      TestNG provides a way to manage method dependencies with annotation parameters. For this testNg provides 'DependsOnMethods' parameter for @Test annotation. If you have declared dependency of one method on second, then first of all second method will be executed and then after first method. 

                      Example Code:

                      Program Explanation:
                      • In above code, method SignIn() is dependent on OpenBrowser() 
                      • And method LogOut() is dependent on both SingIn and OpenBrowser methods. 
                      Below is the execution sequence of above code snippet:
                      1. OpenBrowser
                      2. SignIn
                      3. LogOut.

                      This will execute first (Open Browser)
                      This will execute second (SignIn)
                      This will execute third (Log Out)

                      6. How to use Selenium with TestNG

                      The TestNG annotations that we learned so far can be used with Selenium automation code. Let's take a look at a sample program where we have used TestNG annotations inside Selenium code and then we'll understand it step by step in the program explanation section below:

                      Sample Program

                      package com.techlistic.selenium;
                      import org.openqa.selenium.WebDriver;
                      import org.testng.annotations.AfterMethod;
                      import org.testng.annotations.BeforeClass;
                      import org.testng.annotations.BeforeMethod;
                      import org.testng.annotations.Test;
                      public class TestNGAnnotations2 {
                       WebDriver driver;
                       public void setupClass() {
                        // Set Chrome driver property
                        System.setProperty("", "D:\\mydir\\chromedriver.exe");
                        // Launch chrome browser
                        driver = new ChromeDriver();
                        // Opening Browser URL and maximize it
                       public void setupMethod() {
                        // Webdriver Code for login
                       public void test_HomePage() {
                        // Webdriver code to test some functionality on Home page
                       public void test_UsersPage() {
                        // Webdriver code to test some functionality on Users page
                       public void teardownMethod() {
                        // Webdriver Logout code
                       public void teardownClass() {
                        // Close all webdriver instances

                      Program Explanation:
                      1. @Test - This annotation is used with test methods.
                      2. @BeforeClass - If this annotation is used with some method then it would be executed only once before all test methods or you can say when the class is about to be executed then @BeforeClass executes. It is generally used for setup method for class. E.g.,
                        • Let's say your class has 10 test methods
                        • And you want to launch browser only once at the beginning and use the same browser instance for all test methods
                        • Then launch browser in you setup_class method with @BeforeClass on top of it.
                      3. @BeforeMethod/@AfterMethod - Use this method for setup method to be called before every test. Like in above example, we are logging in before every test method and logging out after every method.
                      4. @AfterClass - Use this annotation for teardown class method. For e.g., in above example we used it for closing all the browser instances at the end of executing all the test methods.

                      Follow Techlistic

                      YouTube Channel | Facebook Page | Telegram Channel | Quora Space
                      Feel free to ask queries or share your thoughts in comments or email us.


                      Popular posts from this blog

                      10+ Best Demo Websites for Selenium Automation Testing Practice

                      Automate Amazon like E-Commerce Website with Selenium WebDriver

                      Handle Static and Dynamic Web Table in Selenium WebDriver

                      How to Automate Google Search with Selenium WebDriver

                      25+ Most Important Selenium WebDriver Commands Tutorial

                      50+ Most Important Selenium WebDriver Interview Questions and Answers

                      Automate Features with Selenium WebDriver

                      Top 10 Highly Paid Indian Origin CEO's in USA