Working on TestNG Annotations
Step 1: Open the Eclipse IDE.
Step 2: Create a Maven Project.
Step 3: After Creating the Maven Project, the project exploration will look like the below image.
Step 4: Create a TestNG Class that contains all TestNG Annotations.
The below code gives you a glimpse of the Working of TestNG Annotations.
TestNGAnnotations. Java
package com.w3wiki.test;
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 TestNGAnnotations {
@Test
public void test1() {
System.out.println("Test Case 1");
}
@Test
public void test2() {
System.out.println("Test Case 2");
}
@BeforeMethod
public void beforeMethod() {
System.out.println("This will Execute Before Method");
}
@AfterMethod
public void afterMethod() {
System.out.println("This will Execute After Method");
}
@BeforeClass
public void beforeClass() {
System.out.println("This will Execute Before Class Execution");
}
@AfterClass
public void afterClass() {
System.out.println("This will Execute After Class Execution");
}
@BeforeTest
public void beforeTest() {
System.out.println("This will Execute Before Test ");
}
@AfterTest
public void afterTest() {
System.out.println("This will Execute After Test ");
}
@BeforeSuite
public void beforeSuite() {
System.out.println("This will Execute Before Suite ");
}
@AfterSuite
public void afterSuite() {
System.out.println("This will Execute After Suite ");
}
}
Now, let’s explain what this code does:
- Package Declaration
- The code is in the com.w3wiki.test package.
- Imports:
- The code imports all annotations and classes from the TestNG framework like org.testng.annotations.AfterMethod and org.testng.annotations.Test etc.
- TestNG Annotations Class : Inside the TestNG Annotations class there are various methods
- @Test: This annotation marks a method as a test method. In this code, test1() and test2() are two test methods. They print out messages indicating which test case is being executed.
- @BeforeMethod: This annotation denotes a method that should be run before each test method. Here, beforeMethod() is executed before each test method and prints a message.
- @AfterMethod: This annotation denotes a method that should be run after each test method. Here, afterMethod() is executed after each test method and prints a message.
- @BeforeClass: This annotation denotes a method that should be run before any test method belonging to the classes inside the <test> tag is run. Here, beforeClass() is executed before any test method prints a message.
- @AfterClass: This annotation denotes a method that should be run after all the test methods in the current class have been run. Here, afterClass() is executed after all the test methods and prints a message.
- @BeforeTest: This annotation denotes a method that should be run before any test method belonging to the <test> tag is run. Here, beforeTest() is executed before any test method and prints a message.
- @AfterTest: This annotation denotes a method that should be run after all the test methods belonging to the <test> tag have run. Here, afterTest() is executed after all the test methods and prints a message.
- @BeforeSuite: This annotation denotes a method that should be run before all tests in a suite have run. Here, beforeSuite() is executed before any test method prints a message.
- @AfterSuite: This annotation denotes a method that should be run after all tests in a suite have run. Here, afterSuite() is executed after all the test methods and prints a message.
Step 5: Now, we create the AnnotationsTest.xml file to configure the TestNG Annotations class.
Annotations.XML
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="suite">
<test name="test1">
<classes>
<class name="com.w3wiki.test.TestNGAnnotations" />
</classes>
</test>
</suite>
Step 6: Run the After_Method file. Right-click on the After_Method, move the cursor down to Run As and then click on the 1 TestNG Suite.
Output:
TestNG Annotations in Selenium Webdriver with Examples
TestNG is a testing framework widely used in Selenium WebDriver for automation testing. It provides a wide range of annotations that help in organizing and controlling the flow of test cases. TestNG learns from JUnit and NUnit, making itself better by adding new features that make testing easier and more effective. One of these improvements is the use of annotations.
Table of Content
- What are TestNG Annotations?
- Types and Hierarchy of TestNG Annotations
- Working on TestNG Annotations
- Conclusion
- FAQs on TestNG Annotations in Selenium Webdriver
Contact Us