Advanced Test Automation Framework for SauceDemo Application
Demonstrating Factory Design Pattern + Page Object Model
- 🎯 Overview
- 🧠 Factory Design Pattern
- 🏗️ Framework Architecture
- ⚙️ Tech Stack
- 🎨 Design Patterns
- 🚀 Getting Started
- 🧪 Running Tests
- ✨ Key Features
- 📊 Test Flow
- 🎯 Sample Output
- 💡 Why Factory Pattern
- 🔮 Future Enhancements
- 👨💻 Author
This project demonstrates an enterprise-grade Test Automation Framework built using Selenium WebDriver, Java, and TestNG. It implements the Factory Design Pattern to automate the SauceDemo e-commerce application.
🏭 Factory Pattern → Smart browser instantiation
📄 Page Object Model → Modular & maintainable code
🧵 Thread-Safe Design → Parallel execution ready
🌐 Cross-Browser Support → Chrome | Firefox | Edge
🔄 CI/CD Ready → Jenkins | GitHub Actions compatible
The Factory Pattern is a Creational Design Pattern that provides an interface for creating objects without exposing the instantiation logic to the client.
// ❌ Without Factory Pattern
WebDriver driver = new ChromeDriver(); // Tightly coupled
// ✅ With Factory Pattern
WebDriver driver = DriverFactory.createInstance("chrome"); // Flexible & extensible
DriverManager.setDriver(driver);| Benefit | Description |
|---|---|
| 🎯 Centralized Logic | All driver creation in one place |
| 🔄 Reduces Duplication | Write once, use everywhere |
| 📈 Easy Extension | Add new browsers without breaking existing code |
| 🛡️ Better Maintainability | Change once, affect all |
| ⚡ Parallel Testing | Thread-safe driver management |
| 🧪 Testability | Easy to mock and test |
📦 SeleniumFactoryDesign/
┃
┣━━ 📂 src/
┃ ┣━━ 📂 main/java/
┃ ┃ ┣━━ 🏭 factory/
┃ ┃ ┃ ┣━━ ⚙️ DriverFactory.java # Creates WebDriver instances
┃ ┃ ┃ ┗━━ 🧵 DriverManager.java # Thread-safe driver handling
┃ ┃ ┃
┃ ┃ ┗━━ 📄 pages/
┃ ┃ ┣━━ 🔐 LoginPage.java # Login page object
┃ ┃ ┗━━ 🛒 InventoryPage.java # Product page object
┃ ┃
┃ ┗━━ 📂 test/java/
┃ ┗━━ 🧪 tests/
┃ ┣━━ 🔧 BaseTest.java # Test setup/teardown
┃ ┣━━ ✅ LoginTest.java # Login validations
┃ ┗━━ 🎯 AddToCartTest.java # Cart operations
┃
┣━━ 📋 pom.xml # Maven dependencies
┣━━ 🎯 testng.xml # TestNG configuration
┗━━ 📖 README.md # Project documentation
┌─────────────────────────────────────────┐
│ 🧪 Test Layer │ ← LoginTest, AddToCartTest
│ (TestNG Tests) │
└─────────────────┬───────────────────────┘
│
┌─────────────────▼───────────────────────┐
│ 📄 Page Object Layer │ ← LoginPage, InventoryPage
│ (UI Elements & Actions) │
└─────────────────┬───────────────────────┘
│
┌─────────────────▼───────────────────────┐
│ 🏭 Factory Layer │ ← DriverFactory, DriverManager
│ (Driver Creation) │
└─────────────────┬───────────────────────┘
│
┌─────────────────▼───────────────────────┐
│ 🌐 WebDriver Layer │ ← Chrome, Firefox, Edge
│ (Browser Instances) │
└─────────────────────────────────────────┘
| Component | Technology | Version | Purpose |
|---|---|---|---|
| ☕ Language | Java | 21 | Core programming language |
| 🤖 Automation | Selenium WebDriver | 4.23.0 | Browser automation |
| 🧪 Test Framework | TestNG | 7.9.0 | Test execution & assertions |
| 🔨 Build Tool | Maven | 3.9+ | Dependency management |
| 📊 Reporting | TestNG/Allure | - | Test reports & analytics |
| 🎨 Design Patterns | Factory + POM | - | Code organization |
DriverFactory.java encapsulates all browser initialization logic:
public class DriverFactory {
public static WebDriver createInstance(String browser) {
WebDriver driver;
switch (browser.toLowerCase()) {
case "chrome":
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--disable-save-password-bubble");
driver = new ChromeDriver(chromeOptions);
break;
case "firefox":
driver = new FirefoxDriver();
break;
case "edge":
driver = new EdgeDriver();
break;
default:
throw new IllegalArgumentException("Browser not supported: " + browser);
}
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
return driver;
}
}Each page is represented as a separate class:
📄 LoginPage.java
┣━━ 🔑 Elements: usernameField, passwordField, loginButton
┗━━ ⚡ Actions: login(), isDisplayed()
📄 InventoryPage.java
┣━━ 🛒 Elements: productButtons, cartBadge
┗━━ ⚡ Actions: addToCart(), getCartCount()Benefits:
- ✅ Code reusability across tests
- ✅ Easy maintenance when UI changes
- ✅ Better readability and organization
- ✅ Reduces test fragility
☑️ Java 21 or higher
☑️ Maven 3.9+
☑️ Chrome/Firefox/Edge browser
☑️ IDE (IntelliJ IDEA / Eclipse)- Clone the repository
git clone https://github.com/sarankumar/selenium-factory-pattern.git
cd selenium-factory-pattern- Install dependencies
mvn clean install- Verify setup
mvn test# Right-click on testng.xml
Run As → TestNG Suite# Run all tests
mvn clean test
# Run specific test class
mvn test -Dtest=LoginTest
# Run with specific browser
mvn test -Dbrowser=firefoxThe testng.xml configuration enables parallel execution:
<suite name="SauceDemo-Factory-Suite" parallel="tests" thread-count="2">
<!-- Chrome Tests -->
<test name="ChromeSuite">
<parameter name="browser" value="chrome"/>
<classes>
<class name="tests.LoginTest"/>
<class name="tests.AddToCartTest"/>
</classes>
</test>
<!-- Firefox Tests -->
<test name="FirefoxSuite">
<parameter name="browser" value="firefox"/>
<classes>
<class name="tests.LoginTest"/>
<class name="tests.AddToCartTest"/>
</classes>
</test>
</suite>Result: Tests run simultaneously on Chrome and Firefox! ⚡
| Feature | Description |
|---|---|
| 🏭 Factory Pattern | Browser-agnostic WebDriver instantiation |
| 🔐 Auto-Handling | Manages Chrome password-manager popups |
| 📦 Modular POM | Organized page objects for easy maintenance |
| 🧵 Thread-Safe | ThreadLocal WebDriver for parallel execution |
| ⚡ Parallel Tests | TestNG parallel execution support |
| 🔄 CI/CD Ready | Compatible with Jenkins & GitHub Actions |
| 🌐 Cross-Browser | Chrome, Firefox, Edge support |
| 📊 Rich Reporting | TestNG HTML reports with screenshots |
✅ SOLID principles adherence
✅ DRY (Don't Repeat Yourself) code
✅ Separation of concerns
✅ Explicit waits over implicit
✅ Meaningful test and method names
✅ Proper exception handling
✅ Comprehensive logging
🚀 START
↓
1️⃣ Factory creates browser instance (Chrome/Firefox/Edge)
↓
2️⃣ Navigate to https://www.saucedemo.com
↓
3️⃣ Enter credentials (standard_user / secret_sauce)
↓
4️⃣ Click login button
↓
5️⃣ Verify landing on Inventory Page
↓
6️⃣ Add product "Sauce Labs Backpack" to cart
↓
7️⃣ Assert cart badge shows "1"
↓
8️⃣ Cleanup & close browser
↓
🏁 END
| Test Case | Validation | Status |
|---|---|---|
| 🔐 Valid Login | User redirected to Inventory Page | ✅ PASS |
| 🛒 Add to Cart | Cart badge increments correctly | ✅ PASS |
| 🔍 Page Title | Correct title displayed | ✅ PASS |
| 🏷️ Product Display | Products visible on inventory | ✅ PASS |
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running TestSuite
╔════════════════════════════════════════════════╗
║ Test Execution Report ║
╚════════════════════════════════════════════════╝
🔵 Chrome Suite
✅ PASSED: LoginTest.validLogin() - 3.2s
✅ PASSED: AddToCartTest.addItemToCart() - 2.8s
🟠 Firefox Suite
✅ PASSED: LoginTest.validLogin() - 3.5s
✅ PASSED: AddToCartTest.addItemToCart() - 3.1s
╔════════════════════════════════════════════════╗
║ Total Tests: 4 | Passed: 4 | Failed: 0 ║
║ Execution Time: 12.6 seconds ║
╚════════════════════════════════════════════════╝
[INFO] All tests executed successfully! 🎉// ❌ Hard to maintain
if (browser.equals("chrome")) {
driver = new ChromeDriver();
} else if (browser.equals("firefox")) {
driver = new FirefoxDriver();
}
// Repeated in every test class... 😱// ✅ Clean, reusable, maintainable
WebDriver driver = DriverFactory.createInstance(browser);
DriverManager.setDriver(driver);| Aspect | Impact |
|---|---|
| 🎯 Flexibility | Easy to add Safari, Opera, or remote browsers |
| 🧩 Separation | Driver setup isolated from test logic |
| 🧵 Thread Safety | Works seamlessly with ThreadLocal for parallel runs |
| 🛡️ Stability | Centralized browser options reduce flakiness |
| 📈 Scalability | Supports distributed execution (Selenium Grid) |
| 🔧 Maintainability | Change once, impact everywhere |
Phase 1: Enhanced Reporting
├── 📊 Integrate Allure Reports
├── 📸 Automatic screenshot on failure
└── 📹 Video recording of test execution
Phase 2: CI/CD Integration
├── 🔄 GitHub Actions pipeline
├── 🔧 Jenkins integration
└── 📦 Docker containerization
Phase 3: Advanced Features
├── 🌐 Selenium Grid support
├── ☁️ BrowserStack/Sauce Labs integration
├── 📁 Property files for configuration
└── 🗃️ Database validation
Phase 4: Performance & Security
├── ⚡ API testing integration
├── 🔒 Security testing
└── 📈 Performance benchmarking
- Design Patterns in Test Automation
- Page Object Model Best Practices
- Selenium Framework Development
By combining the Factory Design Pattern with Page Object Model, this framework achieves:
✨ Clean & Elegant Architecture
✨ High Maintainability & Readability
✨ Scalable Parallel Testing
✨ Reliable & Stable Execution
✨ Enterprise-Grade Quality
💡 "The Factory Pattern transforms complex browser initialization into a simple, reusable, and powerful abstraction - making your automation framework future-proof and scalable."