Skip to content

bija510/selenium-maven-java

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Selenium-WebDriver

πŸ‘‹ Introduction & Why selenium?

Selenium WebDriver is a tool that allows you to automate web browsers using code, making it easier to test web applications and perform repetitive tasks. It supports multiple programming languages like Java, Python, and JavaScript, and works across various browsers such as Chrome, Firefox, and Edge. You can learn more and get started at selenium.dev.

✨ Key Features:


Selenium WebDriver is a programming interface developed by Simon Stewart in 2006.

  • Cross-Platform Support: Works on Windows, Mac, and Linux.

  • Cross-Browser Support: Compatible with major browsers like Chrome, Microsoft Edge, Firefox, and Safari.

  • Multi-Language Support: Works with top programming languages including Java, JavaScript, Python, C#, and Ruby.

  • Use Eclipse IDE to develop and manage test cases.

  • Integrates with Jenkins for CI/CD pipeline automation.

  • Supports Maven for build and dependency management.

  • Use Git (version control) and GitHub (cloud-based repository hosting) for collaboration.

  • Supports TestNG as a testing framework.

  • Huge ecosystem of plugins – offering flexibility and enhanced capabilities.

  • Generates HTML test reports using Maven, TestNG, Extent Reports, and Allure Reports.


πŸ“₯ Download, Install and Setup.

Eclipse IDE Download

Install TestNG in Eclipse

  • Help >> Eclipse Market place >> search >> testng >> Install.

Install Java JDK

  1. Download the x64 Installer (.exe) from Oracle JDK Downloads.
  2. Run the installer and complete the setup.
  3. Set environment variables:
    • Open This PC > Properties > Advanced system settings > Environment Variables.
    • Under System variables, create a new variable: JAVA_HOME = C:\Program Files\Java\jdk-21
    • Edit the Path variable and add: C:\Program Files\Java\jdk-21\bin
  4. Verify installation by running in Command Prompt: java --version

Install Apache Maven

  1. Download the Binary zip archive (apache-maven-3.9.9-bin.zip) from Apache Maven Downloads.
  2. Extract the zip file to a desired location, e.g., C:\Program Files\apache-maven-3.9.9.
  3. Set environment variables:
    • Right-click This PC β†’ Properties β†’ Advanced system settings β†’ Environment Variables.
    • Under User variables, create a new variable:
      MAVEN_HOME = C:\Program Files\apache-maven-3.9.9
    • Edit the Path variable under System variables and add:
      C:\Program Files\apache-maven-3.9.9\bin
  4. Verify installation by running in Command Prompt:

For the pom.xml dependencies

πŸ“˜ Naming conventions for Selenium identifiers

Category UI/Control type Prefix Example
Basic Button btn btnExit
Basic Text box txt txtLastName
Basic Check box chk chkReadOnly
Basic Radio button / group rad radGender
Basic Label lbl lblHelpMessage
Basic Date picker dtp dtpPublished
Basic Links/Anchor Tags lnk lnkForgotPwd
Basic Combo box cbo cboEnglish
Basic Menu mnu mnuFileOpen
Basic Sub Menu sbmnu mnuFileOpen
Basic Dropdown List / Select tag ddl ddlCountry
Basic Table tbl tblCustomer
Basic Form frm frmEntry
Basic Frame fra fraLanguage
Basic Image img imgIcon
Basic Common dialog dlg dlgFileOpen
Basic List box lst lstPolicyCodes
Basic RichTextBox rtf rtfReport
Basic TabStrip tab tabOptions
Basic Text Area txa txaDescription
Complex Spinner spn spnPages
Complex Chevron chv chvProtocol
Complex Data grid dgd dgdTitles
Complex Data list dbl dblPublisher
Complex Directory list box dir dirSource
Complex Drive list box drv drvTarget
Complex File list box fil filSource
Complex Panel/Fieldset pnl pnlGroup
Complex ProgressBar prg prgLoadFile
Complex Slider sld sldScale
Complex StatusBar sta staDateTime
Complex Timer tmr tmrAlarm
Complex Toolbar tlb tlbActions
Complex TreeView tre treOrganization

πŸ—‚οΈ Folder Structure

