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:
In this blog, we will be looking closely at TestNG Annotations.
- Easy to do group Tests.
- Easy to perform parallel Testing.
- Actions of Annotations are easy to understand.
- Prioritizing the test cases is easy.
- 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
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.
List of TestNG Annotations:
We will be looking at each method with the help of the code below.
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.
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.
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: