Home Guide Maven Dependency Management with Selenium

Maven Dependency Management with Selenium

By Himanshu Varshney, Community Contributor and Pradeep Krishnakumar, Manager -

What is Maven?

Maven is a POM (project object model) based build automation and project management tool written in Java. However, it is compatible with projects written in C#, Python, Ruby. Developers or Automation testers face a common problem while using versions of JAR’s/dependencies as all of their code is placed in Eclipse, IntelliJ.

As per research by the International Journal of Computer Techniques, 65% of programmers use Eclipse as their IDE and out of them, more than 65% uses Maven as their build tool. Hence, it is the most popular build management tool in the market.

What do you understand by Maven Dependency?

For a smaller project with few functionalities or modules, a user can manually download JAR’s or dependencies and can add in the project. However, if a user is working on a medium or large project with many modules and a big team, then Maven plays a vital role in project management. It is a challenging task to ensure that all the team members are using the same dependency version. This issue can be solved by standardizing the dependency version for all.

Maven is widely used for dependency management in Java. It also provides a predefined folder structure to write the code. We can add different plugins and JARs in our project.

Key Advantages of Maven

  • One can easily configure dependencies required for building, testing and running code using pom.xml
  • Maven downloads the required file automatically from the repository
  • Using Maven, one can manage the entire life-cycle of a test project

Facts: Many users get confused by facing multiple dependencies in the Maven Dependency folder without adding them through pom.xml, or manually. This is because some dependencies depend on other dependencies, so if one wants to add them then they add dependent dependencies automatically, this feature is known as a transitive dependency. One can refer to the guide offered by Maven Apache.

For example, let a Selenium Tester add Selenium 3.14.0 version using Maven pom.xml (as shown below). After adding it, some other JARs also get added like selenium-API, selenium-chrome-driver, selenium-edge-driver.

Maven Selenium Tutorial

Just Press Ctrl+ Click on Selenium Dependency. It opens the Selenium pom XML where all the dependent dependencies are mentioned.

After adding this dependency, the Maven Dependency folder is updated with below JARs:

Concept: What is Transitive Dependency?

Whenever a programmer adds dependency which in-turn depends on another dependency, Maven automatically downloads the dependent JARs and adds them in the Maven Dependency folder. This feature is known as transitive dependencies.

Getting Started: How to Install Maven

Follow the below steps to download and set up Maven in the system:

  1. Make sure that the system has Java installed (atleast JDK 1.7) and JDK & JAVA_HOME environment variables configured
  2. Go to the Maven Apache Download page and download the binary zip file. Unzip it to the C drive, and the installation is complete
  3. Configure the MAVEN_HOME path as Environment Variable and give path till the bin folder
  4. Open the Control panel and go to the System link
  5. Click on the Advanced system settings tab at the left slide and navigate to the Advanced tab
  6. Click on the environment variables and add the MAVEN_HOME variable in system variables and give the path of the Maven bin folder and save it.
    • To set the Environmental Variable – Go to Start menu > Edit Environment Variables > System Properties > Environment Variables > In the System Variables set the MAVEN_HOME path

To verify whether Maven is installed or not, open the command prompt and run the command ‘mvn –version’ and then hit enter. It should return the Maven version, in this case, Maven 3.6.1.

Creating a Maven project and adding the Selenium Dependency

User can create a Maven project and add the desired dependency using the below steps:

  • Open Eclipse or any other IDE and create a Maven project
  • Once a Project is created, a predefined folder structure, pom.xml is also created
  • Open the pom.xml and navigate to the pom.xml tab at the bottom
  • Add different plugins inside the pom.xml as per the below structure
<build>
  <plugins>
    <plugin>
       desired plugins are placed here
    </plugin>
  </plugins>
</build>
  • While adding the JARs used in our project, search the desired JAR files or dependency in the Maven Repository website
  • Take an example and try to add Selenium dependency in the project. Search Selenium 3.0 Maven dependency and navigate to Maven
  • Click on the dependency, and it is copied to clipboard
  • Go to the pom.xml and paste the Selenium dependency inside the below XML structure
<dependencies>
<dependency>
    • Paste the Selenium dependency inside the dependencies tags
      <\dependency>
      <\dependencies>
  • Similar to this, add all the dependencies and paste inside the dependencies tags

Run Selenium Test for Free

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

After adding all the dependencies, follow the below steps to download them to the local computer:

  • Save the pom.xml and update the Maven Project by right-clicking on the project and updating the Project inside Maven
  • After saving the project, all the dependencies start downloading from the Maven Repository to the local compute
  • All the dependencies are downloaded to the C drive under the m2 folder
  • Before downloading the dependencies, Maven first verifies whether these dependencies are already present in the m2 folder or not. If yes, then Maven directly takes it from there, else Maven downloads it

Using Maven for Dependency Management: Use Cases

There are various applications of Maven as dependency management. Listed below are a few applications:

  • If the user wants to change the version of any dependency, that can be done by just updating the version of the dependency in pom.xml and saving the project
  • Updating to the new JAR version can be done by simply sharing the pom.xml file to all the team members and the entire module of the project can be quickly built on the new version
  • In the case of Eclipse IDE for Selenium development, the user can use an M2Eclipse Maven plug-in. One can create, modify, and execute Maven projects from Eclipse IDE

One can download M2E Eclipse plug-in from Eclipse Marketplace.

How to configure the TestNG XML in pom.xml

Selenium testing using TestNG can be powerful. We can prioritize our test methods by using TestNG. Users can easily integrate the TestNG XML with Maven.

Users can place TestNG.xml in the Maven folder and use the below code snippet inside the configuration tag, that can be placed in the pom.xml to integrate the testing with Maven.

 <Configuration>
    <suiteXmlFiles>
          <suiteXmlFile>TestNG.xml</suiteXmlFile>
   </suiteXmlFiles>
</Configuration>

How to Run the Maven Project?

There are multiple features available in Maven, to run the Selenium Java code. They are as follows:

Maven clean: Clean the Target folder to start the fresh run so that previous output files can be deleted before running a fresh run.

Maven build: Build command builds the code and verifies whether all the test cases are running fine. If the code is flawless, then Build is successful else the build fails. For a build to be successful, code should be flawless.

In case of a Build Pass or a Build Fail, this kind of report is displayed in the command prompt

Maven Install: Maven install command is the combination of both Maven clean, and Maven build command. First, it cleans the target folder files from the previous run and then builds a fresh one.

These commands, along with running methods, can also be executed without using an IDE like Eclipse, IntelliJ. Here are the steps to run the maven project from Command prompt:

  • Go the path where the pom.xml is placed
  • Type the mvn clean for Maven clean, mvn build for Maven build and mvn install for Maven install
  • User can get the console output on the command prompt screen only

BrowserStack Logo Run Selenium Tests on 2000+ Browsers & Devices Get Started Free