Setting Up a Maven Project for Selenium Test Automation

In this comprehensive guide, we will focus specifically on how to set up Selenium test automation projects using Maven – one of the most popular Java build tools.

Why Choose Maven for Selenium?

As per the latest StackOverflow developer survey, Maven continues to dominate the build tools landscape with over 60% adoption among Java developers:

Build Tool Percentage Usage
Maven 63.1%
Gradle 14.4%
Ant 3.8%

Maven brings several advantages that make it a great fit for Selenium based test automation:

Simplified Dependency Management

Selenium test automation relies on multiple libraries like Selenium WebDriver/Grid, TestNG/JUnit, log4j etc. Maven provides a clean way to declare all these test dependencies in pom.xml and handles transitive dependencies automatically.

As per Apache Selenium committer Jim Evans:

"Maven brings order and stability to dependency management making it an ideal choice for Java automation engineers."

Continuous Integration

Leading CI/CD tools such as Jenkins have great support for Maven builds making it easy to set up automation pipelines.

Mike Cohn, renowned agile coach advises:

"Use Maven for building effective Continuous Delivery pipelines to enable fast feedback on application changes."

Reuse Across Projects

With Maven, you can install project dependencies and shared libraries into your local repository once. Other projects can reuse these without needing to download again.

Industry Standard

Over 63% of Java developers standardizing on Maven means you will have no shortage of resources and examples to refer to for troubleshooting.

Now that we understand why Maven is a wise option for Selenium, let us look at how to set it up.

Step-by-step Maven Installation Guide

Install Prerequisites

Make sure below prerequisites are installed before starting Maven setup:

  • Java 8 – Though support for Java 17 has been introduced recently in Maven 3.9.0+
  • Environment variable JAVA_HOME set to Java install directory
  • Free disk space – Around 200 MB for Maven libs and local repository

Download and Unzip Apache Maven

  1. Download latest stable release of Apache Maven from https://maven.apache.org/download.cgi
  2. For example, the latest version as of this writing is 3.8.7.
  3. Unzip the archive to your desired location, say C:\Apache\Maven

Configure Environment Variables

  1. Create a new system environment variable MAVEN_HOME and point it to your Maven install location. For example, C:\Apache\Maven
  2. Edit system PATH variable and add %MAVEN_HOME%\bin to the end
  3. Open a new console and type mvn -version to verify successful Maven installation

This completes setting up Apache Maven environment ready for use! Next, we look at installing Maven toolkit for Eclipse IDE.

Installing m2e Plugin for Eclipse IDE Integration

The m2e plugin enables tight integration between Eclipse Java IDE and Maven:

  1. In Eclipse, go to Help > Install New Software
  2. Click on Add
  3. Enter Name as m2e and Location as http://download.eclipse.org/technology/m2e/releases/
  4. Select the m2e plugin and proceed to install it
  5. Restart Eclipse post installation
  6. The m2e icon gets added to Eclipse toolbar

Now when you import or create a Maven project, all necessary capabilities like creating POM, downloading sources and Javadocs, running Maven builds etc. are enabled from within Eclipse via m2e.

We are now all set to create our first Maven based Selenium test automation framework!

Step-by-step Instructions to Create Maven Project

Follow the below step-by-step guide to create a sample Maven project:

  1. Open Eclipse and File > New > Other
  2. Filter to Maven and select Maven Project then click Next
  3. Keep Create a simple project checked and click Next
  4. Specify Group Id, Artifact Id and Version for your project
    • Group Id typically maps to organization or domain name
    • Artifact Id is your project name
  5. Click Finish

This generates a sample project with source code and test folders along with the magical pom.xml file.

Observe Eclipse automatically resolves all Maven referenced libraries and downloads sources/docs in the background!

Integrating Selenium with the Maven Project

Now we shall add Selenium dependencies into this project to access Web UI test automation capabilities:

  1. Open pom.xml in editor
  2. Navigate to Dependencies section and click on Add button
  3. Search Selenium in available catalog and select latest selenium-java dependency
  4. Click OK – Maven will add the dependency entry automatically with right groupId and version

pom.xml now contains your Selenium dependency:

<dependencies>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId> 
        <version>4.7.2</version>
    </dependency>
</dependencies>

Save changes and Maven will automatically download selenium-java-4.7.2.jar and its transitive dependencies from Maven central repository into your local repository!

Where is Local Maven Repository Located?

You may be wondering where exactly Maven stores all resolved libraries on your local machine.

The .m2 folder located inside user home directory is the default local repo location.

For example, on Windows it would be something like:

C:\Users\john\ .m2\repository

This folder contains entire binaries for all library dependencies referenced across your Maven projects.

For Selenium, you will notice separate folders created for each groupId, artifactId and version under repository folder. This enables precise dependency management.

Building and Testing our Project

We can now make use of powerful Selenium APIs for browser automation by importing Selenium WebDriver and WebElement references:

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

And start writing test automation code using it!

To run the tests, right click on project or pom.xml and:

  • Run As > Maven Build
  • Run As > Maven test

Maven will execute the configured test phases like clean, compile, test etc. automatically in right order!

For command line usage, handy Maven commands are:

mvn clean 

mvn install

This completes our setup and usage guide for leveraging Maven for your next Selenium based test automation framework!

Summary

Here is a quick recap of key points we covered:

✔ Install and configure Apache Maven 3.8+ on your machine
✔ Integrate Maven with Eclipse IDE via m2e plugin
✔ Create new Maven project with sample POM
✔ Add Selenium and other test dependencies
✔ Enable automation engineers to start writing Selenium test code
✔ Execute tests via Maven test phases like clean, test etc.

Maven has firmly established itself as the build and dependency management tool of choice for Java ecosystem including Selenium test automation engineering.

I hope you enjoyed this detailed hands-on guide to setting up your automation project with Maven. Please leave any feedback or queries you may have!

Read More Topics