Menu Close
    Hire us Request quote Reading list Switch to dark mode
    lockdownWebkulUp

    Getting started with TestNG Selenium

    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.

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

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

    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.

    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.

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

    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.

    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.

    . . .
    Subscribe Articles

    Just enter your email below to subscribe and receive weekly updates about the most interesting similar articles.

    You need to accept conditions to receive letters.
    You've Subscribed!

    Share article via

    . . .

    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

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

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

    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.

    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:

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

    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

    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!!

    . . .

    Comment

    Add Your Comment

    Be the first to comment.

    Back to Top
    css.php
    Hire Us!
    Brief us about your requirements and we'll get back to you.
    Woo! Hooy!
    We have just recieved your project brief and our expert will contact you shortly.
    Send Again
    Close