Maven in Selenium: Selenium Maven Dependency Tutorial

By Vijay

By Vijay

I'm Vijay, and I've been working on this blog for the past 20+ years! I’ve been in the IT industry for more than 20 years now. I completed my graduation in B.E. Computer Science from a reputed Pune university and then started my career in…

Learn about our editorial policies.
Updated May 9, 2025

In our last Selenium tutorial, we learned a build tool named “Apache Ant”. We also broadly discussed its applicability and importance besides the practical approach.

In this Selenium Testing tutorial, we will learn Maven – a build automation tool that is distributed under Apache Software Foundation. It is mainly used for Java projects. It makes the build consistent with another project.

Maven is also used to manage the dependencies. For example, if you are using selenium version 2.35 and at any later point in time you have to use some other version, Maven can manage easily the same. You will find more examples of this later in this chapter. It works effectively when there is a huge number of Jar files with different versions.

Maven – Build Automation Tool

Use of Maven Build Automation Tool and Maven Project Setup for Selenium

What is a Build Tool

The build tool is used to set up everything that is required to run your Java code independently. This can apply to your entire Java project. It generates source code, compiling code, packaging code to a jar, etc. Maven provides a common platform to perform these activities, which makes a programmer’s life easier while handling a huge project.

Maven provides pom.xml which is the core of any project. This is the configuration file where all required information is kept. Many of the IDEs (Integrated Development Environments) are available, which makes it easy to use. IDEs are available for tools like Eclipse, NetBeans, IntelliJ, etc.

Maven stores all project JARs. Library jar is in a place called a repository which could be central, local or remote. Maven downloads the dependency jar from a central repository. Most of the commonly used libraries are available at http://repo1.maven.org/maven2/.

Downloaded libraries are stored in the local repository called m2. Maven uses the libraries available in an m2 folder and, if any new dependency is added, then Maven downloads from the central repository to the local repository. If libraries are not available in the central repository, then Maven looks for the remote repository.

The user has to configure the remote repository in pom.xml to download from the remote repository.

Below is an example of configuring a remote repository to pom.xml file. Provide the id and URL of the repository where libraries are stored.

<repositories>
     <repository>
         <id>libraryId</id>
         <url>http://comanyrepositryId</url>
     </repository>
</repositories>

General Phrases used in Maven:

  • groupId: Generally groupId refers to domain id. For best practices, the company name is used as groupId. It identifies the project uniquely.
  • artifactId: It is the name of the Jar without version.
  • version: This tag is used to create a version of the project.
  • Local repository: Maven downloads all the required dependencies and stores them in the local repository called m2. We will share more details regarding the same in the next topic.

Build Life Cycle

Basic maven phases are used as below:

  • clean: deletes all artifacts and targets that are created already.
  • compile: used to compile the source code of the project.
  • test: test the compiled code and these tests do not require to be packaged or deployed.
  • package: package is used to convert your project into a jar or war, etc.
  • install: install the package into the local repository for another project.

Maven Setup

Follow these steps:

Step #1: To setup Maven, download Maven’s latest version form Apache depending upon the different OS.

Step #2: Unzip the folder and save it on the local disk.

Step #3: Create an environment variable for MAVEN_HOME.

Observe and adhere to the following instructions:

Navigate to System Properties ->Advanced System Settings->Environment Variable->System Variable->New->Add path of Maven folder

Environment Variable
System Variable

Step #4: Edit the path variable and provide the bin folder path.

Edit the path variable

Step #5: Now verify the maven installation using the command prompt and don’t forget to setup JAVA_HOME

Use mvn –version to verify the Maven version and the output comes like below.

Maven Tutorial 4

Install Maven IDE in Eclipse

Maven provides IDE to integrate with Eclipse. I am using Eclipse Juno here.

Navigate to Help->Eclipse Marketplace-> Search maven ->Maven Integration for Eclipse ->INSTALL

Eclipse Marketplace

After installation, restart Eclipse.

Then right-click on pom.xml and verify all the options are available like below. 

Maven Tutorial 6

Create Maven project:

Step #1: Navigate to File- new-others-Maven-Maven Project-Click Next

Maven Project

Step #2: Check the Create a simple project and click Next.

Create a simple project

