TestNG is a testing framework commonly used in conjunction with Selenium for automating and organizing test cases in Java.
This project is a web automation framework designed to perform end-to-end testing of a web application using the TestNG testing framework, Selenium WebDriver for browser automation, and Maven for project management and build automation.
Purpose: TestNG is utilized for structuring and executing test cases. It provides annotations for defining test methods, supports parameterization, and facilitates the grouping of tests.
Purpose: Selenium WebDriver is employed for automating interactions with the web application. It allows the scripting of browser actions such as clicking buttons, entering data, and validating page content.
Purpose: Maven is used for project management and build automation. It simplifies the project setup, manages dependencies, and enables the execution of tests. Maven ensures a consistent and reproducible build process.
- TestNG Annotations
- Assertions in TestNG
- Hard Assert and Soft Assert
- Passing Parameters in TestNG
- Prioritizing Selenium Test Cases using TestNG Feature
- Grouping Tests in Selenium
- DataProviders in TestNG
- TestNG Listeners in Selenium
- Reporting and logging in TestNG
- Cross Browser Testing using Selenium and TestNG
- Running Parallel Tests in Selenium with TestNG
-
Annotations help define the order of execution and manage the test lifecycle. TestNG annotations such as @Test, @BeforeMethod and @AfterMethod are used to setup and tear down the Selenium test environment.
Following annotation are used in the sample project:- @Test β This marks a class or a method as a part of the test.
- @BeforeMethod β The @BeforeMethod method in TestNG will execute before each test method.
- @AfterMethod β The @AfterMethod method in TestNG will run after each test method is executed.
- @Parameters β This annotation is used to pass parameters to test methods.
- @DataProvider β The @DataProvider annotation in TestNG is used to supply test methods with data, allowing the same test method to be executed multiple times with different sets of data.
Assertions in TestNG are used to verify that the expected result and the actual result matches or not in the test case.
Following assertion methods are used in project :
-
Assert.assertEquals(String actual, String expected) : This method is to check whether the actual value is equal to the expected value.
Code:
TestNG Xml file:
Output :
Report :
-
Assert.assertTrue(condition) : This method checks that a condition is true or not. If not, it will fail the test and an AssertionError is thrown.
Code:
TestNG Xml file:
Output :
Report :
-
Hard Assert : A "hard assert" refers to an assertion in testing that, when it fails, immediately stops the execution of the test.
Code:
TestNG Xml file:
Output:
Report :
-
Soft Assert : A "soft assert" refers to a mechanism in testing that allows the execution of a test to continue even after encountering a failure. Unlike a "hard assert," which immediately stops the test upon failure, a soft assert continues executing subsequent steps. If the first assertEquals condition fails, the subsequent steps and the additional assertions will still be executed. The assertAll() method at the end; collects all the failures and reports them together after the test execution is complete.
Code:
TestNG Xml file:
Output:
Report :
The @Parameters annotation is used to pass parameters to a test method. This annotation allows to define parameters at the method level and pass values from the XML suite configuration file.
Code:
TestNG Xml file:
Output:
Report :
The priority attribute is used to specify the execution order of test methods within a test class. By assigning priority values to test methods, you can control the sequence in which these methods are executed during a test run.
Code:
Testng XML file:
Output:
Report:
TestNG provides the ability to categorize tests into groups using the @Test(groups = "groupname") annotation.
The tests in this project are grouped as "Smoke" and "Regression". E.g: groups = {"Smoke", "Regression"}
Code:
Testng XML file: Following xml will only run test grouped under "Smoke"
Report:
Testng XML file: Following xml will only run test grouped under "Regression"
Report:
The @DataProvider annotation is used to supply test methods with data. It allows to run the same test method with different sets of data, increasing the test coverage and flexibility.
Code: This is the dataprovider class which contains @DataProvider annotated method.
The following code, the @Test annotation uses attribute dataProvider with name to access the data along with DataProvider class name
Testng XML file:
Output:
Report:
Listeners are useful for tasks such as logging, reporting, and taking actions based on the status of the test cases. ITestListener used in the project is an interface that allows to implement custom listeners to log the status of execution of test cases and also capture screenshot in case of test failure.
Code:
Testng XML file:
Output:
Report:
Screenshot:
Following is the location the screenshot is getting captured.
Following is the screenshot captured.
TestNG makes it easy to implement cross-browser testing by allowing to parameterize tests and configure test methods to run on multiple browsers.
Testng XML file:
Testng XML file:
Output:
Report:
TestNG generates detailed HTML reports that provide information of the test executions. By default, TestNG generates an HTML report after each test run. The report named emailable-report.html is located in the project's output directory, the "test-output" folder. This report includes information such as the overall test results, test durations, and individual test method outcomes.
Testng XML file:
Report:

