Selenium For Custom Workflows
Selenium For Custom Workflows

Click Here To See The Project Codebase

Use of selenium with design patterns as chain of responsibility for customize the workflows.

Selenium Chain of Responsibility

This project automates a Decathlon shopping scenario with Selenium while demonstrating a clean, pattern-driven architecture. The repository is intentionally small but showcases the following ideas:

  • Chain of Responsibility to model multi-step user journeys as composable handlers.
  • Template/Hook-based base class for WebDriver orchestration, encapsulating Selenium boilerplate in a reusable bot.
  • Value-level validation utilities for static tests that illustrate simple, deterministic rule sets.

Architecture Overview

High-level flow

  1. Main launches the shopping use case by delegating to ComplexDecathlon.
  2. ComplexDecathlon wires a sequence of handler objects that each perform one browser action and then invoke the next handler.
  3. DecathlonBot (the concrete bot) extends the abstract Bot, which owns WebDriver lifecycle and primitive user interactions such as clicking, typing, scrolling, and selecting options.
  4. At the end of the chain the bot is quit, ensuring the driver resources are released.

Module layout

  • src/main/java/testing/project/Main.java — entry point that triggers the Decathlon scenario.
  • src/main/java/testing/project/bot/ — core browser automation abstraction:
    • Bot encapsulates ChromeDriver setup, waits, and keyboard/mouse helpers.
    • decathlon/DecathlonBot provides Decathlon-specific configuration (URL, timeouts) through ConnectionInformation.
  • src/main/java/testing/project/bot/decathlon/usecases/ — use case composition:
    • ComplexDecathlon builds and executes the handler chain for the shopping flow.
    • chainofresponsibility/Handler defines the minimal contract for chaining.
    • chainofresponsibility/DecathlonBaseHandler stores the shared bot and delegates to the next link.
    • chainofresponsibility/steps/* contains concrete handlers such as accepting cookies, searching products, applying filters, picking an item, and validating the cart.
  • src/main/java/testing/project/bot/decathlon/statictests/utils/ — standalone validation utilities used by unit tests (email, password, physical attributes, comments, cart quantity, registration rules).
  • src/test/java/util/ — JUnit tests that exercise the static validators.

Key Patterns and Concepts

Chain of Responsibility

The chain-of-responsibility pattern is used to represent the ordered browser actions required for the Decathlon journey. Each handler performs exactly one concern and then calls super.executeNext() to advance the chain. This keeps responsibilities isolated and makes it trivial to reorder, insert, or remove steps without changing other classes.

Implementation highlights

  • Handler contract — Handler defines setNext and executeNext, creating a minimal protocol for chaining without leaking implementation details.
  • Base link — DecathlonBaseHandler holds the shared DecathlonBot instance and provides a default executeNext that only forwards to the next handler if one is present.
  • Concrete steps — Each class in steps/ overrides executeNext to run its browser action before delegating. For example, DecathlonAcceptCookies clicks the consent button and pauses, then invokes super.executeNext() to continue the flow.
  • Chain wiring — ComplexDecathlon instantiates the handlers, links them with setNext, and kicks off the sequence by calling executeNext on the first step.

Template-style base class for Selenium bots

Bot encapsulates WebDriver lifecycle management plus reusable user interactions. Concrete bots only supply configuration (URL, timeout) and can focus on domain-specific actions instead of Selenium boilerplate.

Implementation highlights

  • Lifecycle — initialize sets up the driver, waits, and Actions provider, while connect and quit open and close the browser respectively.
  • Interaction primitives — Protected helpers abstract common gestures: clicking by locator, typing with paced keystrokes, selecting dropdown options, scrolling, navigating history, and keyboard navigation of autocomplete lists.
  • Configuration injection — DecathlonBot passes Decathlon-specific constants to the base constructor, keeping configuration decoupled from behavior.

Validation utilities for deterministic checks

The statictests/utils package showcases straightforward validation logic without WebDriver dependencies. They illustrate pure functions that are easy to test and reuse.

Examples

  • Email syntax rules — EmailValidator.isValidEmail enforces single @, valid domain segments, allowed extensions, and whitespace checks.
  • Registration guard — RegistrationValidator.canRegister composes email and password validation to model a simple gatekeeper for account creation.

Object-oriented principles in practice

  • Single Responsibility — Each handler encapsulates one step of the UI flow; validators each own one rule set; Bot centralizes driver mechanics.
  • Open/Closed — New UI steps can be added by implementing Handler and linking it into the chain without modifying existing handlers.
  • Encapsulation — WebDriver details are hidden behind Bot’s methods; consumers interact through intention-revealing operations (e.g., clickByXpathscrollToBottom).
  • Composition over inheritance — The flow is composed by linking handler instances at runtime rather than hard-coding logic in a monolithic method.

Extending the project

  • Add a new UI step: create a new class under chainofresponsibility/steps/ extending DecathlonBaseHandler, implement its executeNext, and connect it inside ComplexDecathlon with setNext calls.
  • Swap environments: subclass Bot to provide alternative URLs or timeouts (e.g., staging vs. production) while reusing the same handler chain.
  • Enrich validation: augment the statictests/utils validators with additional rules and add matching tests under src/test/java/util/ to keep the suite deterministic.

Running the validators

The repository includes JUnit tests for the static validators. Run them with Maven:

mvn test

They execute without needing a browser because they only cover the pure validation utilities.