Wednesday, December 17, 2025

thumbnail

Maven Project Setup for Selenium Testing

 Maven Project Setup for Selenium Testing


Maven is a build automation tool used primarily for Java projects, which also helps manage dependencies, build configurations, and project structures. Selenium is a powerful tool for automating web browsers. Using Maven with Selenium allows you to easily manage dependencies, configure test execution, and handle reports and build lifecycles efficiently.


This guide will walk you through setting up a Maven project for Selenium testing.


Step 1: Install Prerequisites


Before you begin, make sure you have the following installed:


Java Development Kit (JDK): Download and install JDK (preferably JDK 8 or higher).


Verify installation: java -version


Apache Maven: Download and install Maven from Maven's official website

.


Verify installation: mvn -version


Selenium WebDriver: We will configure this using Maven dependencies.


IDE (Integrated Development Environment): It’s recommended to use an IDE like Eclipse, IntelliJ IDEA, or VS Code for easier project management.


Step 2: Create a New Maven Project


You can create a Maven project using your IDE (like IntelliJ IDEA or Eclipse) or from the command line.


Option 1: Using IDE (IntelliJ IDEA or Eclipse)


In IntelliJ IDEA:


Click File > New > Project.


Select Maven as the project type.


Set the GroupId (e.g., com.example) and ArtifactId (e.g., selenium-tests).


Choose your JDK version.


Finish the setup.


In Eclipse:


Click File > New > Maven Project.


Follow the wizard and set up your GroupId and ArtifactId.


Option 2: Using Command Line


If you want to create the Maven project from the command line, navigate to the directory where you want to create your project and run the following command:


mvn archetype:generate -DgroupId=com.example -DartifactId=selenium-tests -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false



This creates a basic Maven project.


Step 3: Add Selenium Dependencies to pom.xml


Now that you have a Maven project, you need to add the necessary Selenium WebDriver dependencies to the pom.xml file to use Selenium for browser automation.


Here’s how to modify the pom.xml to include Selenium, JUnit, and other necessary dependencies.


1. Add Selenium Dependency


Open your pom.xml and add the following Selenium WebDriver dependency inside the <dependencies> section:


<dependencies>

    <!-- Selenium WebDriver dependency -->

    <dependency>

        <groupId>org.seleniumhq.selenium</groupId>

        <artifactId>selenium-java</artifactId>

        <version>4.8.0</version> <!-- Use the latest stable version -->

    </dependency>


    <!-- JUnit dependency (for running tests) -->

    <dependency>

        <groupId>org.junit.jupiter</groupId>

        <artifactId>junit-jupiter-api</artifactId>

        <version>5.7.2</version> <!-- JUnit 5 -->

        <scope>test</scope>

    </dependency>


    <dependency>

        <groupId>org.junit.jupiter</groupId>

        <artifactId>junit-jupiter-engine</artifactId>

        <version>5.7.2</version>

        <scope>test</scope>

    </dependency>


    <!-- WebDriver Manager (for managing browser drivers automatically) -->

    <dependency>

        <groupId>io.github.bonigarcia</groupId>

        <artifactId>webdrivermanager</artifactId>

        <version>5.0.3</version>

    </dependency>

</dependencies>



selenium-java is the core dependency for Selenium WebDriver.


JUnit is used for test execution (JUnit 5 is recommended).


webdrivermanager is used to automatically download and manage the browser drivers (e.g., ChromeDriver, GeckoDriver) without manual configuration.


Step 4: Configure the pom.xml Build Settings


You can also define build configurations, such as using the Surefire Plugin for running tests. Add this plugin to your pom.xml to enable running JUnit tests in the Maven lifecycle.


<build>

    <plugins>

        <!-- Surefire Plugin for running tests -->

        <plugin>

            <groupId>org.apache.maven.plugins</groupId>

            <artifactId>maven-surefire-plugin</artifactId>

            <version>2.22.2</version>

        </plugin>

    </plugins>

</build>


Step 5: Create Selenium Test Class


Create a new Java class for your Selenium test under the src/test/java directory. For example, create a file called GoogleSearchTest.java.


Write the Selenium WebDriver Test using JUnit 5 for browser automation.


package com.example;


import org.junit.jupiter.api.BeforeEach;

import org.junit.jupiter.api.Test;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

import io.github.bonigarcia.wdm.WebDriverManager;


import static org.junit.jupiter.api.Assertions.assertTrue;


public class GoogleSearchTest {


    private WebDriver driver;


    @BeforeEach

    public void setUp() {

        // Automatically download the correct WebDriver

        WebDriverManager.chromedriver().setup();

        driver = new ChromeDriver();

    }


    @Test

    public void testGoogleSearch() {

        driver.get("https://www.google.com");


        // Find the search bar and enter text

        WebElement searchBox = driver.findElement(By.name("q"));

        searchBox.sendKeys("Selenium WebDriver");

        searchBox.submit();


        // Verify that the page title contains the search query

        assertTrue(driver.getTitle().contains("Selenium WebDriver"));


        // Pause for a few seconds to visually confirm the test

        try {

            Thread.sleep(2000); // Just for visualization; remove in real tests

        } catch (InterruptedException e) {

            e.printStackTrace();

        }

    }


    @AfterEach

    public void tearDown() {

        if (driver != null) {

            driver.quit();

        }

    }

}


Explanation:


@BeforeEach: This annotation ensures that the setUp() method runs before each test. It initializes the WebDriver and ensures the browser is ready for the test.


@Test: The actual test method. Here, we're opening Google's homepage, performing a search, and verifying the page title.


@AfterEach: Cleans up by quitting the browser after each test.


Step 6: Run the Tests with Maven


To run the tests, you can use the following Maven command from the terminal:


mvn test



This command will execute the tests defined in your project using JUnit and generate a test report.


If you want to run the tests directly from your IDE, you can right-click on the test class and choose Run or Run with Maven depending on your IDE.


Step 7: Viewing Test Results


After running the tests, Maven will generate the test reports in the target folder, typically located at target/surefire-reports. You’ll find a file named TEST-*.xml containing the detailed test results.


Step 8: Optional - Advanced Selenium Features


Once you have the basic setup, you can explore additional features of Selenium and Maven, such as:


Parallel test execution: Running tests across multiple browsers in parallel using Maven plugins like Surefire or Failsafe.


Cross-browser testing: Use Selenium Grid to run tests on different browsers and operating systems.


Headless testing: Running tests in a headless browser (e.g., Chrome headless) for faster execution in CI/CD pipelines.


Test Reporting: Integrating tools like Allure or ExtentReports for enhanced test reporting.


Conclusion


By following these steps, you have successfully set up a Maven project for Selenium WebDriver testing. The use of Maven ensures smooth dependency management, and JUnit 5 provides a framework for executing and asserting tests. By managing your WebDriver dependencies with WebDriverManager, you avoid the hassle of manually downloading and updating drivers.


Feel free to extend the project by adding more tests, working with different browsers, and integrating additional testing libraries and frameworks.

Learn Selenium with JAVA Training in Hyderabad

Read More

Adding Custom Reports in Your Framework

How to Use POM with PageFactory

Introduction to Data Driven Framework

Introduction to Keyword Driven Framework

Visit Our Quality Thought Institute in Hyderabad

Get Directions

Subscribe by Email

Follow Updates Articles from This Blog via Email

No Comments

About

Search This Blog

Powered by Blogger.

Blog Archive