Open In App

Maven Dependency Management with Selenium

Last Updated : 05 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In software testing, Selenium is a popular tool for automating web applications. Managing Selenium dependencies efficiently is crucial for ensuring smooth and consistent builds. Maven is a powerful build automation tool that effectively simplifies this process by handling project dependencies. This article explores Maven's dependency management features and specifically focuses on integrating Selenium for browser automation testing.

What is Maven?

Maven is an open-source build automation tool primarily used for Java projects. It provides a uniform build system, project dependency management, and documentation. Maven uses the Project Object Model (POM) file to manage project configurations and dependencies, simplifying the build process and ensuring consistency across different environments.

What Do You Understand by Maven Dependency?

Maven dependencies are external libraries or modules that a project needs to build and run. These dependencies are specified in the pom.xml Maven project. Maven automatically downloads and includes these libraries during the build process, ensuring that all necessary libraries are available without manual intervention.

What is Maven Dependency with Selenium?

In the context of Selenium, Maven dependencies refer to the Selenium WebDriver libraries required for your project. By specifying Selenium dependencies in the pom.xml file, Maven ensures that the correct versions of these libraries are downloaded and included in the project. Managing Selenium dependencies through Maven simplifies the inclusion of the necessary Selenium JARs in your project.

Key Advantages of Maven

  • Consistent Builds: Ensures the same build process across different environments.
  • Dependency Management: Automatically downloads and updates project dependencies.
  • Centralized Repository: Acts as a central repository for library management.
  • Integration with CI/CD: Easily integrates with continuous integration and deployment tools.
  • Build Automation: Automates compilation, packaging, testing, and deployment processes.
  • Extensibility: Supports plugins that extend its capabilities.

Add the Selenium Dependency

We can edit the pom.xml file and add the following dependencies to the project.

<!-- Selenium dependency -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.22.0</version>
</dependency>

How to Configure the TestNG XML in pom.xml

We can add the TestNG dependency to the pom.xml

<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.4.0</version>
<scope>test</scope>
</dependency>

Configure the TestNG suite in the build section of pom.xml

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>

Saving or Updating the Maven Project: Check if Dependencies are Added

  • Update the Project: In your IDE like IntelliJ, Eclipse, right-click on the project and select > Maven > Update Project.
  • Verify the Dependencies: We can ensure that the Maven Dependencies library in the project includes the Selenium libraries.

Using Maven for Dependency Management: Use Cases

  • Version Management: It can easily upgrade or downgrade the library versions.
  • Transitive Dependencies: It can automatically manage the dependencies of the dependencies.
  • Exclusion: It can be exclude the unnecessary transitive dependencies.

Implementation of Maven Dependency Management with Selenium

Step 1: Create the Maven Project

Open the Command Prompt, navigate to your desired project directory, and run:

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

Step 2: Main Class

Dont add any logic into the main class. Its only representation of maven project.

Java
package com.gfg;

public class Main {
    public static void main(String[] args) {
        System.out.println("Hello world!");
    }
}

Step 3: Add the Selenium and TestNG Dependencies

pom.xml

XML
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.auxentios</groupId>
    <artifactId>maven-selenium-demo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>22</maven.compiler.source>
        <maven.compiler.target>22</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>4.22.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.testng/testng -->
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>7.10.2</version>
            <scope>test</scope>
        </dependency>



    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.2</version>
                <configuration>
                    <suiteXmlFiles>
                        <suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
                    </suiteXmlFiles>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

Once we can add the plugins then you will find the selenium dependencies into the library files of the project.

selll-compressed

How to Run the Maven Project

We can execute the following command to run the tests.

mvn clean test
sel2-compressed

Conclusion

Maven Can Simplifies the dependency management and builds the automation for the Selenium projects. By the leveraging Maven's capabilities. We can ensure consistent builds, easily manage the dependencies, and streamline the testflow workflow.


Next Article

Similar Reads