DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Frameworks
  4. How to Use the TestNG Framework for Creating Selenium Scripts

How to Use the TestNG Framework for Creating Selenium Scripts

If you're looking to start whipping up some quick scripts, see why TestNG might be the better framework on the market.

Bala Murugan user avatar by
Bala Murugan
·
Apr. 17, 19 · Tutorial
Like (2)
Save
Tweet
Share
14.31K Views

Join the DZone community and get the full member experience.

Join For Free

Before moving on to the techniques to use TestNG framework, let’s understand what it is.

TestNG is a framework for automation testing for the Java language. The "NG" in TestNG stands for "Next Generation." It is designed to cover a wide range of testing croups like unit, functional, integration, end-to-end, and more, along with strong and easy-to-use functionalities. TestNG would be quite familiar to the people who have used JUnit, with some additional enhanced features. When the whole buzz about the framework took place, JUnit took all the attention across the Java platforms. JUnit is an extremely easy-to-use framework, but has some limitations as well. The limitations of JUnit opened the gates for TestNG. TestNG is created by a renowned programmer Cedric Beust with a view to overcoming the limitation of JUnit and combining the advantages of JUnit and NUnit. TestNG is distributed under the Apache Software license and is an open-source framework.

TestNG is capable of generating powerful reports that describing passed, failed and rejected test cases and the reports can be shared for the purpose of management decisions.

Why Use TestNg With Selenium?

It is preferred by many Selenium users because of its advantages over JUnit. Here are some of the key features of TestNG:

  • Annotations are easy to use and understand. Annotations are always preceded by the "@" symbol in TestNG and JUnit.
  • Proper format report regarding complete test details can be generated.
  • Many test cases can be compiled by converting them to testng.xml file and prioritizing the execution schedule for the tests.
  • Keyword "invocation count" can be used to execute the same test case multiple time.
  • Cross-browser testing can be performed.
  • Easy integration with Maven and Jenkins.
  • TestNG can generate reports in a variety of readable formats. Image title
  • TestNG allows cross-browser testing, which means executing multiple test cases on different browsers.
  • TestNG simplifies the tests by using a series of annotations that do not require static methods.
  • Instead of terminating the test prematurely for any unidentified exceptions, TestNG automatically handles and reports the exceptions as failed steps. 

Let's get started with using TestNG installation in Eclipse.

Downloading and Installing TestNG on Eclipse

Step 1: Launch Eclipse in the menu, click on "Help," and select Eclipse marketplace from the dropdown list.

Image title

Step 2: Type TestNG in the search box and click on go button as shown below:

Image title


Step 3: The search results on the above command will appear and the user has to click on the "Install" button shown in the below image in the right corner.

Image title

Step 4: As soon as the user presses the install button, a window pops up to confirm the installation, which is activated by clicking "Confirm."

Image title

Step 5: In this step, accept the license terms and press the finish button. The installation progress will be seen in a pop up window. In order to save the changes, Eclipse needs to be closed and restarted. Upon restart, verify the TestNG install:

Image title

Image title

Sample TestNG Project Creation

The first step should always be creating a test case by setting up a new TestNG project in the Eclipse integrated development environment (IDE).

Step 1: Click on "File" in the menu option. Select "New," then select "Java Project."

Image title

Step 2: Name the project "Demo TestNG," click "Next" and "Finish," and the Java project is ready to go!
Image title

Step 3: In this step, the TestNG library has to be configured into the newly-created  Java project.
Click on the "Libraries" tab under Configure Build Path then click on "Add Library."

Image title


Step 4: In continuation to the next step, a dialogue box would appear asking the user to select the library to be configured. Select TestNG "Next," and "Finish."

Image title

Now, TestNG has been added to the Java project and all the libraries can be seen in the package explore by expanding the project. All Selenium libraries and jars to be added in the project's build path.

Image title

TestNG Class Creation

Now, the basic setup is done to get started with TestNG class creation. The following steps will create a sample script with TestNG.

Step 1:
Expand "Demo TestNG" and hover over the "SRC" folder, right-click "SRC" and navigate to
 "New" > "Other."

Image title

Step 2: Expand "TestNG," select "TestNG class," option and click next.

Image title

Step 3: Fill in the details as asked in the below figure and click "Finish." TestNG annotation can also be selected which can be seen in test class schema.

Image title

The TestNG class would be created with a default schema.

Image title

Now the foundation of the TestNG test script has been created and the actual test code will be inserted.

TestNG Script Execution

Steps to be followed for executing TestNG script:

1. Right-click inside the editor, select "Run As," and select "TestNG Test."

Image title

TestNG results will be shown either in the console or TestNG result window.

Image title

Image title

Generating HTML Reports


TestNG is known for generating very comprehensive HTML reports for the test executions that can be viewed in many browsers and even in Eclipse’s in-built browser.

Step 1: A new TestNG class can be executed at this step.  Refresh the project that contains the TestNG class by right-clicking and selecting the "Refresh" option.

Step 2: Generate a new folder called "test-output" under the "SRC" folder. Expand "test-output" and open "emailable-report.html" with the Eclipse browser. The HTML file will reveal the recent results that can be seen.

Image title

Image title

Step 3: The HTML report should be opened in the Eclipse. Refer to the image below. To see the fresh executions, refresh the page.

Image title

Set Priority in TestNG

Image title

Code snippet: This is how the priority is set in the code

In many cases, the test script is composed of more than one test method, in that case, the priority can be set using  @test  and setting the priority there and then. In the above code, the methods are annotated with @test along with the priorities set as 0, 1 and 2. method execution will happen as method 1,2,3 respectively.

A few of the most useful and preferred annotations used in TestNG are @test,    @before suite,   @after  suite,  @ before  test, and  @after test . Many of these can be executed in JUnit 3 and 4 frameworks.

Let’s show how a few of the annotations can be written in code along with the execution priority.
Image titleIn the above code, the method execution sequence is being presented.  The position on the annotation blocks can be interchanged without affecting the sequence in which they will run.

Conclusion

TestNG is a reliable Java-based testing framework capable of producing easy-to-understand HTML reports that can be generated automatically. It has certain advantages over JUnit like with annotations, easily grouped test cases, the capability to create parallel tests and many more. 

TestNG Framework Testing JUnit Eclipse Annotation

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Load Balancing Pattern
  • How Observability Is Redefining Developer Roles
  • OpenID Connect Flows
  • Iptables Basic Commands for Novice

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: