top of page
Writer's picturepavithrasenthurai

Selenium TestNG TDD Framework in Visual Studio Code Part-2


This blog will guide you to create a TestNG framework in visual studio code with step by step explanation. To create the Git repo and maven project, go through this TestNG setup with maven in VS Code and start creating a framework with me.


Maven

Maven is a widely used dependency management tool, which uses Project Object Model (POM.xml) to define the dependencies and helps Maven to build and configure the project. The initial step to create a maven project is to add the necessary dependencies in pom.xml. You have to go to Maven repository to know the latest version of dependencies and use it in your pom.xml.


Dependency links are attached below in the tabulate column which are required for TestNG framework.

Maven Surefire Plugin


Maven Surefire Plugin is an important part of the build lifecycle of Maven which is specifically designed to run tests. Frameworks like JUnit and TestNG use this plugin to execute the unit tests in Maven project. This plugin is responsible to run tests and generate reports. This is a built-in-plugin in maven hence there is no need to add this plugin separately.


To Start creating the skeleton of the project, you have to add the folders, java files and properties. follow the screenshot below

Click on the Explorer and right click on test/java to add a New folder

Enter the Folder name and click Enter

To add a Java file

click on a test/java(newly created java folder) to add New file. New file can be added by right clicking on Analyzer and add the java files under Analyzer

To create a java file, add .java with the file name. VS Code will throw an error if you don't include .java and it will not be considered as a java file.

Enter the file name with .java as the screenshot depicts

Add the required folders for the TestNG framework such as analyzer, factory, pageobject, reportlistener, testcases and utilities. You can give any names for the folder but it should be related to the particular class and project requirement.

This screenshot provides the required folders for the TestNG framework

To add the resources folder, right click on src/test and add the folder name as resources. Add the necessary properties to the resources folder. To add the property file, select new file and add property name.properties as shown as below

Added properties to the resources folder

We are defining a way to initialize the Selenium WebDriver along with ThreadLocal to support the cross browser testing and parallel execution. The following implementation selects the driver based on the config provided by the testng.xml.


Selenium WebDriver Manager allows you to automate interactions with the web browsers, which performs the user actions like entering text, clicking buttons, and navigating between pages.


Thread local is used to make the thread safe regardless of being declared in static memory.

ThreadGuard Protect helps to ensure thread count when WebDriver instance is created.

Define a class where you can initialize the driver with ThreadLocal and getDriver Method

Property file organize the external configuration. The below code helps in reading the properties from the config.properties. The logger instance is created for logging purposes.




Hooks are used to run around the test cases based on the hooks method like @AfterMethod, @BeforeMethod @BeforeAll, @AfterAll.


We are using the @BeforeMethod to initialize the browser for every test via WebDriver and @AfterMethod to tear down the browser and WebDriver for every test.


The annotation BeforeMethod takes a parameter where browser is being passed from TestNG.xml configuration which calls BaseClass.initializeBrowser(browser) to initialize WebDriver.


@AfterMethod parameterized iTestResult.FAILURE which is responsible to take screenshots for the failed methods and attaches it to the Allure report.

Hooks class

We are defining the BasePage to be extended by every Page Object Model, which abstract out the PageFactory initialization.

BasePage.java

This is an example of SignInPage which extends the BasePage to initialize the web driver.

SignInPage.java

This Page object class should be created to represent a specific page or component of the web application. We are using the PageFactory pattern where the eleents on this page are defined using @FindBy annotations to locate the web component using the xpath and define the WebElement.

WebElement class is used for Identifying and working with element objects in the DOM.


The main functions of WebElement:

SignInPage.java

These methods are exposed from page class to be used by the test classes to set the test data. This is created to interact with the web elements and perform actions such as clicking buttons, verifying title, page states, filling forms and sending the values etc.

SignInPage.java

