Selenium Data Driven Framework with POM

In this article we are going to discuss following points:                                 
  •     Brief Introduction about Selenium
  •     What is a TEST Automation Framework? 
  •     What is Data Driven Testing and why we are using it?
  •     POM and its Pros and Cons
  •     Framework Components
  •     Key Enhancements in Framework
  •     Framework Demo
  •     Steps to Create framework from Scratch
  •     How to explain Framework
  •     Implementation of OOP Concept in Selenium Framework 
Different Components in a Framework
Selenium and It’s history
Selenium is a free (open source) automated testing suite for web applications across different browsers and platforms. Selenium is not just a single tool but a suite of software's, each catering to different testing needs of an organization. It has four components:
  • Selenium Integrated Development Environment (IDE)
  • Selenium Remote Control (RC)
  • WebDriver
  • Selenium Grid

Selenium Supporting Environment
Operating Environment: MS Windows, Linux, Apple OX
Programming Languages: 
Language Supported by Selenium
Application Environment:  Web Based and Mobile Based web applications which has web forms. For mobile native apps we are using third party tool i.e. Appium.

What is a TEST Automation Framework?
A set of guidelines like coding standards , test-data handling , object repository treatment etc... which when followed during automation scripting produce beneficial outcomes like increase code re-usage , higher portability , reduced script maintenance cost etc. Mind you these are just guidelines and not rules; they are not mandatory and you can still script without following the guidelines. But you will miss out on the advantages of having a Framework.

What is Data Driven Testing?
Data-driven is a test automation framework which stores test data in a table or spreadsheet format. This allows automation engineers to have a single test script which can execute tests for all the test data in the table.
Data-Driven Framework
Why Data Driven Testing?
Look at the following Example:
We want to test the login system with multiple input fields with 1000 different data sets.
To test this, you can take following different approaches:
Approach 1) Create 1000 scripts one for each data-set and runs each test separately one by one.
Approach 2) Manually change the value in the test script and run it several times.
Approach 3) Import the data from the excel sheet. Fetch test data from excel rows one by one and execute the script.
In the given three scenarios first two are laborious and time-consuming. Therefore, it is ideal to follow the third approach. Thus, the third approach is nothing but a Data-Driven framework.

Page Object Model
Page Object Model is a design pattern to create Object Repository for web UI elements. Under this model, for each web page in the application, there should be corresponding page class. This Page class will find the Web Elements of that web page and also contains Page methods which perform operations on those Web Elements.
POM Vs Non POM Structure
POM Advantages and Disadvantages:
Advantages
Object Repository: You can create an Object Repository of the fields segmented page-wise. This as a result provides a Page Repository of the application as well. Each page will be defined as a java class. 
Functional Encapsulation: All possible functionality or operations that can be performed on a page can be defined and contained within the same class created for each page. This allows for clear definition and scope of each page's functionality.
Low maintenance: Any User Interface changes can swiftly be implemented into the interface as well as class.
Programmer Friendly: Robust and more readable. The Object-oriented approach makes the framework programmer friendly.
Low Redundancy: Helps reduce duplication of code. If the architecture is correctly and sufficiently defined, the POM gets more done in less code.
Efficient & Scalable: Faster than other keyword-driven/data-driven approaches where Excel sheets are to be read/written.

Disadvantages
High Setup Time & Effort:  Initial effort investment in development of Automation Framework is high. This is the biggest weight of POM in case of web applications with hundreds/thousands of pages. It is highly suggested that if this model is decided to be implemented, then it should be done parallel to development of the application.
Skilled labor: Testers not technically sound or aware of programming best practices are a nightmare in this case. 
Specific:  Not a generic model. Automation Framework developed using POM approach is specific to the application. Unlike keyword-driven/data-driven frameworks, it is not a generic framework.

Framework Demo Overview– WordPress Automation
Project/Application: WordPress
No. of Test Cases: 10 Test Cases
Automation Tools: Selenium Web Driver, TestNG framework and Java

Key Points:
  • Selenium Webdriver is being used as the core automation engine.
  • Eclipse IDE is used to develop the automated scripts. 
  • Build tool Maven is used for build, execution and dependency purpose.
  • TestNG framework is used for organizing the scripts.
  • Page Factory are created to store the element definitions.
  • Test data is read from Excel sheet at run time.
  • Git and Github is used for version control management.
  • CI tool Jenkins is used to run the scripts.
  • Extent Reports test results are generated for each run.
