Introduction:
Annotations are a form of meta-data that can be added to the Java source code. They were introduced from Java 1.5 (JDK 5). They can be attached with class, interface, methods, fields, etc. to represent any additional information.
Some famous examples of java annotation are as given below:
- @Override
- @Deprecated
- @Documented
In this blog, we will be looking closely at TestNG Annotations.
Features:
- Easy to do group Tests.
- Easy to perform parallel Testing.
- Actions of Annotations are easy to understand.
- Prioritizing the test cases is easy.
Pre-Requisite:
- Download Eclipse IDE
- Create a maven project in Eclipse
- Add various dependencies like selenium, Test Ng, WebDriver Manager
- Download and install TestNG from eclipse market place
Configuration:
In the First step, we would be creating a maven project inside Eclipse. We would fill in basic details like name, group id, description, etc.

In the Second step, we would add a package and then create an execution file.

After creating the package, we would be creating different classes each performing different sets of actions as shown below.
And lastly, we would create a TestNg class file as shown below.

We notice that all important annotations are provided while creating the class itself.
File Structure:

List of TestNG Annotations:
- @Test
- @BeforeSuite
- @AfterSuite
- @BeforeClass
- @AfterClass
- @BeforeMethod
- @AfterMethod
- @BeforeTest
- @AfterTest
- @BeforeGroup
- @AfterGroup
We will be looking at each method with the help of the code below.
Test Case:
We can see below is just a basic code of how the test methods would function. After each annotation we are just printing out the statements.
There are two separate test methods in code which are bonded together, Test method 1 and test Method 2 as given.
Example 1:

Results:

We notice that we have added two different test methods. The code interpreters both differently and thus execute each line of code separately with respect to the given test method. We also see that before Method, before class, and before a test, each is executed before the actual method execution and the same goes with after method, after class, and after test which is executed after the actual test method.
Example 2:
In Below example we will be looking how we can launch browser, navigate to particular website and perform login action.
Code Before applying Annotations:

Code After applying Annotations:

XML code

Results:

Reports:
