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.
Join the DZone community and get the full member experience.
Join For FreeBefore 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.
- 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.
Step 2: Type TestNG in the search box and click on go button as shown below:
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.
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."
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:
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."
Step 2: Name the project "Demo TestNG," click "Next" and "Finish," and the Java project is ready to go!
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."
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."
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.
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."
Step 2: Expand "TestNG," select "TestNG class," option and click next.
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.
The TestNG class would be created with a default schema.
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."
TestNG results will be shown either in the console or TestNG result window.
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.
Step 3: The HTML report should be opened in the Eclipse. Refer to the image below. To see the fresh executions, refresh the page.
Set Priority in TestNG
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.In 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.
Opinions expressed by DZone contributors are their own.
Comments