Test Cases Covered:
Smoke Test: Check for the links are directing to desired pages
Functional Tests: Search Posts
Regression Tests: Add New User, Publish a New Posts

Key Components in this Framework
  •     Config.Properties file for Constants
  •     Extent Report
  •     TestNG with Groups and Priorities
  •     Simplified script writing
  •     Cross Browser Testing
  •     New Selenium Customized functions
  •     Maven for Jar files version control
  •     Created dependencies for Jar files using Maven
  •     Git configuration
  •     Jenkins Integration
  •     Ready to use Frame work
Selenium Framework Integration
Maven: Using Maven for build, execution and dependency purpose. Integrating the TestNG dependency in POM.xml file and running this POM.xml file using Jenkins.
Version Control Tool: We use Git as a repository to store our test scripts.
Jenkins: By using Jenkins CI (Continuous Integration) Tool, we execute test cases on daily basis and also for nightly execution based on the schedule. Test Result will be sent to the peers using Jenkins.

MAVEN Configuration - POM File
Sample POM file
Project Structure:
Complete Project structure shown as below:
Sample Project Structure
Properties File
This file (config.properties) stores the information that remains static throughout the framework such as browser specific information, application URL, Test Data path etc.
All the details which change as per the environment and authorization such as URL, Login Credentials are kept in the config.properties file. Keeping these details in a separate file makes easy to maintain.

Properties file
TestBase Class
Test Base class (TestBase.java) deals with all the common functions used by all the pages. This class is responsible for loading the configurations from properties files, Initializing the WebDriver and also to create the object of FileInputStream which is responsible for pointing towards the file from which the data should be read.

TestBase Class
Page Class Package
This package contains all the objects of different pages.
Advantage :  If the object locator is changed, and the same object is used in more than 10 test cases, then you can  change in one object page , the updated locator will be called in all test cases.

Page Classes
Test Class Package
We have separate packages for Pages and Tests. All the web page related classes come under Pages package and all the tests related classes come under Tests package.

For example, Home Page and Login Page have a separate classes to store element locators. For the login test there would be a separate class which calls the methods from the Home Page class and Login Page class.
Test Classes
Test Data Package
All the test data will be kept in excel sheet (TestData.xlsx). By using ‘TestData.xlsx’, we pass test data and handle data driven testing. We use Apache POI to handle excel sheets.

Test Data Sheet
Utility Package
Utility class (TestUtil.java) stores and handles the functions (The code which is repetitive in nature such as waits, actions, capturing screenshots, accessing excels, sending email etc.,) which can be commonly used across the entire framework. The reason behind creating utility class is to achieve re-usability. This class extends the TestBase class to inherit the properties of TestBase in TestUtil.

Utility Class
Action Driver Package
ActionDriver class contains all generic functions/ customized functions like clicking on an element, verifying element or text,  Waitforelementpresent, mousehover and rightclick etc.
For Example we can write customized commands scrollByVisibilityOfElement and click shown below:
Action Driver Class
Browser Driver Folder
This folder contains IEDriverServer.exe , chromedriver.exe and geckodriver.exe.

Driver Folder
Screen Shots Folder
Screenshots will be captured and stored in a separate folder and also the screenshots of a failed test cases will be added in the extent reports.

Screen Shots
TestNG.xml
This testNG.xml suite file contains groups and classes of the modules.

TestNG.xml
Reports
Extent Reports is being used for reporting purpose.


Extent Report
Logs – log4j
Grants a complete understanding of test suites execution. Logs are an exceptional assistant in debugging the program execution issues and failures.

log4j

Version Control tool
GitHub is a web-based service for version control using Git.

GitHub

Selenium Framework Integration with CI Tool
Jenkins is used as CI tool

Jenkins

Steps to Create Framework from Scratch
You can follow below Steps:
  •  Create Maven Project
  •  Update pom.xml
  •  Create Page Objects using page classes
  •  Create Basic Test Case
  •  Add logs to test case
  •  Read common values from properties file
  •  Run test cases on desired browser
  •  Add extent report
  •  Create data driven test case
  •  Adding new test cases.
How to Explain Framework?
1. Framework Overview -- What kind of Framework are you using?
 We are using Page Object with Page Factory framework with functional/structural implementation.
 Make sure you know what is functional/structural implementation in this framework.
