Java Spring Framework with Selenium Automation

Java Spring Framework with Selenium Automation

The Java Spring Framework can effectively integrate with Selenium Automation to leverage dependency injection and create clean, modular, and testable code. Combining it with the Page Factory Pattern allows you to structure your automation framework to maximize maintainability and scalability. Below is a guide to implement this:


Project Setup

1. Maven Dependencies

Add the following dependencies to your pom.xml:

<dependencies>
    <!-- Selenium Dependency -->
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>4.x.x</version>
    </dependency>
    
    <!-- Spring Framework -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.x.x</version>
    </dependency>
    
    <!-- TestNG for Testing (optional) -->
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>7.x.x</version>
        <scope>test</scope>
    </dependency>
</dependencies>

2. Project Structure

Organize your project as follows:

src/main/java
  ├── config
  │     └── SpringConfig.java
  ├── pages
  │     └── LoginPage.java
  ├── tests
  │     └── LoginTest.java
  └── utils
        └── WebDriverFactory.java

Implementation

1. Spring Configuration Class

Define a Spring configuration class to manage your beans:

package config;

import org.openqa.selenium.WebDriver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import utils.WebDriverFactory;

@Configuration
public class SpringConfig {

    @Bean
    public WebDriver webDriver() {
        return WebDriverFactory.createDriver("chrome"); // You can pass browser type dynamically
    }
}

2. WebDriver Factory

Create a utility class for WebDriver setup:

package utils;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

import java.time.Duration;

public class WebDriverFactory {

    public static WebDriver createDriver(String browser) {
        WebDriver driver;
        switch (browser.toLowerCase()) {
            case "firefox":
                System.setProperty("webdriver.gecko.driver", "path/to/geckodriver");
                driver = new FirefoxDriver();
                break;
            case "chrome":
            default:
                System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
                driver = new ChromeDriver();
                break;
        }
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
        driver.manage().window().maximize();
        return driver;
    }
}

3. Page Object with Page Factory

Leverage the Page Factory pattern for element initialization:

package pages;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class LoginPage {

    private WebDriver driver;

    @FindBy(id = "username")
    private WebElement usernameField;

    @FindBy(id = "password")
    private WebElement passwordField;

    @FindBy(id = "loginButton")
    private WebElement loginButton;

    @Autowired
    public LoginPage(WebDriver driver) {
        this.driver = driver;
        PageFactory.initElements(driver, this);
    }

    public void login(String username, String password) {
        usernameField.sendKeys(username);
        passwordField.sendKeys(password);
        loginButton.click();
    }
}

4. Test Class

Write a test class that uses Spring for dependency injection:

package tests;

import config.SpringConfig;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.WebDriver;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import pages.LoginPage;

public class LoginTest {

    private AnnotationConfigApplicationContext context;
    private WebDriver driver;
    private LoginPage loginPage;

    @BeforeEach
    public void setup() {
        context = new AnnotationConfigApplicationContext(SpringConfig.class);
        driver = context.getBean(WebDriver.class);
        loginPage = context.getBean(LoginPage.class);
        driver.get("https://example.com/login");
    }

    @Test
    public void testValidLogin() {
        loginPage.login("testuser", "password");
        // Add assertions to validate login success
    }

    @AfterEach
    public void tearDown() {
        if (driver != null) {
            driver.quit();
        }
        if (context != null) {
            context.close();
        }
    }
}

Key Benefits

  1. Separation of Concerns: Spring manages WebDriver and Page Object dependencies, reducing tight coupling.
  2. Reusability: Page Factory ensures maintainable and reusable page objects.
  3. Scalability: Adding new pages and tests is straightforward.
  4. Test Configuration: Spring simplifies test setup and teardown, centralizing configuration.

This setup provides a clean, modular, and highly testable automation framework using Java Spring, Selenium, and the Page Factory pattern.

Prakash Bojja

I have a personality with all the positives, which makes me a dynamic personality with charm. I am a software professional with capabilities far beyond those of anyone who claims to be excellent.

Post a Comment

Previous Post Next Post

Contact Form