Step #3: Provide Group Id and Artifact Id. You can change the version of Jar as per your wish. Here I am using the default name. Click Finish.

Provide Group Id and Artifact Id

Step #4: After finishing, you will find the project structure is created like below. pom.xml is created which is used to download all dependencies.

Maven Tutorial 10
pom.xml file looks like below:

<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.softwaretestinghelp.test</groupId>
<artifactId>com.softwaretestinghelp.selenium</artifactId>
<version>0.0.1-SNAPSHOT</version>
</project>

Step #5: Add dependencies for Selenium.

All selenium Maven artifacts are available in below central repository

http://repo1.maven.org/maven2/org/seleniumhq/selenium/

Add the following dependencies in pom.xml for selenium:

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

Similarly, the following is the dependency for Junit :

<dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.4</version>
 </dependency>

If you want to add other third-party jars then add those dependencies in pom.xml

Step #6: Final pom.xml will be like below:

<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.softwaretestinghelp.test</groupId> <artifactId>com.softwaretestinghelp.selenium</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
       <groupId>org.seleniumhq.selenium</groupId>
       <artifactId>selenium-java</artifactId>
       <version>2.41.0</version>
   </dependency>
   </dependencies>
</project>

Step #7: Maven will download all the dependency jars into the local repository called .m2.

The M2 folder is basically inside Users->username->m2

All the jars will be placed in a folder called repository which is inside .m2 folder. Maven will create separate folders for the different versions and different group id.

M2 folder

Step #8: If an m2 folder does not populate in Maven dependencies, then you can populate those jars manually.

– Eclipse Windows ->Preference
– Navigate Java->Build Path->Classpath Variables

Classpath Variables

Click New Button ->Define M2_REPO and provide the path of the m2 folder.

Step #9: Upon successful setup, you will find the Maven Dependencies folder below which will have the required dependency jar for the project

Maven Dependencies

Build the Project

The project can be built by both using IDE and command prompt.

Using IDE you have to right-click on POM-Run As-Maven Build

IDE and command prompt

Enter goals like clean install etc. and click Run.
The same can be done using a command prompt. Navigate to the project folder where pom.xml lies.
And use the below commands to clean, compile, and install

For clean: mvn clean
For compile: mvn compile
For Install: mvn install

Below is the info which is displayed when you clean any project and shows “BUILD SUCCESS”.

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building com.softwaretestinghelp.0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
 [INFO] --- maven-clean-plugin:2.4.1:clean (default-clean) @ com.softwaretestinghelp ---[INFO] Deleting C:\Users\rshwus\WORKSPACE\com.softwaretestinghelp\target
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.702s
[INFO] Finished at: Sat May 24 18:58:22 IST 2014
[INFO] Final Memory: 2M/15M
[INFO] ------------------------------------------------------------------------

Conclusion

Maven simplifies the code handling and process of building the project. Most of the projects follow the maven structure.

Download all dependencies provided the dependencies are available in the Maven central repository. If any of the dependencies are not available in the Maven central repository then you have to add the repository path in pom.xml explicitly.

There are many other build tools available in like ant. But it is better to use maven while dealing with different versions and different dependencies. Maven even can manage the dependencies of dependencies. Other tools may not provide such flexibility like maven. Please post your queries anything related to maven here.

Next Tutorial #25: In the upcoming tutorial, we will discuss a continuous integration tool known as Hudson. We would study about its importance, role, and benefits in the Test Automation Framework. We would look at the Hudson straight from the beginning, from its installation to its working.

Was this helpful?

Thanks for your feedback!

Recommended Reading

  • Sikuli GUI Automation Testing Tool

    The most practical tutorials on Sikuli GUI automation testing tool: In part 1 of this "Introduction to Sikuli tutorial series", we have discussed Sikuli, how it works, and how to create a simple Sikuli project. In this 2nd part, you are going to learn some advanced concepts like - how…

  • selenium automation with phantomjs

    In This Article, Selenium Automation With PhantomJS is Explained With Code Examples: PhantomJS is a headless browser that is primarily used for GUI less automation. The performance and execution happening on this browser is faster and is generally used in the scenarios where manual monitoring is not required and on…

  • Introduction To Selenium WebDriver

    Introduction to Selenium WebDriver: Earlier in this series, we published tutorials that focused more on Selenium IDE and its various aspects. We introduced the tool and discussed its features. We also constructed a few scripts using Selenium IDE and Firebug. From there, we moved on to different types of web…

  • Selenium on Chrome

    In-Depth Tutorial On ChromeDriver for Running Selenium Webdriver Tests on Chrome Browser: Handling browser alerts while automating through Selenium will be discussed in this article. Moreover, we will elaborate on the set up of the Selenium script for the Google Chrome browser along with appropriate examples and pseudo-codes. Upon going…