In Test case for SignIn web page, we are creating an object Model SignInPage to call the methods and validate using assertions. It is preferred to use the attribute "Depends on methods" which is used to specify dependencies between test methods instead of creating an object. You can also add logger.info to inform the status about the execution. Test cases package contains test classes which define and execute test scenarios for different pages and web application's feature. It should be annotated with @test and this performs actions and assertions to validate the functionality which we have defined. We should include the priority with the number of order you want it to be executed. testing like smoking, regression can be done by using groups. We can execute the test individually by clicking the play button or you can execute the testcases through testng.xml.

Test Case Sample

The DataReader reads and provides the access from an Excel file to test data. The loadWorkbook method loads the Excel workbook for the given file path. To retrieve data from the excel sheet, we have to create methods like getCellData, getRowcount, getCellCount which access the test data.


Data Provider

Excel utility

Excel Utility serves the same purpose as Data provider which reads and provides access to test data from an excel file. This helps in centralizing data management, which makes it easier to access and maintain data across different test cases.







The ConfigReader class reads and provides access from a properties file to configuration properties.

Config Reader

Config, Allure, Extent, Log4j2 are the properties which are created under resources will be responsible for generating reports and logging purpose. To add property file, right click on resources and add propertyname.properites. The example has been provided below.

properties added in resources

This is the config.properties where we are storing all the URL's and configure the browser values.

config properties

This is the folder for Test Data where we are storing all the excel sheets of our data.

Folder of Test Data

The TestNG.xml is a configuration for the TestNG to execute the test. We can perform multiple functionalities like executing individual test, group test and excluding specified classes. Parallel execution can also be configured with multiple thread counts.


Here is the example on how to add Test, parameters, classes and parallel execution in your TestNG project.

testng.xml

The MyTransformer class implements the IAnnotation interface to modify testng annotations at runtime using reflection.

This method applies retry analyzer for all test methods to RetryAnalyzer.class which allows to apply this retry logic to all methods instead of applying individually in each test method.

MyTransformer

This RetryAnalyzer class implements the interface IRetryAnalyzer. This RetryAnalyzer helps to automatically retry all the failed test cases to a specified limit. This can be globally configured in testng.xml for the TestNG suite. It also implements the ITestListener interface and overrides the method 'onTestFailure' to dynamically connect the RetryAnalyzer with failed test cases.

Retry Analyzer

The Extent report class in our project is a TestNG listener which generates Extent Reports for our executed test cases. It listens to the test events and provides the results into an Extent Report.

This class implements the iTestListener interface from TestNG, which provides methods to listen to test events.

This onStart method is called before any test method is executed which initializes the ExtentReport instance.

This onFinish method is called after all test methods are executed which builds the nodes for passed, failed and skipped tests.

Build test nodes method create nodes in the Extent Report for each result and sets the start and end times for the test.

This class plays a big role in generating detailed and visually appealing test reports which makes the results to be analyzed easily.



Extent report file

Listeners class can be configured globally in testng.xml file to react to the events

listeners class configured in testng.xml

TestNG tests can be executed through maven. Maven is a tool to manage your dependency and help with the build cycle. To run Maven, You need to click maven which is located in the left bottom of the VS Code

Click Maven
click to clean, install or test maven

As we have given the path to generate the results in Target folder, Test reports are generated in Target folder which includes the allure-results, maven-status and surefire-reports etc.

Screenshot of reports generated in Target folder

As we saw Maven Surefire plugin is responsible for generating reports, you can see these test reports generated in Surefire reports folder which provides reports such as DSAlgo_Extent.html, emailable-report.html, failed.png, index.html, passes.png, skipped.png, testng-failed.xml and testng-results.xml etc.

Screenshot of reports folder in Surefire reports

Finally after completing the framework and executing the test cases, you can commit your file and push all your changes to the main repository. You can also pull the changes from other branches by accepting incoming changes, keeping the current changes, or accepting both changes according to the project needs and merge all changes. The final step should be configuring your git repo with Jenkins to finally build your project and generate the reports by configuring Allure, extent in Jenkins.


25 views

Recent Posts

See All
bottom of page