Reading list Switch to dark mode

    Getting started with TestNG Selenium

    Updated 28 February 2020

    TestNG is a testing framework sparked from JUnit and NUnit but advancing some new functionalities that make it more robust and easier to use. TestNG include all levels of tests: unit, functional, end-to-end, integration, etc., and it requires JDK 5 or higher. 

    TestNG Features

    • Supports Before and after annotations.
    • Parameterization of test methods.
    • Supports Data-Driven Testing using Data providers.
    • Test cases can be arranged and prioritized easily.
    • Supports threat safe environment when executing multiple threads.
    • Easily integrate with various tools and plug-ins like build tools (Ant, Jenkins, Maven etc.), Integrated Development Environment (Eclipse).
    • Facilitates user with effective means of Report Generation using ReportNG.
    • Support for multi-threaded testing.

    Installation and Setup

    If you are using Eclipse IDE, it’s very easy to install TestNG. Below are the steps to install TestNG.

    Step 1: Launch eclipse IDE -> Click on the Help option in the menu -> Select “Eclipse Marketplace” option in the dropdown.

    Screenshot-3-1

    Step 2: Enter “TestNG” in the search field and click on the “Go” button.

    Screenshot-4-1

    Step 3: Now, click on the Install button to install TestNG.

    Start your headless eCommerce
    now.
    Find out More
    Screenshot-5-1

    So, After the Installation Eclipse would recommend you to restart in order to reflect the changes made. After restarting Eclipse, you can verify the TestNG installation.

    Go to Window->Preferences.

    Screenshot-6-1
    Screenshot-7-1

    TestNG: First Test

    Let us start with the creation of TestNG project.

    Step 1: Click on the File option in the menu -> Click on New -> Select Java Project.

    Screenshot-8

    Step 2: Enter the project and click on the “Finish” button.

    Screenshot-9-1

    Step 3: Configure the TestNG library inside the Java project. Go to “Configure Build Path” -> Click on the “Libraries” tab. Click on “Add library” as shown below.

    Screenshot-10-1

    Step 4: Now, select TestNG and click on the Next button. and, click on the Finish button.

    The TestNG is now added to the Java project.

    NOTE: Add all the JAR files inside the selenium and libs folders.

    Creating TestNG File

    Step 1: Right-click the src folder of the Java Project and select New > Other…

    Select TestNG class and then click Next

    testngclass

    Step 2: Now, enter the Source folder, package name and the TestNG class name. Here, we can also select multiple TestNG notations.

    Screenshot-1-1

    Click Finish. The template for your first TestNG file will be created with the default schema

    import org.testng.annotations.Test;
    import org.testng.annotations.BeforeTest;
    import org.testng.annotations.AfterTest;
    
    public class NewTest {
      @Test
      public void f() {
      }
      @BeforeTest
      public void beforeTest() {
      }
    
      @AfterTest
      public void afterTest() {
      }
    
    }

    The @Test  annotation is the root of TestNG test cases, tells that the method under it is a ‘test case’. Here, the method f is a test case.

    The @BeforeTest annotation tells that the method under it will be executed before the first test case. The @AfterTest annotation tells that the method under it will be executed after the last test case.

    Let’s now code an actual test code. In the following code snippet, we will be automating the sign-up flow using annotations.

    import org.testng.annotations.Test;
    import org.testng.annotations.BeforeMethod;
    import org.testng.annotations.AfterMethod;
    import org.testng.annotations.BeforeClass;
    
    import java.util.concurrent.TimeUnit;
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.testng.annotations.AfterClass;
    
    public class NewTest {
    
    WebDriver driver;
    
    @BeforeClass
    public void testSetup()
    {
    System.setProperty("webdriver.chrome.driver", ".\\Driver\\chromedriver.exe");
    driver=new ChromeDriver();
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    driver.manage().window().maximize();
    
    }
    
    @BeforeMethod
    public void openBrowser()
    {
    driver.get("https://example.com/");
    driver.findElement(By.id("signup")).click();
    System.out.println("We are currently on the following URL" +driver.getCurrentUrl());
    }
    
    @Test(description="This method validates the sign up functionality")
    public void signUp()
    {
    driver.findElement(By.id("fullname")).sendKeys("John Doe");
    driver.findElement(By.id("emailid")).sendKeys("[email protected]");
    driver.findElement(By.id("password")).sendKeys("demo123");
    driver.findElement(By.id("submit")).click();
    
    }
    
    @AfterMethod
    public void postSignUp()
    {
    System.out.println(driver.getCurrentUrl());
    
    }
    
    @AfterClass
    public void afterClass()
    {
    driver.quit();
    }
    
    }

    Execute the TestNG script

    In order to run the test, follow the below steps:

    Right-click the NewTest folder and Select Run as > TestNG Test:

    Screenshot-11

    The results of the test will be displayed into two windows the console window and in the TestNG results window:

    Screenshot-5
    Screenshot-1-2

    To generate the HTML report

    1. Right-click the Project and select Refresh. A test-output folder will be created:
    2. Open the test-output folder.
    3. Open on the “emailable-report.html” file
    Screenshot-2-1

    This is all about creating a TestNG test file, and to create a test suite and to run the automated test using TestNG.

    Thanks for reading this blog 🙂

    Happy testing!!

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    Be the first to comment.

    Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home