Basically, TestNG is a user – friendly automation framework that overcomes the drawbacks and limitations of Java Unit, thus introducing the entire new set of features which makes it more powerful and useful.
This automated testing framework helps in elimination of numerous limitation of the older framework. It facilitates the developer the ability to write more flexible and powerful tests.
TestNG Framework
Features of TestNG
How to Download or Install TestNG for a project
Before performing this step by step process, do not forget to visit these prerequisites to install selenium IDE.
In order to download TestNG plug-in, all you need to have is a selenium IDE and Active internet connection. But, if selenium IDE is already installed in your system, then follow these simple steps:
Step 1: Download and install the Java Software Development Kit (JDK)
Step 2: Download “Eclipse IDE for Java Developers”. You choose 32 bit or 64 bit as per your requirement.
Step 3: You can download the Selenium Java Client Driver. There would be different languages, choose only the Java from the options.
Step 4: Configure Eclipse IDE with Web Driver.
TestNG Basic Annotations
Annotations are a new feature in Java provided by JDK 5.0 version. Annotations can be used to describe metadata in java programs. To describe any information about the coding part, annotations are used. Following is the list of annotations usually used in TestNG:
Sr. No. | Annotation | Description |
1. | @BeforeSuite | This annotation will always be executed before all tests are run inside a TestNG. |
2. | @AfterSuite | This annotation will always be executed after all tests are run inside a TestNG. |
3. | @BeforeClass | This annotation will always be executed before the first test method in a class is called on. |
4. | @AfterClass | This annotation will always be executed after the first test method in a class is called on. |
5. | @BeforeTest | Executed before any test method associated with the class inside <test> tag is run. |
6. | @AfterTest | This annotation will always be executed before any test method associated with the class inside <test> tag is run. |
7. | @BeforeGroups | This annotation will always be executed before the first test method associated with these groups is called on. |
8. | @Aftergroups | This annotation will always be executed shortly after the first test method associated with these groups is called on. |
9. | @BeforeMethod | This annotation will always be executed before each test method. |
10. | @AfterMethod | This annotation will always be executed after each test method. |
11. | @DataProvider | This annotation must return object [ ], it marks a method as supplying data for a test method. |
12. | @Factory | This annotation will return the object and marks a method as a factory that will be used by TestNG as test classes. |
13. | @Listeners | This annotation defines the listeners on a test class. |
14. | @parameters | This annotation describes how parameters can be passed to a test method. |
15. | @Test | This annotation marks a class as a part of the test. |
Execution Procedure of Methods in TestNG with Annotations
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; import org.testng.annotations.BeforeTest; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeSuite; import org.testng.annotations.AfterSuite; public class TestngAnnotation { // Case 1 @Test public void testCase1() { System.out.println(“Case I”); } // Case 2 @Test public void testCase1() { System.out.println(“Case II”); } @BeforeMethod public void beforeMethod() { System.out.println(“Before Method Annotation”); } @AfterMethod public void afterMethod() { System.out.println(“After Method Annotation”); } @BeforeClass public void beforeClass() { System.out.println(“Before Class Annotation”); } @AfterClass public void afterClass() { System.out.println(“After Class Annotation”); } @BeforeTest public void beforeTest() { System.out.println(“Before Test Annotation”); } @AfterTest public void afterTest() { System.out.println(“After Test Annotation”); } @BeforeSuite public void beforeSuite() { System.out.println(“Before Suite Annotation”); } @AfterSuite public void afterSuite() { System.out.println(“After Suite Annotation”); } } Creating XML file in C:\>TestNG for executing annotations <?xml version = “1.0” encoding = “UTF-8”?> <!DOCTYPE suite SYSTEM “http://demo” > <suite name = “Suite1”> <test name = “test1”> <classes> <class name = “Annotation”/> </classes> </test> </suite> Test case compilation using javac C:\TestNG>javac Annotation.java Output: Before Suite Annotation Before Test Annotation Before Class Annotation Before Method Annotation Case I After Method Annotation Before Method Annotation Case II After Method Annotation After Class Annotation After Test Annotation After Suite Annotation =============================================== Suite Total tests run: 1, Failures: 0, Skips: 0 =============================================== |
Why use TestNG with Selenium?
Selenium tests alone cannot generate a proper format for the test results. However, with the help of TestNG, you can generate the proper format for the test results.
Read also : 10 Best Automation Testing Tools of 2018
TestNG is widely used by other selenium users because of its features and feasibility. Moreover, it is associated with several advantages through which a user can be very comfortable to leverage TestNG.
Following are the points why you need to use TestNG with selenium:
Advantages of TestNG over JUnit