src/main/java
β”œβ”€β”€ com.qa.utils
    └── Base.java
    └── CommonUtils.java
    └── ExcelUtils.java
    └── ExtendReportManager.java
    └── SeleniumUtils.java
    └── PdfReader.java
    └── log4j2.xml


src/test/java
β”œβ”€β”€ tests
    └── LoginTest.java
β”œβ”€β”€ pages
    └── LoginPage.java

Configurations
β”œβ”€β”€ Config.properties

Data
β”œβ”€β”€ TestData.json

Include
β”œβ”€β”€ UploadFile.pdf, UploadFile.png, UploadFile.csv...
β”œβ”€β”€ SelectorHub.crx (browser extension)

Logs
β”œβ”€β”€ execution.logs

reports
β”œβ”€β”€ extendReport.html

Screenshots
β”œβ”€β”€ assertDemo0313_Failed_183355.png

target (Maven Report)
β”œβ”€β”€ surefire-reports
    └──index.html

test-output(Testng Report)
β”œβ”€β”€ index.html

Test Suites
β”œβ”€β”€ts-smoke.xml
β”œβ”€β”€ts-sanity.xml
β”œβ”€β”€ts-regression.xml

README.md

πŸ“ Selenium Java Framework Architecture Overview

This project is a structured Maven-based Selenium Java framework following a layered architecture for better modularity, scalability, and maintainability.


βœ… 1. Directory Structure and Components

πŸ”Ή src/main/java – Contains main framework code (Core framework logic)

Directory/File Description
com.qa.utils Stores utility and helper classes
Base.java Base class for initializing WebDriver, managing browser setup, and teardown
CommonUtils.java Reusable methods like wait handling, data formatting, etc.
ExcelUtils.java Handles reading/writing data from Excel files
ExtendReportManager.java Manages Extent Report generation for test execution
SeleniumUtils.java Utility methods for handling Selenium operations (e.g., handling alerts, frames, windows)
PdfReader.java Handles PDF file reading and verification
log4j2.xml Log configuration file for logging using Log4j

πŸ”Ή Configurations – Contains framework configuration files

File Description
Config.properties Stores global configuration (e.g., browser type, URL, timeout)

πŸ”Ή Data – Stores test data

File Description
TestData.json Stores structured test data in JSON format

πŸ”Ή Include – Stores external dependencies

File Description
UploadFile.pdf, UploadFile.png, UploadFile.csv Test files for file upload scenarios
SelectorHub.crx Browser extension for element inspection
AutoIt Scripts for handling Windows-based popups

πŸ”Ή Logs – Contains execution logs

File Description
execution.logs Logs generated during test execution

πŸ”Ή Reports – Stores test execution reports

File Description
extendReport.html Extent Report file with test summary and results

πŸ”Ή Screenshots – Contains screenshots of test execution

File Description
assertDemo0313_Failed_183355.png Screenshot of failed test for debugging

πŸ”Ή target – Stores compiled files and Maven reports

Directory/File Description
surefire-reports/index.html Test execution report generated by Maven Surefire Plugin

πŸ”Ή test-output – Stores TestNG-generated reports

File Description
index.html TestNG report with execution summary

πŸ”Ή Test Suites – Contains test suite files for TestNG execution

File Description
TS_app_smoke.xml TestNG suite file for smoke tests
TS_app_Regression.xml TestNG suite file for regression tests

βœ… 2. Framework Workflow

  1. Test Execution

    • TestNG reads the test suite file (TS_app_smoke.xml or TS_app_Regression.xml).
    • Initializes WebDriver using Base.java.
  2. Page Object Handling

    • LoginTest.java calls LoginPage.java methods.
    • LoginPage.java uses locators and actions to interact with web elements.
  3. Utilities and Configuration

    • Config.properties provides browser type, URL, and other environment settings.
    • CommonUtils, SeleniumUtils, and ExcelUtils provide helper methods.
  4. Data Handling

    • Test data is fetched from TestData.json or ExcelUtils.java.
  5. Assertions and Reporting

    • Assert statements validate expected vs actual results.
    • ExtendReportManager.java generates Extent Reports.
    • Screenshots are captured for failed cases.
  6. Logging

    • Logs are generated using log4j2.xml.
  7. Teardown

    • Browser session is closed using Base.java cleanup methods.

