A Selenium project for test automation requires you to have all the dependencies associated with it. These dependencies can be downloaded and upgraded manually. However, as the project gets bigger, it can be quite challenging to manage these dependencies. This is why you need to have build automation tools to handle them automatically. In this article, you will learn all about one of the most popular automation tools out there - Maven, and also run your first Maven in Selenium project.
Why Use Maven to Manage Selenium Dependency?
Maven can easily handle the entire lifecycle of a project which can include code generation, compilation, testing, validation, packaging, and much more. It works in phases rather than tasks and makes the build management process much easier. Here are some key reasons to use Maven in Selenium:
Some of the key reasons Maven is used are:
- Provides a uniform system and simplifies the build process
- Increases reusability
- Includes a centralized repository to manage jar files
- Handles dependency management compilation, distribution, and other tasks
- Reduces extra steps like creating jar/war/ear files, building reports, or executing Junit test cases
So what exactly is Maven and how does it work? Let’s deep dive into it.
What Is Maven?
Maven is a built automation tool from Apache Software Foundation and is commonly used to handle Java projects. Initially, it was developed to simplify the build process of the Jakarta Turbine Project. Nowadays, it is widely used to manage project dependencies and the whole lifecycle of any project. Maven uses the concepts of the project object model (POM) and enables the user to cut down several steps followed in the build process.
How to Install Maven in Windows?
There are two ways to install Maven in Windows - through Command-Line or with Eclipse IDE.
How to Install Maven in Eclipse IDE?
It’s very easy to install Maven in Eclipse IDE. Here are the steps involved:
Step 1: Go to the top menu in Eclipse. Click on ‘Help’ and select ‘Install New Software’.
Step 2: A new window will open. Click on the Add button.
Step 3: Type ‘Maven’ in the name text box. Type “http://download.eclipse.org/technology/m2e/releases/” in the location text box. This contains the link to the location from where Maven can be downloaded.
Step 4: Click on the check-box and then click on the Next button.
Step 5: Keep the settings as default and then click on the Next button.
Step 6: Accept the Terms and Conditions and click on Finish.
Step 7: You would need to restart Eclipse once the installation is finished.
That’s it. You have now successfully installed Maven from Eclipse IDE.
How to Install Maven Through Command-Line?
Installing Maven through the command line is a bit more complex than through Eclipse. Here are the steps you need to follow:
Step 1: Download and Install Java
If you have not yet installed Java in your system, you would need to do so. If you already have it installed, make sure that you have the latest version. To check your Java version, type ‘cmd’ to launch the command prompt and type in ‘Java -version’.
Step 2: Set Up Java Environment Variable
Step 2a: Navigate to ‘This PC’ on your computer, right-click on it, and select ‘Properties’.
Step 2b: Click on Advanced System Settings and go to ‘Environment Variables’.
Step 2c: Under System variables, click on the New button.
Step 2d: In the variable name box, type ‘JAVA_HOME’ and in the variable value, type ‘C:\Program Files\Java\jdk1.8.0_20’, which is the JDK path.
Step 3: Download Maven and Set Up Maven Environment Variable
Download Maven and extract it to some location on your computer. Now, set up the Maven Environment Variable using the same steps we used to set up the Java Environment Variable.
Step 4: Update the Path Variable
Now we need to update the Path Variable with Maven’s installation ‘bin’ directory to run Maven from the command line. Follow the following steps:
Step 4a: Go to ‘Advanced System Settings’ and click on ‘Environment Variables’.
Step 4b: Click on the ‘Edit’ button.
Step 4c: In the Variable name box, type ‘PATH’, and in the variable value box, type ‘C:\apache-Maven-3.6.1\bin’.
Step 5: Testing the Maven Installation
Now that Maven is successfully installed in your system, let us verify it from the Windows command line. Open your command prompt and type in ‘mvn -version’ and then hit Enter. Check to see the version of Maven installed in your system. Now you’re all set to create projects using Maven.
Create Your First Maven Project
Just like the Maven installation, you can create a Maven project in two ways - through the Eclipse IDE or command line.
Creating Maven Project With Eclipse IDE
Step 1: Go to Eclipse IDE and create a new project.
Step 2: Select Maven Project.
Step 3: You can choose to create a simple project or just skip this option. For now, we’ll use a simple project.
Step 4: Fill in all the appropriate information for your project. If your project does not have any parent dependencies, you can skip the project dependencies. Click on Finish. Your Maven project is now created.
Step 5: You can open the pom.xml file to see the structure set up by Maven.
Creating Maven Project With Command Line
Open the Command Prompt and go to the folder where you want to set up your project. Now, type the following command and click Enter:
mvn archetype:generate -DgroupId=demoProject -DartifactId=DemoMavenProject -DarchetypeArtifactId=Maven-archetype-quickstart -DinteractiveMode=false
Your Maven project is now created.
Selenium Maven Dependency For Your Automation Project
Maven allows you to automatically download all the required libraries from its central repository. This makes your job a lot easier as you don’t have to store them locally.
TestNG Selenium Maven Dependency
Use the following code to import the testing framework dependency for Java.
Apache POI Selenium Maven Dependency
Use the following code to download the libraries required to access Microsoft format files.
Maven Lifecycle in Selenium
There is a lifecycle that every Maven in Selenium build follows. Maven has a number of “goals” that represent a specific task for building and managing a project. There are a number of goals present in Maven, but three are crucial for Selenium test automation. They clean, install, and test.
Maven Clean is used to clean the target folder. This is where the previous build’s files, libraries, reports, output files, etc., are saved. The command to execute this is ‘mvn -clean’.
Maven Install is used to install all the dependencies and create the deployment file. After this, it will run the test.
Maven Run will simply run the test without creating any deployment file.
Want to Learn More?
This article pretty much sums up all the basics that you need to get started with Maven. If you want to learn more about Maven in Selenium and other DevOps fundamentals, you can sign up for Simplilearn’s Post Graduate Program in DevOps developed in collaboration with Caltech CTME. You will also learn a number of configuration management tools such as Ansible, SaltStack, and Puppet. Get started with this course today and get one step closer to achieving your dream career.