HomeGuideMaven Dependency Management with Selenium
Maven Dependency Management with Selenium
By Himanshu Varshney, Community Contributor and Pradeep Krishnakumar, Manager - August 13, 2019
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. So, it is the most popular build management tool in the market.
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 many 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. After adding it, some other JARs also get added like selenium-API, selenium-chrome-driver, selenium-edge-driver.
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: 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.
How to Install Maven
Follow the below steps to download and set up Maven in the system
Make sure that the system has Java installed (at least JDK 1.7) and JDK & JAVA_HOME environment variables configured
Configure the MAVEN_HOME path as Environment Variable and give path till the bin folder
Open the Control panel and go to the System link
Click on the Advanced system settings tab at the left slide and navigate to the Advanced tab
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 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
desired plugins are placed here
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
Paste the Selenium dependency inside the dependencies tags
Similar to this, add all the dependencies and paste inside the dependencies tags
Saving or Updating the Maven Project and checking the dependencies 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
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.
User 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.
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