πŸ”€ 3. Design Patterns Used

Pattern Description Recommendation & Notes
βœ”οΈ Page Object Model (POM) Separates UI element locators and actions from test cases πŸ‘ Best practice: Keeps tests clean, maintainable, and reusable. Highly recommended for all Selenium projects.
Singleton Pattern Ensures a single instance of WebDriver Recommended: Helps manage WebDriver lifecycle consistently and avoids resource conflicts.
Data-Driven Testing Fetches test data from JSON, Excel, or CSV files Best practice: Enables running tests with multiple data sets, improves test coverage and flexibility.
❌ Page Factory Pattern (@FindBy) Uses annotations to initialize WebElement objects for locators Not recommended / Deprecated: Still works but deprecated in Selenium 4+. Can cause proxy/stale element issues. Explicit locators with WebDriver are preferred.
Decorator Pattern Enhances Selenium functions (like waits, screenshots) Recommended: Useful for extending WebDriver capabilities and adding reusable features like logging or screenshots.

❌ Why PageFactory (@FindBy) Is Deprecated

  • Proxy issues: Uses lazy proxies that can cause StaleElementReferenceException and are hard to debug.
  • No built-in waits: Leads to flaky tests due to timing problems.
  • Maintenance: Adds complexity and hides element lookup details.
  • Better alternatives: Explicit By locators with WebDriverWait are clearer and more reliable.
  • Limited modern support: Doesn’t support Selenium 4 features well.
  • Performance: Proxy creation can slow down large test suites.

πŸ”₯ Summary

βœ… Follows layered architecture for better maintainability
βœ… Implements Page Object Model for clean separation of concerns
βœ… Supports data-driven testing using JSON and Excel
βœ… Provides logging and reporting with Log4j and Extent Reports
βœ… Enables parallel and cross-browser testing using TestNG and Maven

Demo Project Overview

This project demonstrates Selenium WebDriver features from version 3 up to 4.29, including:

  1. Basic Selenium WebDriver actions
  2. PDF file reading using Apache PDFBox
  3. Comprehensive TestNG framework test cases
  4. Core Java fundamentals
  5. ChromeOptions and Chrome DevTools usage
  6. Mobile emulator testing on Chrome browser
  7. Data-driven testing with JSON, Excel, Properties files, and MySQL database
  8. File upload automation with Robot class and AutoIT
  9. Headless browser testing using HtmlUnit, ChromeDriver, FirefoxDriver, and PhantomJS
  10. Logging with Log4j framework
  11. Page Object Model design pattern demos: traditional POM and Page Factory with @FindBy annotation
  12. Singleton design pattern example
  13. Shadow DOM handling
  14. Broken links detection
  15. Use of relative/friendly locators

Key Dependencies

  • Apache PDFBox for PDF handling
  • Log4j for logging
  • Automation library for friendly locators

πŸš€ How to Execute Tests

  1. Clone the repository.
  2. Configure Config.properties with browser, URL, and timeout.
  3. Run tests using:
mvn clean test
  1. View reports at:
  • target/surefire-reports/index.html
  • reports/extendReport.html
  • Allure report via:
allure serve allure-results

▢️ Running Specific Test Suites

Run a suite with:

mvn clean test -DtestngFile=YourTestSuite.xml

Examples:

mvn clean test -DtestngFile=smoke-test-suite.xml
mvn clean test -DtestngFile=sanity-test-suite.xml
mvn clean test -DtestngFile=regression-test-suite.xml
  • Adjust <suiteXmlFile> path if suite files are not in Test Suites.
  • Change dependency scope from <scope>test</scope> to <scope>compile</scope> to run from command line.

▢️ Parallel Execution Demo

  • Parallel suite located in Test Suites/Parallel...

πŸ“Š Report

  1. Maven surefire report target/surefire-reports/index.html

  2. TestNG Report test-output/index.html

  3. Allure Report install scoop window powershell : iwr -useb get.scoop.sh | iex install allure command: scoop install allure cmd command : allure serve allure-results

πŸ‘¨β€πŸ’» Author

Bijaya Chhetri | Automation Engineer | πŸ”— github.com/bija510

πŸ“„ License

This project is licensed under the MIT License.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages