Technologies - TestNG Framework - Annotations

 

List of annotations that TestNG supports:

Annotations - are default Interfaces in TestNG

@BeforeSuite 

The annotated method will be run only once before all tests in this suite have run.

 

@AfterSuite 

The annotated method will be run only once after all tests in this suite have run.

 

@BeforeClass 

The annotated method will be run only once before the first test method in the current class is invoked.

 

@AfterClass 

The annotated method will be run only once after all the test methods in the current class have run.

 

@BeforeTest 

The annotated method will be run before any test method belonging to the classes inside the <test> tag is run.

 

@AfterTest 

The annotated method will be run after all the test methods belonging to the classes inside the <test> tag have run.

 

@BeforeGroups 

The list of groups that this configuration method will run before. This method is guaranteed to run shortly before the first test method that belongs to any of these groups is invoked.

 

@AfterGroups 

The list of groups that this configuration method will run after. This method is guaranteed to run shortly after the last test method that belongs to any of these groups is invoked.


@BeforeMethod 

The annotated method will be run before each test method.


@AfterMethod 

The annotated method will be run after each test method.


@Test 

Marks a class or a method as a part of the test.


@DataProvider 

Marks a method as supplying data for a test method. The annotated method must return an Object[][] where each Object[] can be assigned the parameter list of the test method. The @Test method that wants to receive data from this DataProvider needs to use a dataProvider name equals to the name of this annotation.


As the name indicates, it supplies the Data to the test method


Where we are using this TestNG annotation in our Automation framework?

This is basically used to iterate our Test script with various data sets also called Test Data:
So our Driver script reads the Input excel file with Keywords, execution status, objects, etc row by row & copies the entire data set to an ArrayList. This arraylist will be iterated over the Test Script method using the Data provider annotation.

Simple Example:

============================================================

/ This method will provide data to any test method that declares that its Data Provider is named"test1"

@DataProvider(name = "test1")

 public Object[][] createData1() {

  return new Object[][] { { "AJAY", new Integer(36) }, { "Amit", new Integer(37) } };

 }

 // This test method declares that its data should be supplied by the Data Provider named "test1"

@Test(dataProvider = "test1")

public void verifyData1(String n1, Integer n2) {
System.out.println(n1 + " " + n2);
}

 

============================================================


@Listeners 

 

Defines listeners on a test class. 

Used for custom logging & reporting based on the test execution

@Parameters 

 

Describes how to pass static parameters to a @Test method.

We can provide any number of parameters on each of our test method, and can instruct TestNG to pass the correct parameters with the @Parameters annotation. In the case of complex objects - Dynamic data, we can use @DataProvider annotation.

 

Usage of Listeners & Parameters :

======================================================================

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">

 

<!--Change The Parallel attribute for parallel Execution-->

 

<suite name="Suite" parallel="tests">

 

<listeners>

<listener class-name="org.automation.TestListener" />

</listeners>

 

 <test name="FirefoxTest">

  <parameter name="browser" value="firefox" />

  <classes>

   <class name="org.automation.TestNGDriverClass" />

  </classes>

 </test> 

</suite>

========================================================================

Note:

"org.automation.TestNGDriverClass" is Driver class, where our actual execution logic begins - where our test methods are present.

 

TestNG executes the test methods in a prescribed order.


@Parameters Usage from TestNG.xml file in Driver script class is as follows: 

============================================================
//
@Parameters({ "browser","name"})

@Parameters({ "browser" })
@Test
public void testSingleString(String browser) {
System.out.println("Invoked Browser is " + browser);
assert "Firefox".equals(browser);
}

============================================================


@Test

It is used to tell that the method under it is a test case. By default, methods annotated by @Test are executed alphabetically.

 

Comments

Popular posts from this blog

QA's approach 2 Java - Understanding Static context

Selenium 4 absolute beginners - How to create Batch execution file

Technologies - Log4J - Create Log4j Configuration File - Where ? How ? What ?