READ MORE FROM THIS SERIES:



34 thoughts on “Maven in Selenium: Selenium Maven Dependency Tutorial”

  1. Sir/Mam,
    When i run maven test
    Pom.xml–When i right click on this and choose run as–>Maven test

    I got the successful build and my testcase(using TestNG) is not run in the browser. No browser is opened when i am using maven

    Reply
  2. Though you have given beautiful explanation. But still a question confuses me. Why we need a Build tool with selenium when we already are using Eclipse.
    Perhaps only for :
    1. Test Scheduling
    2. Creating Batch file that gives single point for execution )in ANT)

    Is there anything else that I am missing

    Reply
  3. Hi um using eclipse helios … whenever i tried to install maven from marketplace, it shows an error “Cannot complete the install because one or more required items could not be found.” I also tried lower version (1.5 , 1.4, 1.3) not working. Kindly help. Thanks in advance

    Reply
  4. Hi after running maven i get build successful but not getting the results of the test case executed why?
    And how i will get the results?

    Reply
  5. If I want to download jar/dependency from remote custom repository which is hosted,Is there any need to create folder structure for groupId and artifactId and version? what should be groupId for adding the dependency from remote repository?

    Reply
  6. It is a wonderful information about Maven. Really it will help to lot of beginners. Thank you very much for your continuous efforts. Now a days lot of people are gaining by your post. Keep it up.

    Reply
  7. Vijay thanks for the wonderful post, This clearly explains.
    you have explained about both build tools – ANT – Maven. I am a fresher learning selenium.
    Which is best of these two , which one suits better for Testing frameworks. Please clarify.

    Reply
  8. Great.. well articulated about the most common build tools. Definitely it helps a lot for the beginners a lot.
    Thanks guys, keep the work flowing…..

    Reply
  9. This tutorial is really very nice. Its good to know some jargons which we keep on reading on various forums but never used. STH you did a great job with these selenium series.

    Please start advance level also for this

    Reply
  10. i want to thank software testinghelp for:
    1.Clearing my basic concepts of selenium, before reading your tutorials i almost was about to quit on my coaching as i was not able to understand a single word in the program.
    2.Actually it started with single word only — Webelements– now i am like writing my own programs since then.
    3.For this Maven thing also, i am really grateful, because one thing i am confident about, if i am notable to understand anything in my class at least i have an outside help.
    Frankly speaking previously after coming from classes i hardly studied because i was too terrified with what was taught, my trainer is really nice, but still for basic things and LINE TO LINE meaning i approach your site only for understanding.

    Thanks for taking me this far with my selenium training. and making my dream come true as a selenium automation tester.

    Reply
  11. Test Auto Framework is maven based. We are a central team supporting multiple teams who want to use our framework. However each team creates there own project for their purposes, so each has a POM of their own . The pom’s would increase as customer solutions increase. Is there a way you can manage the number of poms created?

    Reply
  12. It gives very clear explanation for each and every point in selenium

    Can u update db readre in webdriver instead of xls reader

    Reply
  13. Hi, It was nice explanation indeed.
    Could you please help me in below problem ? Thanks in advance.

    I’m using maven 3.5 version. I have kept paths correctly, but after creating simple Maven project, it is showing error in “pom.xml” file.
    I have added dependency as well.

    Below is my pom.xml code.

    4.0.0
    com.testOne.one
    com.testTwo.two
    0.0.1-SNAPSHOT

    org.apache.maven.plugins
    maven-dependency-plugin
    3.0.0

    Reply
  14. Superb post and really very useful information with step by step guide. This information build confidence in other who generally afraid of setting up automation project.
    Thanks again!

    Reply

Leave a Comment