This is a robust Selenium-based automation framework for testing web applications. It's built with Java, TestNG, Maven, Log4J2, and Allure Reporting, and leverages the Page Object Model (POM) for efficient test design. The framework's WebDriver management system is designed for flexibility and maintainability, utilizing the Abstract Factory, Factory Method, and Thread Local patterns to handle different browser types and ensure thread safety.
- Prerequisites
- Setup
- Project Structure
- Configuration
- Running Tests
- Reporting
- WebDriver Management Approach
- Java 17+
- Maven 3.8+
- Chrome/Firefox/Edge WebDriver binaries (managed via SeleniumManager)
- IDE (IntelliJ recommended)
- Clone the repository:
git clone https://github.com/alhusseinzaghloul/Selenium-Test-Automation-Framework-for-Ecommerce.git
- Install dependencies:
mvn clean install
Selenium-Test-Automation-Framework-for-Ecommerce-Website/
├── .idea/ # IntelliJ configuration
├── src/
│ ├── main/
│ │ ├── java/
│ │ │ ├── drivers/ # WebDriver management
│ │ │ │ ├── AbstractDriver.java
│ │ │ │ ├── ChromeDriver.java
│ │ │ │ ├── EdgeDriver.java
│ │ │ │ ├── FirefoxDriver.java
│ │ │ │ ├── GUIDriver.java
│ │ │ │ └── WebDriverOptionsAbstract.java
│ │ │ ├── listeners/ # TestNG event listeners
│ │ │ │ └── TestNGListeners.java
│ │ │ ├── pages/ # Page Object Model (POM)
│ │ │ │ ├── CartPage.java
│ │ │ │ ├── CheckoutComplete.java
│ │ │ │ ├── CheckoutInfoPage.java
│ │ │ │ ├── CheckoutOverviewPage.java
│ │ │ │ ├── ConfirmationPage.java
│ │ │ │ ├── HomePage.java
│ │ │ │ └── LoginPage.java
│ │ │ ├── utils/ # Utility classes
│ │ │ │ ├── AllureUtils.java
│ │ │ │ ├── BrowserActions.java
│ │ │ │ ├── ElementsActions.java
│ │ │ │ ├── FilesUtils.java
│ │ │ │ ├── CustomSoftAssertion.java
│ │ │ │ ├── JsonUtils.java
│ │ │ │ ├── LogsUtils.java
│ │ │ │ ├── PropertiesUtils.java
│ │ │ │ ├── ScreenshotUtils.java
│ │ │ │ ├── Scrolling.java
│ │ │ │ ├── TImeStampUtils.java
│ │ │ │ ├── Validations.java
│ │ │ │ └── Waits.java
│ │ │ └── resources/ # Configuration files
│ │ │ ├── allure.properties
│ │ │ ├── environment.properties
│ │ │ ├── log4j2.properties
│ │ │ ├── waits.properties
│ │ │ └── web.properties
│ └── test/
│ ├── java/
│ │ └── tests/ # Test cases
│ │ ├── CartTest.java
│ │ ├── EndToEndScenariosTest.java
│ │ ├── LoginTest.java
│ │ └── HomePageTest.java
│ && resources/ # Test data
│ └── testData.json
└── test-outputs/ # Test artifacts
├── allure-results/ # Allure report data
├── Logs/ # Log files
├── screenshots/ # Screenshot captures
└── target/ # Build output
├── .gitignore # Git ignore rules
├── pom.xml # Maven configuration
└── testng.xml # TestNG suite file
Modify environment.properties:
loginPageURL=https://www.saucedemo.com/
homePageURL=https://www.saucedemo.com/inventory.html
appTitle=Swag Labs
errorMSG=Epic sadface: Username and password do not match any user in this serviceConfigure logging in log4j2.properties:
appender.console.type=Console
appender.console.name=STDOUT
appender.console.layout.type=PatternLayout
appender.console.layout.pattern=%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%nAdjust implicit/explicit wait timeouts in waits.properties:
explicit.wait=10- Run all tests:
mvn clean test - Generate Allure report:
allure serve test-outputs/allure-results
The framework uses Allure Reports for detailed test results. After execution:
- Open
test-outputs/allure-resultsin your browser. - View aggregated reports with timelines, steps, and screenshots.
This framework employs a thread-safe mechanism to manage Selenium WebDriver instances using ThreadLocal, ensuring each test thread has its own isolated WebDriver instance for parallel execution. The design leverages the factory design pattern and adheres to the single responsibility principle for better maintainability and scalability.
-
GUIDriver Class:
- Manages WebDriver instances in a thread-safe manner using
ThreadLocal<WebDriver>. - Initializes drivers based on browser type, retrieves them for use, and quits them to free resources.
- Uses a factory method to instantiate the appropriate browser-specific driver factory.
- Manages WebDriver instances in a thread-safe manner using
-
AbstractDriver Class:
- Defines the contract for starting a WebDriver instance.
- Extended by browser-specific factories (e.g.,
ChromeFactory,FirefoxFactory).
-
WebDriverOptionsAbstract Interface:
- Provides a generic method to configure browser-specific options (e.g.,
ChromeOptions). - Implemented by each browser factory for customized settings.
- Provides a generic method to configure browser-specific options (e.g.,
-
Browser-Specific Factories:
- Handle the creation and configuration of their respective WebDriver instances.
- Each factory is responsible for its browser's driver creation and option configuration.
- Factory Design Pattern: The
GUIDriverclass uses a factory method to determine and instantiate the correct browser factory based on the browser name, decoupling driver creation from the main management class. - Single Responsibility Principle: Each class has a clearly defined responsibility:
GUIDriver: Manages the lifecycle of WebDriver instances per thread.AbstractDriver: Defines the interface for starting a driver.WebDriverOptionsAbstract: Defines the interface for configuring browser options.- Browser factories: Handle the creation and configuration of specific WebDriver instances.
- Initialization: The
GUIDriverconstructor takes a browser name, uses the factory method to get the appropriate driver factory, starts the driver, and stores it inThreadLocal. - Access:
getDriver()andget()retrieve the current thread’s driver. - Cleanup:
quitDriver()closes the driver and removes it fromThreadLocal. - Options Configuration: Factories configure settings like headless mode based on execution type.
- Thread Safety: Isolated drivers per thread for safe parallel execution.
- Extensibility: Add new browsers by creating new factory classes without modifying existing code.
- Maintainability: Clear separation of concerns and well-defined responsibilities.
- Resource Management: Ensures proper cleanup of WebDriver instances.
- Modular Design: Separation of concerns via POM, utilities, and listeners.
- Data-Driven Testing: JSON-based test data in
testData.json. - Logging & Screenshots: Automated logs and screenshot captures.
- CI/CD Ready: Maven + TestNG setup for seamless integration.
- Single Responsibility Principle:
- Each class has a single responsibility (e.g., page classes handle element interactions, utility classes handle specific operations).
- Test classes focus solely on test logic, while configuration and reporting are handled separately.
- Improves maintainability and reduces side effects during updates.
- Java Best Practices:
- Follows Java naming conventions (
camelCasefor variables,PascalCasefor classes). - Javadoc comments for all public methods and classes.
- Clear method names (e.g.,
performLogin(),validateCartContents()). - Consistent code formatting for readability.
- Follows Java naming conventions (