UNIT – V
TestNG Frameworks
Syllabus: Framework Designing Structure, Keyword Framework, Data Driven
Framework, Linear Framework, Modular Framework, Hybrid Framework. Working on
Maven Project – Creating Extent Reports, Basics of Github and Jenkins.
Framework Designing Structure:
Design Principle 1: Test Configuration
Configurable items like Application Url, Username, Password, Browser type, Secret
key, Driver.exe path should be kept in an external file. This will make sure while you
change anyconfiguration there should be minimum effort involved.
Design Principle 2: Test Setup Information
Any kind of test setup must be done by the @BeforeSuite, @BeforeClass TestNG
annotations. Similar manner test clean up should be done by the @AfterSuite,
@AfterClass TestNG annotations if you are using Test NG Based automation
framework inyour project.
Design Principle 3: Test Folder and Main Folder
All Test classes should be saved inside a folder called “test” and it MUST be a kind of
mirror of main folder, meaning it will follow the same structure of the main project
folder, but it willonly contain tests.
As shown, com.nga will be the group-id for both test and main folder. And all test
classmethods part of src/test/java package.
Design Principle 4: Maintain Object identification repository
Most common issues faced during automation are object identification changes. Framework
should be able to patch such changes easily.
Good design approach creates Object identifiers in page class and also map the object
identifiers with page interaction methods in the same Page Class.
Example Snapshot
Design Principle 5: Status monitoring using debug logs and Exception Handling
A framework should allow monitoring of the test execution status in real time and should be
capable of sending alerts in case of failure using exception handling. This design approach
ensures quick turnaround time in event of a test scripts failure.
Example Logging Snapshot
Design Principle 6: Reporting
The framework should support html/Excel/Pdf report formats with details about test pass/fail
for each test case/suite/test run.
Example Report Snapshot
Design Principle 7: Test Scripts and Test Data
Test scripts and test data should always be separate from each other. Test data input can be in
various forms like Excel, Ini, database inputs, hash maps etc.
Example Test Scripts and Test Data Snapshot
Design Principle 8: Creation of Utility Methods
Utility method contain all reusable components. It includes both generic functions, and
application specific functions.
Tests should be exposed only to the implemented utility methods and tests should be
performed by invoking these utility methods only.
Example Utility Class Methods Snapshot
Design Principle 9: Right code placement
Automation projects tend to grow fast. Along with new tests, new shared code like page
objects and data models keep adding during the test development.
Maintaining a good, organized structure is necessary for project scalability and teamwork.
Test cases should be organized by feature area. Common code should be abstracted from test
cases and put into shared libraries.
Example Multiple Test Classes Snapshot
Design Principle 10: Avoid Code Duplication
Many testing operations are inherently repetitive.
QA Engineers sometimes just copy-paste code blocks, rather than seek existing methods or
add new helper methods, to save development time.
Such Coding practice must be strictly avoided as it not only makes code base in huge size but
also result maintenance effort extremely high.
Design Principle11: Multi-Platform Support
Today's business applications run on multiple platforms same time: Multiple Web Browsers,
Android Device, IOS Device and some cases Windows Version of application also available
Automation framework must have capability to add on new platform if required by
application in future
Data-driven Framework:
A data-driven framework is a software testing framework that separates the test script
logic and the test data, allowing for more flexibility and reusability in testing.
The test cases and suites are retrieved from external files like Excel, .csv, .xml, or some
database tables. After the data sets are transformed into external files, they are imported
into the automation testing tool.
1.Define Your Test Class:
• Create a TestNG class where you will define your test methods.
2. Create a Data Provider:
• Implement a data provider method annotated with @DataProvider. This method
supplies the test data to your test methods.
3. Run Your Tests:
Configure your TestNG XML file to include the test class, and TestNG will execute
your test methods with the provided data.
Advantages:
1. Allows testing of the application with multiple sets of data values during regression
testing
2. Separates the test case data from the executable test script
3. Allows reusing of Actions and Functions in different tests
Keyword-driven Framework:
Keywords are crucial when promoting code functionality. They receive parameters and
provide developers with accurate outputs. Testers often have to put extra effort into
rewriting codes from some parts of the script if the code has to manage complex
functionality.
1. The automation script will read the instructions or test input data from the Excel sheet
2. The input data is entered in the application under test
3. The test cases are performed and results are returned
4. The test output data is stored in the Excel sheet
Step 1: To design a Keyword Driven Framework, first identify all the actions to be
performed for automated testing of an application. In the above scenario, seven steps
have to be completed.
Step 2: Once all actions are identified, the next step is to create a keyword map table in
the Excel sheet. Keyword map table is a table in an Excel sheet that defines all the
keywords available for test automation projects.
The table below shows the keyword map table for the above scenario
• Step 3: Once all keywords have been identified, the next step is to place the Excel sheet
in the project package. Create a package named ‘excelData’ by right-clicking on the
Project. For this, go to New > Package.
• Step 4: Create a new package named “utility”. Now, create a new class file named
“Constants” in which the tester will define constants like URL, filePath,
and excelData.
• Step 5: Now, write code for each action keyword. Create a new package named
“keywordDriven”. Create a new class file named “Action_Keyword”. In this class,
create methods for each Action Keyword identified in Excel.
• Step 6: Write code to read data from an Excel sheet in this step. For this, use an Apache
POI library, which allows testers to read, create, and edit Microsoft Office documents
using Java.
Create a new package named “excelUtility” and then create a new class file named
“ReadExcelSheet”. Look at the following source code to read all keywords from the
step table.
• Step 7: Next step is to write code for calling readExcelData() method
of ReadExcelSheet class and methods of ActionKeywords class. Create a new package
named “executionEngine” and write the following source code by creating a class
named “ExecutionTest”.
Hybrid Framework In Selenium:
• Hybrid Driven Framework is a combination of both the Data-Driven and Keyword-
Driven framework.
• Here, the keywords, as well as the test data, are externalized. Keywords are maintained
in a separate Java class file and test data can be maintained either in a properties
file/excel file/can use the data provider of a TestNG framework.
• This framework has five key components:
1. Function Library
2. Excel Sheet to store Keywords
3. Design Test Case Template
4. Object Repository for Elements/Locators
5. Test Scripts or Driver Script
Function library:
• User-defined methods are created for each user action. In other words, Keywords are
created in the library file.
Excel Sheet To Store Keywords
• Keywords that are created in the library file are stored in an excel sheet with its
description for anyone who uses this framework to understand.
Design Test Case Template
• An organized and consistent test case template is necessary to create organized and
consistent test documentation.
• A well-designed template improves test case clarity, thoroughness, and
understandability.
Object Repository
A separate Repository is maintained for all elements on the webpage. Each
WebElement is referred to with a name followed by its value in an Object Repository
(in this case, it is a properties file).
Driver Script
• This contains the main logic to read all the test cases from the test case template excel
sheet and performs the corresponding action by reading from the library file.
• The script is designed based on the test case template created.
4. Linear Scripting Framework:
• A basic automation framework that used to linearly test small apps. Tests are created
and executed individually. This Framework is also sometimes known as the ‘Record
and Playback.’
Advantages:
• Generating test scripts is less time consuming
• No prior coding knowledge needed
• Fast and easy
Disadvantages:
• Autogenerated scripts cannot be used again as the data is hardcoded
• Need to rewrite for different data sets
5. Modular Testing Framework
• Module wise test scripts are created so that an application is broken into smaller,
independent modules whose scripts can run individually.
Advantages
• Better scalability
• Individual scripts can be reused
• Modules are independent and do not rely on each other
Disadvantages
• Takes more time to analyze test cases
• Identifying reusable flows is hard
• Need to rewrite for different data sets
• Requires coding skills
Extent reports
• These reports are customizable HTML documents that depict results as pie charts. They
also allow the generation of custom logs, snapshots, and other customized details.
• Once an automated test script runs successfully, testers need to generate a test execution
report. While TestNG does provide a default report, they do not provide the details.
Extent Reports contain two major classes that are used frequently.
• ExtentReports class
• ExtentTest class
ExtentReports class:
Syntax:
• ExtentReports reports = new ExtentReports(“Path of directory to store the resultant
HTML file”, true/false);
• Extent Reports class is used to generate an HTML report on the user-specified path. The
Boolean flag indicates if the existing report needs to be overwritten or a new report
needs to be created.
ExtentTest class
Extent Test class is used to log test steps onto the generated HTML report.
The above classes can be used with the frequently used built-in methods that are
stated below.
• startTest -startTest method is used to execute preconditions of a test case
• endTest - endTest methods is used to execute post-conditions of a test case
• Log - log method is used to log the status of each test step onto the resultant HTML
report
• Flush - Flush method is used to erase any previous data on the report and create a new
report.
Test Status can be any of the following values:
• PASS
• FAIL
• SKIP
• INFO
Syntax:
reports.endTest();
test.log(LogStatus.PASS,”Test Passed”);
How to generate Extent Reports
1. Import the JAR file : extentreports-java-2.41.2.jar . After downloading the ZIP file,
extract its contents into a folder.
2. Add the JAR files to the project build path with the option Build Path ->
Configure Build Path.
3. Create a new JAVA class for Extent Reports with the code below.
public class ExtentDemo {
static ExtentTest test;
static ExtentReports report;
@BeforeClass
public static void startTest()
{
report = new
ExtentReports(System.getProperty("user.dir")+"ExtentReportResults.html");
test = report.startTest("ExtentDemo");
}
@Test
public void extentReportsDemo()
{
System.setProperty("webdriver.chrome.driver", "D:\\chromechromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.google.co.in");
if(driver.getTitle().equals("Google"))
{
test.log(LogStatus.PASS, "Navigated to the specified URL");
}
else
{
test.log(LogStatus.FAIL, "Test Failed");
}
@AfterClass
public static void endTest()
{
report.endTest(test);
report.flush();
}
}
How to capture screenshots in Extent Report
• getScreenShotAs(): Captures screenshot of the current WebDriver instance and stores
it in different output forms.
• Log method uses the built-in method, addScreenCapture of Extent Test class to fetch the
screenshot and append it to the Extent Report.
Ex: test.log(LogStatus.FAIL,test.addScreenCapture(capture(driver))+ “Test Failed”);
GitHub
• GitHub is a web-based platform that uses the Git version control system for tracking
changes in source code during software development.
• It provides a collaborative environment for developers to work on projects, share code,
and manage versions effectively.
• GitHub is widely used for open-source projects, but it's also used by many organizations
for private development.
Key features of GitHub include:
1. Version Control: GitHub uses Git, a distributed version control system, to track
changes to code over time. This allows multiple developers to work on a project
simultaneously without conflicts.
2. Repository: A repository (repo) is a collection of files and version history associated
with a project. Repositories can be public, allowing anyone to view and contribute, or
private, limiting access to a specific set of users.
3. Branching and Merging: Developers can create branches to work on specific features
or fixes without affecting the main codebase. Once changes are tested and complete,
they can be merged back into the main branch.
4. Collaboration: GitHub facilitates collaboration among developers. It provides tools for
code review, issue tracking, and project management. Teams can discuss code changes,
report issues, and plan future development within the platform.
5. Pull Requests: A pull request is a proposal to merge changes from one branch into
another. It is a way for developers to review code, discuss changes, and ensure that the
proposed changes meet the project's standards before merging.
6. Issues: GitHub's issue tracker helps manage and prioritize tasks, enhancements, and
bugs. Users can create, comment on, and close issues, providing a way to communicate
about the project's development.
7. GitHub Actions: GitHub Actions allow developers to automate workflows, such as
testing and deployment, directly within the GitHub repository.
8. GitHub Pages: GitHub provides a feature called GitHub Pages that allows users to
publish static websites directly from their GitHub repositories.
Jenkins
• Jenkins is an open-source automation server that facilitates the automation of building,
testing, and deploying software.
• It is widely used in the field of Continuous Integration and Continuous Deployment
(CI/CD).
• Jenkins helps automate parts of the software development process, making it easier for
development teams to integrate changes, identify and fix issues early, and deliver
software more rapidly.
Jenkins Architecture
• Developers commit changes to the source code, found in the repository.
• The Jenkins CI server checks the repository at regular intervals and pulls any newly
available code.
• The Build Server builds the code into an executable file. In case the build fails,
feedback is sent to the developers.
• Jenkins deploys the build application to the test server. If the test fails, the developers
are alerted.
• If the code is error-free, the tested application is deployed on the production server.