Selenium WebDriver Automation Framework in Java

A Selenium-based modular automation framework in Java for web application testing.

·

9 min read


Github Repository

Java Selenium Automation Framework

The GitHub repository linked above serves as the foundation for our Selenium automation framework project. Inside, you'll find a comprehensive Java-based framework designed to streamline web testing and automation tasks.

Here's what you can expect to find:

  1. Framework Architecture: Dive into the src directory to explore the architecture of our Selenium automation framework. The framework is structured to promote scalability, maintainability, and reusability of test scripts and resources.

  2. Test Scripts: Browse through the test directory to discover a collection of test scripts demonstrating various automation scenarios. These scripts leverage the power of Selenium WebDriver and Java to interact with web elements, perform actions, and validate outcomes.

  3. Utilities: Explore the utilities package to find utility classes with methods that facilitate common automation tasks, these components aim to streamline your automation workflow.

Feel free to clone the repository to explore the source code, experiment with test scripts, or adapt the framework to suit your specific testing needs. Whether you're a QA engineer, a software developer, or a curious learner, this repository provides valuable insights into building efficient Selenium automation frameworks with Java.


Introduction

This blog post introduces a Selenium-based modular automation framework developed in Java for web application testing. The framework is designed to be scalable, maintainable, and efficient, allowing you to automate tests for your web applications.


Key Features

FeatureDescription
Modular DesignThe framework follows a modular architecture, promoting separation of concerns and improving maintainability.
Page Object Model (POM)It utilises the Page Object Model pattern for better code organisation and reusability.
TestNG IntegrationTestNG is integrated for robust test management, parallel execution, and reporting capabilities.
Logging and ReportingLogging and reporting mechanisms provide detailed insights into test execution.
Configuration ManagementExternal configuration files manage test environment configurations.
Cross-Browser TestingThe framework supports testing across various browsers.
Parallel ExecutionTest suites can be executed concurrently, reducing execution time.
CustomisableFramework components are designed to be customisable for specific project requirements.

Project Structure

The project structure is well-organised, with clear separation of functionalities. The provided image offers a visual representation of the structure.

.
├── logs
│   └── automation.log
├── pom.xml
├── reports
│   └── AutomationReport.html
├── runTests.sh
├── src
│   ├── main
│   │   ├── java
│   │   │   └── com
│   │   │       └── example
│   │   │           ├── assertions
│   │   │           │   └── SoftAssertionManager.java
│   │   │           ├── browserCapabilities
│   │   │           │   ├── ChromeCapabilities.java
│   │   │           │   ├── EdgeCapabilities.java
│   │   │           │   └── FirefoxCapabilities.java
│   │   │           ├── browserManager
│   │   │           │   ├── BrowserManager.java
│   │   │           │   ├── LocalBrowserManager.java
│   │   │           │   └── RemoteBrowserManager.java
│   │   │           ├── configManager
│   │   │           │   ├── ConfigFactory.java
│   │   │           │   └── FMConfig.java
│   │   │           ├── driverManager
│   │   │           │   ├── DriverManager.java
│   │   │           │   ├── LocalChromeDriverManager.java
│   │   │           │   ├── LocalEdgeDriverManager.java
│   │   │           │   ├── LocalFirefoxDriverManager.java
│   │   │           │   ├── RemoteChromeDriverManager.java
│   │   │           │   ├── RemoteEdgeDriverManager.java
│   │   │           │   └── RemoteFirefoxDriverManager.java
│   │   │           ├── enums
│   │   │           │   ├── BrowserType.java
│   │   │           │   ├── EnvironmentType.java
│   │   │           │   └── LocatorType.java
│   │   │           ├── listeners
│   │   │           │   └── ExtentTestListener.java
│   │   │           ├── logManager
│   │   │           │   └── LoggerManager.java
│   │   │           ├── pagefactory
│   │   │           │   ├── HomePage.java
│   │   │           │   └── LoginPage.java
│   │   │           ├── reportManager
│   │   │           │   └── ExtentReportManager.java
│   │   │           ├── testbuilder
│   │   │           │   └── TestBuilder.java
│   │   │           └── utilities
│   │   │               ├── CommonUtil.java
│   │   │               ├── ExtentReportNGUtil.java
│   │   │               └── StarterKit.java
│   │   └── resources
│   │       ├── local.properties
│   │       ├── log4j2.xml
│   │       └── remote.properties
│   └── test
│       └── java
│           └── com
│               └── example
│                   └── testcases
│                       ├── HomePageTest.java
│                       └── LoginPageTest.java
└── testng.xml

Packages and Classes

The blog dives into the details of the included packages and classes, explaining their functionalities.

Here's a summary:

  • com.example.assertions: Handles soft assertions in TestNG tests.

  • com.example.browserCapabilities: Provides browser capabilities for remote execution.

  • com.example.browserManager: Manages the creation of WebDriver instances.

  • com.example.configManager: Handles configuration properties.

  • com.example.driverManager: Manages ThreadLocal storage of WebDriver instances.

  • com.example.enums: Defines enumerations for browsers, environments, and locators.

  • com.example.listeners: Includes listeners for ExtentReports.

  • com.example.logging: Manages logging throughout the framework.

  • com.example.reportManager: Manages ExtentTest instances.

  • com.example.testbuilder: Provides a fluent interface for building and configuring WebDriver instances.

  • com.example.utilities: Offers utility methods for interacting with WebDriver and performing validations.

💡
Each class within these packages has a specific role in the framework's operation

Framework Diagram

An architectural diagram included in the blog illustrates how different components interact during test execution, providing a comprehensive understanding of the framework's workflow.


Prerequisites

