Content ITV PRO
This is Itvedant Content department
TestNG Integration
TestNG Annotations
Learning Outcome
4
Execute test cases in a structured and prioritized manner using TestNG
3
Apply annotations to control test flow, setup, and teardown operations
2
Identify common annotations like @Test, @BeforeMethod, @AfterMethod, @BeforeClass, @AfterClass.
1
Understand the purpose of TestNG annotations in organizing test execution
5
Improve test readability, reusability, and maintainability through annotation-based design
Recall
Handling Alerts, Frames, Windows
Before learning TestNG Annotations, you should recall these basic concepts like :
Selenium fundamentals like WebDriver and locators
Basics of automation testing and test cases
Basic Java programming (methods and classes)
Understanding test execution flow
Need for test setup and teardown in frameworks
Think of running a movie shoot
@BeforeSuite
Decide movie story & budget (full setup)
@BeforeTest
Plan shooting schedule
@BeforeClass
Set up camera, lights, location
@BeforeMethod
Prepare actors before each scene
Think of running a movie shoot
@Test → Shoot the actual scene
@AfterMethod
Reset actors & scene after each shot
@AfterClass
Pack up equipments
@Test → Shoot the actual scene
@AfterTest
Wrap up shooting schedule
@AfterClass
Pack up equipment
TestNG annotations in Selenium WebDriver are like a movie shoot where setup happens first, scenes are executed, and cleanup happens in a proper sequence.
Why are TestNG annotations used in Selenium WebDriver?
TestNG annotations are used to control test execution flow, manage setup and teardown, and support test reporting in a structured way.
Control the order of test execution
Handle setup and cleanup (before/after tests)
Reduce repeated code in test scripts
Organize and structure automation tests
Support test execution reporting and results generation
What is TestNG?
TestNG is a testing framework inspired from JUnit and NUnit but introducing some new functionality that makes it more powerful and easier to use.
It is an open-source automated testing framework; where NG of TestNG means Next Generation.
TestNG is similar to JUnit but it is much more powerful than JUnit but still, it's inspired by JUnit.
It is designed to be better than JUnit, especially when testing integrated classes.
Pay special thanks to Cedric Beust who is the creator of TestNG
List of TestNG Annotations
@BeforeSuite → Runs before all tests in suite
@AfterSuite → Runs after all tests in suite
2. Test Level
@BeforeTest → Runs before test section
@AfterTest → Runs after test section
3. Class Level
@BeforeClass → Runs before first method in class
@AfterClass → Runs after all methods in class
List of TestNG Annotations
@BeforeMethod → Runs before each test method
@AfterMethod → Runs after each test method
5. Test Execution
@Test → Marks a test case
6. Additional (Advanced)
@DataProvider → Supplies data to test methods
@Parameters → Passes parameters from XML
@Listeners → Used for reporting and event handling
Execution Order of TestNG Annotations
In TestNG, annotations control how and when different parts of your test code are executed. Understanding their execution order is essential for designing clean and predictable test suites.
@BeforeSuite
@BeforeTest
@BeforeClass
@BeforeMethod
@Test
@AfterMethod
(repeat BeforeMethod → Test → AfterMethod for each test)
@AfterClass
@AfterTest
@AfterSuite
TestNG Priority
We can have multiple @Test annotations in a single TestNG file
By default, methods annotated by @Test are executed alphabetically .
Methods can be executed in a different order, by using parameter "priority" .
@Test(priority=0)
TestNG will execute the @Test annotation with the lowest priority value up to the largest
Dependent Test
TestNG allows to specify dependencies with attributes dependsOnMethods.
@Test(dependsOnMethods={'openBrowser'}
public void closeBrowser()
{
driver.close();
}
@Test
public void openBrowser()
{
driver = new FirefoxDriver();
}
The collection of TestNG Tests together is called a Test Suite.
A test suite can run multiple tests at once by executing the test suite.
Additionally, these test cases can be dependent on each other or may have to be executed in a specific order independently.
It is important to remember that the we need to create a TestNG XML file for test suite to be run
Grouping of Tests
Groups in TestNG denote the process of grouping different tests together into a straightforward group and running these tests together by just running the group in a single command
<?xml version="1.0" encoding="UTF-8"?>
<suite name="Sample Suite">
<test name="testing">
<groups>
<run>
<include name="Regression"/>
</run>
</groups>
<classes>
<class name="com.example.tests.SampleTest"/>
</classes>
</test>
</suite>
TestNG Parameters
Parameters in TestNG are similar to annotations in TestNG in their declaration.
Similar to parameters in any other programming language, they are declared to pass some values onto the function.
A simple reason to use parameters is that they let us run a function many times with different values or to run different functions with the same values.
An example of using the parameters in TestNG can be entering different values in an input box .
Parameters pass the values in the runtime.
@Parameters ({"a", "b"})
where a and b are the values that pass to the function.
TestNG Parameters are run through the TestNG XML file and not from the test case files directly.
NOTE
Passing Parameters In TestNG
There are two ways to pass the parameters in TestNG:
TestNG Parameters
TestNG DataProviders
DataProviders pass the different parameters on a single test in a single execution, whereas parameters pass the parameters just once per execution in TestNG
Handling Frames
Handling frames (iframes) means switching Selenium’s control from the main page to an embedded HTML document so that elements inside it can be accessed and interacted with.
To interact with frame elements, Selenium must switch context using the following ways.
By Index
switchTo.frame(int frameNumber)
Pass the frame index and driver will switch to that frame to switch to 0th iframe use
driver.switchTo().frame(0)
By Name or ID
switchTo.frame(string frameNameOrId)
Pass the frame ID or frame Name and driver will switch to that frame
driver.switchTo().frame(“iframe1”)
By WebElement
switchTo.frame(WebElement frameElement)
Pass the frame web element and driver will switch to that frame
WebElement iframeElement = driver.findElement(By.id("IF1"));
driver.switchTo().frame(iframeElement);
Switching Back to Main Page
driver.switchTo().defaultContent();
OR
driver.switchTo().parentFrame();
Summary
4
3
2
1
WebElements represent HTML elements on a web page in Selenium.
5
Check element states: displayed, enabled, selected.
Perform actions: click, type, clear, submit.
1
Alerts, frames, and windows need context switching.
2
Use accept(), dismiss(), sendKeys() for alerts.
3
Switch to frames to access inner elements.
4
Use window handles to switch tabs/windows.
5
WebElements enable verification of element states for testing.
Quiz
Which command is used to switch to a frame in Selenium?
A. switchTo().frame()
B.switchTo().alert()
C. switchTo().window()
D.getWindowHandles()
Quiz - Answer
Which command is used to switch to a frame in Selenium?
B.switchTo().alert()
C. switchTo().window()
D.getWindowHandles()
A. switchTo().frame()
By Content ITV