2. High Level overview of your framework -- Different Components with Architecture.
Language - We use java because it is known to most people when we started automation.
Build Tool - We are using standardized maven project for build, execution & dependency management.
Data Driven - For handling data driven cases we are passing data using java properties file/xls file /csv file.
Make sure you know about libraries like openCSV,JXL/APACHE POI/Java Properties class
Testing Framework - For ordering tests we are using testng framework.
Configuration management - Git and Github - We check in our code into client repository using a version controlling tool git bash on windows system.
3. Make sure you know what is functional/structural implementation in this framework.
 We have maintained a page class for every page in our application and a page test class to maintain test for that pages.
 e.g. Product listing page,Add to cart page,Payment page,Invoice generation page. 
4. We have maintained separate package for page and page test e.g.
 com.companyName.page1 com.companyName.pageTest1
 Maintaining different packages is always a good practice to follow.
5. We also have a base page class for common functions use by all the pages.
 Make sure you know why we have Base Page class in page object.
6. Logging Mechanism - We are using log4j library to maintain logging of our project. We are using all kinds of logging statements like 
 INFO,DEBUG,ERROR etc. We have maintained a separate class for it in com.companyName.main package
7. Reporting - We are using Extent Report for reporting purpose. It is a third party report and it is easily available at maven central repo.
We are using maven postman plugin / JAVA API to send generated extent reports as an attachment to client Distribution list.

Implementation of OOP Concept in Selenium Framework
INTERFACE:
An interface in the Java programming language is an abstract type that is used to specify a behavior that classes must implement. An interface also contain methods and variables just like the class but the methods declared in interface are by default abstract.
To understand this the very basic statement we write in Selenium
WebDriver driver = new Chromedriver();

In this Case WebDriver itself is an Interface. So based on this statement, WebDriver driver = new Chromedriver(); we are initializing chrome browser using Selenium WebDriver. It means we are creating a reference variable (driver) of the interface (WebDriver) and creating an Object. Here WebDriver is an Interface as mentioned earlier and Chromedriver is a class.

ABSTRACTION:
Abstraction is a process of hiding the implementation details from the user and showing only relevant details to them. It also helps to reduce programming complexity and effort.

In our Automation Framework whenever we Use Page object Model, we write all the locator in page class and use this locator in our test it means we are hiding our implementation from the user this is the simple example of using abstraction in framework.

INHERITANCE:
The process by which one class acquires the properties (instance variables) and functionalities of another class is called inheritance.

When We create a Base Class in our automation Framework to initialize WebDriver interface, waits,loggers,reports etc. and when we extend this Base Class in other classes such as Tests and Utility Class. In this case extending one class into other class is example of implementing Inheritance.

ENCAPSULATION:
Encapsulation is a mechanism of wrapping data (variables) and code together as a single unit.
All the classes which we write in our automation framework are an example of Encapsulation. 

For e.g In Page object model classes, in which we declare the WebElement locator using @FindBy and initialization of this data members will be done using Constructor to utilize those in test methods

METHOD OVERLOADING:

If a class has multiple methods having same name but different in parameters, it is known as Method Overloading.

In Implicit wait when we use different time stamps such as SECONDS, MINUTES, HOURS etc is one of the possible example of method overloading..

METHOD OVERRIDING:
If subclass or child class has the same method as declared in the parent class, it is known as method overriding in Java. 

Whenever we use a method which was already implemented/written in another class by changing its parameters this is the example of method overriding.

Please refer below video to understand the real usage of OOPS concept in framework:
  

Please refer below YouTube video to understand framework in detail:


Framework development Series 10 videos (Step by Step)


20 comments:

  1. Replies
    1. Thank You! Keep Visiting the blog for upcoming interesting articles.

      Delete
  2. Plzz provide remaining selenium answer...

    ReplyDelete
  3. Hi Hitendra,
    Thank you very very much. Its very useful information and moreover, the information is very clear and easy to understand. Once again Thank you.

    ReplyDelete
  4. Awesome. Thanks for all the details!

    ReplyDelete
  5. Sir can u post the hybrid framework tutorials

    ReplyDelete
  6. Very Good Content, appreciate it. Recommending to others..

    ReplyDelete
  7. Awesome!!Very Nice Tutorials!!Please can you add how to integrate with Bamboo? I didn't see any videos on this section on you tube.

    ReplyDelete
  8. Awesome explanation in a chronological order thank you

    ReplyDelete
  9. This comment has been removed by the author.

    ReplyDelete
  10. Good content and explanation.
    Could you please share the framework complete code.

    Thank you,
    M Venkatesh

    ReplyDelete
  11. Wonderfully explanation, can we have the git repository link of complete code.

    ReplyDelete