Before leveraging the selenium-automation-framework-java for automated testing, ensure the following prerequisites are met:

  • Java Development Kit (JDK): Install the latest version of JDK to support Java development.

  • Apache Maven: Ensure Maven is installed and properly configured on your system for project build management.

  • Integrated Development Environment (IDE): Utilise a Java IDE such as Eclipse or IntelliJ IDEA for convenient development and execution.

  • Git: Install Git, a distributed version control system, to manage the source code efficiently.


Dependencies

The framework relies on the following dependencies to deliver its functionality:

  • Selenium WebDriver: A powerful web automation framework facilitating web testing across various browsers.

  • TestNG: A robust testing framework for Java applications, offering comprehensive testing capabilities.

  • Owner: A Java properties management library for streamlined configuration handling.

  • SLF4J API, SLF4J Simple: Logging facade and simple implementation for logging in Java.

  • Log4j Core, Log4j API: Logging library and API for Java, enhancing logging capabilities.

  • Extent Reports: A reporting library for test automation, providing detailed and visually appealing test reports.

  • Commons IO: Utility classes for performing I/O operations in Java.


Setup

Setting up the selenium-automation-framework-java is straightforward:

  1. Clone the Repository: Clone the repository to your local machine using the provided Git clone command.

     git clone https://github.com/harryvn/selenium-automation-framework-java.git
    
  2. Execute the Command: Run the provided Maven command to execute the test suite seamlessly.

     mvn clean test
    

Test Parameters Configuration

A dedicated section explains how to configure various test parameters through a configuration file. The blog post includes details on the parameters, their descriptions, and example use cases demonstrating how to run tests with different configurations.

### SAMPLE ###
# Configuration file for setting up test parameters
browser=CHROME
url=https://the-internet.herokuapp.com
headless=false
recordVideo=false
remoteSeleniumGridUrl=http://localhost:4444
env=REMOTE
username=tomsmith
password=SuperSecretPassword!
ParameterDescription
browserSpecifies the web browser to use for testing (e.g., CHROME, EDGE, FIREFOX).
urlDefines the URL of the application under test, enabling seamless navigation to specific web pages.
headlessDetermines whether to run the browser in headless mode, useful for executing tests without GUI interaction.
recordVideoIndicates whether to record video of the test execution, facilitating post-execution analysis and review.
remoteSeleniumGridUrlSpecifies the URL of the remote Selenium Grid server for distributed testing.
envSpecifies the testing environment (e.g., LOCAL, REMOTE) to target during test execution.
usernameProvides username for authentication.
passwordProvides password for authentication.
parallelDetermines whether to run tests in parallel, optimising test suite execution time.
💡
Users can customise these parameters based on their testing requirements, adjusting values as necessary to tailor the testing experience.

Example Use Cases

  1. Executing Tests without Any Additional Parameters:

     mvn clean test
    

Tests will run with default configuration as no additional parameters are specified.


  1. Local Testing with Chrome Browser:

     mvn clean test -Denv=LOCAL -Dbrowser=CHROME
    

Tests will run locally with Chrome browser and default value will be used for other parameters.


  1. Remote Testing with Chrome Browser and Video Recording:

     mvn clean test -Denv=REMOTE -Dbrowser=CHROME -DremoteSeleniumGridUrl=<ip-address:port> -DrecordVideo=true
    

Tests will run on remote Selenium Grid with Chrome browser with video recording enabled.


  1. Remote Testing with Chrome Browser in Headless Mode:

     mvn clean test -Denv=REMOTE -Dbrowser=CHROME -DremoteSeleniumGridUrl=<ip-address:port> -Dheadless=true
    

Tests will run on remote Selenium Grid with Chrome browser in headless mode.


  1. Authentication Testing with Username and Password:

     mvn clean test -Denv=REMOTE -Dusername=myusername -Dpassword=mypassword
    

Tests will run on remote Selenium Grid with provided username and password for authentication.


  1. Running Tests in Parallel:

     mvn clean test -Dparallel=tests
    

Tests will run in parallel reducing overall execution time.


  1. Specifying Application URL for Testing:

     mvn clean test -Durl=<url-of-site-under-test>
    

Tests will run using the specified URL of the application under test, enabling testing across various environments (e.g., staging, production) and seamless integration with continuous integration (CI) pipelines through dynamic URL specification.


Convenient Script

To further streamline the testing process, the repository includes a shell script named runTests.sh. This script automates the execution of tests based on user inputs, offering options for browser selection, local or remote testing, Selenium Grid configuration, video recording, parallel execution, and headless mode.

Usage

  • Ensure the script has executable permissions:

      chmod +x runTests.sh
    
  • Users can simply run the runTests.sh script and follow the prompts to configure and execute test scenarios effortlessly.

      ./runTests.sh
    

The script prompts the user to select various options and constructs the appropriate Maven command to execute the Selenium tests.

Below is an example usage of the script:

Note: Video recording is not supported in headless mode.

💡
For detailed explanations of each option and additional information, refer to the comments within the script itself.

Conclusion

This blog post offers a comprehensive overview of the Selenium automation framework in Java. It covers the key features, project structure, functionalities of included packages and classes, framework architecture, setup instructions, and a convenient script for test execution. By following this guide, you can gain a solid understanding of the framework and leverage it to automate your web application testing process.

The source code is available on GitHub. If you find the framework helpful, be sure to star the repository to show your support!

To setup and execute automation scripts on remote selenium grid refer this article.

If you find this article helpful or have any suggestions, reach out to me on LinkedIn.

Thank you and keep learning!


References

Selenium

TestNG

ExtentReports

Maven

Log4J

The Internet Heroku App