Why Use Maven?
Some of the key reasons Maven is used are:
- It simplifies the build process and provides a uniform system
- It handles compilation, distribution, dependency management and other tasks efficiently.
- It increases reusability.
- It reduces steps, like adding jar files to the project library, building reports, executing JUnit test cases, creating jar/war/ear files for deployment.
- It has a repository which is centralized that manages jar files.
What Is Maven?
By now, you already have the idea that Maven is a build automation tool which is used to manage the project dependency and the whole project lifecycle. Maven is built by Apache Software Foundation and is used majorly for Java projects.
ome key terms you should know for this Selenium Maven tutorial:
Maven Local repository: It is a local repository in the developer’s machine that holds all the project artifacts. On executing a Maven build, all the project dependent jars are downloaded into the local repository.
Maven Central Repository: It is a default repository in every new project which is official to Maven. Whenever any dependency is not present in local repository Maven automatically consults the central repository to download it.
Project Object Model: POM(Page Object Model) XML contains project information and configuration details used by Maven build. It is located in the root directory of each project.
Dependency: Any library on which a java project is dependent on, to run or build is called a dependency. Examples: Log4j jars, Apache Poi jars, Selenium Jars, etc.
Surefire PlugIn: To execute the unit tests of an application during the test phase of the build lifecycle one can use the Surefire Plugin. A surefire report is available in two file formats, viz Plain text file(.txt) and XML file(.xml). This plugin is a must to use as it helps Maven identify tests even when using TestNG or Junit frameworks.
How to Install Maven In Windows?
Maven can be installed either through Command-Line or with Eclipse IDE. We’ll first go by the steps on how to install it through Eclipse IDE.
How To Install Maven in Eclipse IDE?
Installing Maven in Eclipse is pretty straightforward, here are the steps involved:
Step 1: Click on Help from the top menu in Eclipse and select ‘Install New Software’.
Step 2: On the newly opened window, click on the Add button.
Step 3: In the name text box type “Maven”, and in the location text box type “http://download.eclipse.org/technology/m2e/releases/”. This URL is the location from where Maven can be downloaded.
Step 4: A check-box will appear in the pop window, Check the check-box and click on the Next button.
Step 5: Wait for a few minutes for the window to complete its process.
Step 6: Keep the default settings and click on the Next button.
Step 7: Accept the Terms & Conditions and click on Finish.
Step 8: Wait for the installation to finish.
Step 9: Once the installation is finished, it will ask you to restart your eclipse. Click on Yes so as to see the changes being reflected.
Boom! This is it. You’ve now installed Maven successfully to your Eclipse IDE.
Just in case you want to go around the other way of installing through the command line, please follow the below steps.
How To Install Maven Through Command Line?
Here are the steps to install Maven through command line in this Selenium Maven tutorial:
Step 1: Download & Install Java
You need to install Java in your system, if not already done. The latest version of java can be downloaded from this link
To check the java version of your system, please go running and type ‘cmd’ to launch the command prompt. Type ‘Java -version’ and press Enter to see which java version is installed.
Step 2: Set Up Java Environment Variable
After installation of Java, set up the Java Environment Variable. Open the system settings to set the Java Environment Variable.
- Go to This PC and right-click on the empty space anywhere within and select Properties
- Click on Advanced System Settings so that a new pop up opens up.
- Now, from the pop-up window click on Environment Variables
- Click on the New button under System variables.
- Type “JAVA_HOME” in the Variable name box and ‘C:\Program Files\Java\jdk1.8.0_20’ JDK path in the Variable value box and save the same.
- You can now see the newly created Java variable under ‘System Variables’
Step 3: Download Maven and Set Up Maven Environment Variable
- Maven can be downloaded from below location:
https://Maven.apache.org/download.cgi - Extract it to some location in your machine as per your convenience. For me it is lying at ‘C:/apache-Maven-3.6.1’
- You can set up the Maven Environment Variable similar to how we set up the Java Environment Variable in steps above.
- Type ‘Maven_HOME‘ in the Variable name box and ‘C:\apache-Maven-3.6.1’ in the Variable value box.
- You’ll now be able to see the newly created Maven variable under ‘System Variables’.
Step 4: Updating the Path Variable
In order to run Maven from the command line, we have to necessarily update the Path Variable with Maven’s installation ‘bin’ directory.
- Open system properties through My Computer.
- Navigate to ‘Advanced System Settings’.
- Click on ‘Environment Variables’.
- Click on the Edit button under user variables.
- Type ‘PATH’ in the Variable name box & ‘C:\apache-Maven-3.6.1\bin’ in the Variable value box.
Step 5: Testing the Maven Installation
Maven is now successfully installed in your system. Now let’s verify it from the Windows command line. Open command prompt and type mvn -version and hit Enter. Check to see the version of Maven installed in your system being displayed in the results.
Now you’re all set with Maven Installation now and can go ahead with creating projects using Maven.
Create your First Maven Project
Just like the Maven installation we discussed earlier in the Selenium Maven tutorial, you can also create a Maven project either through Eclipse IDE or through Command Line.
Creating Maven Project with Eclipse IDE
Below are the steps to create a Maven Project with Eclipse IDE:
Step 1: Create a new project from the Eclipse IDE.
Step 2: From the new project window expand Maven and select Maven Project and then click on Next.
Step 3: You may create a simple project or just let go of this option. For now, we’ll use a simple project which would create a simple Maven-enabled Java project.
Step 4: Now upon clicking Next you’ll need to type information of the Maven project is created. You may refer below descriptions to fill in the values:
Group Id- corresponds to your organization’s name.
Artifact Id- refers to the project name.
The version can be chosen flexibly. If your project does not have any parent dependencies, you don’t need to fill in the project dependencies. Just fill in the appropriate information and click on ‘Finish’.
Step 5: Your Maven project has now been created!
Note: Java code is placed in /src/main/java, resources are kept in /src/main/resources, testing code is placed in /src/test/java and the testing resources are placed in /src/test/resources.
Step 6: You may now open the pom.xml to view the structure set up by Maven. You’ll see all the information that we entered in ‘step 4’ here. You can use the tabs at the bottom to change the view. The pom.xml tab has the pom XML code for the Maven project.
The Maven Project is now ready to be used.
Next, let us see how we can create a Maven project using Command-Line.
Step 1: Open a Command Prompt and navigate to the folder where you want to set up your project. Once navigated, type below command:
mvn archetype:generate -DgroupId=demoProject -DartifactId=DemoMavenProject -DarchetypeArtifactId=Maven-archetype-quickstart -DinteractiveMode=false
Here, DgroupId is the organization name, DartifactId is the project name and DarchetypeArtifactId is the type of Maven project.
On clicking Enter, your Maven project will be created.
Step 2: You can go to the project location to see the newly created Maven project. You can open the pom.xml file, which is in the project folder, by default the POM is generated like this:
Step 3: You can view the default folder structure of your Maven project.
Now that we know how to create a Maven project, let’s try to integrate Selenium with Maven. But before we do that, we need to understand the various Dependencies that would help with this integration.
This is a very simple POM but still displays the key elements every POM contains, so let’s walk through each of them to familiarize you with the POM essentials:
- project This is the top-level element in all Maven pom.xml files.
- modelVersion This element indicates what version of the object model this POM is using. The version of the model itself changes very infrequently but it is mandatory in order to ensure stability of use if and when the Maven developers deem it necessary to change the model.
- groupId This element indicates the unique identifier of the organization or group that created the project. The groupId is one of the key identifiers of a project and is typically based on the fully qualified domain name of your organization. For example org.apache.maven.plugins is the designated groupId for all Maven plugins.
- artifactId This element indicates the unique base name of the primary artifact being generated by this project. The primary artifact for a project is typically a JAR file. Secondary artifacts like source bundles also use the artifactId as part of their final name. A typical artifact produced by Maven would have the form <artifactId>-<version>.<extension> (for example, myapp-1.0.jar).
- version This element indicates the version of the artifact generated by the project. Maven goes a long way to help you with version management and you will often see the SNAPSHOT designator in a version, which indicates that a project is in a state of development. We will discuss the use of snapshots and how they work further on in this guide.
- name This element indicates the display name used for the project. This is often used in Maven’s generated documentation.
- url This element indicates where the project’s site can be found. This is often used in Maven’s generated documentation.
- properties This element contains value placeholders accessible anywhere within a POM.
- dependencies This element’s children list dependencies. The cornerstone of the POM.
build This element handles things like declaring your project’s directory structure and managing plugins.
Maven Lifecycle In Selenium
There is a maven lifecycle in Selenium that every Maven build follows. The different methods are simply goals. Before going ahead I’ll explain what these goals are.
Open a command line in your system and type mvn and then Enter.
You can see a Build failure message with error being displayed saying that no goal has been defined in this Selenium Maven tutorial. As you parse through this message you can see the different goals that can be defined for our Maven project. I’ll quickly go through these default goals before going into detail about the goals required for the Selenium project.
- validate: would check if our project is correct and all the required information available
- compile: would compile the project source code
- test: would unit test the compiled source code of our project
- package: would package the compiled code into the distributable formats, like JAR
- integration-test: would deploy the package into an environment where we would run the integration tests
- verify: would verify if the package is valid
- install: would locally install the package
- deploy: would be used in integration or release environment by copying the final project into a remote repository where it can be accessed by other projects or developers
- clean: cleans up previous build artifacts
- site: creates site documentation for the project
Of the above-said default goals, three are crucial for Selenium test automation. There are clean, install, and tests.
You can either use these goals alone or use it as a combination like clean-install for this Selenium Maven tutorial.
Clean- It would clean the target folder, i.e. the folder where the previous build’s libraries, build files(war, tar or jar files), reports, output files, etc are saved. On executing mvn -clean this target folder will be deleted.
Install- It would install all the dependencies, in case of missing jars, and create the deployment file(war/jar files in case of JAVA) and then it’ll run the test.
Test- It will simply run the test without creating any deployment file.
When using Eclipse IDE, you can directly use any of these three goals by right-clicking on your pom.xml, then Run As and selecting any of the options.
Maven Clean
I’ll start with selecting Maven clean in this Selenium Maven tutorial, you can see the output below:
So here the task of clean, which is to delete the target folder has been successfully completed and hence our Build is successful.
Maven Install
Before going to the second task of install, you need to add a plugin called Maven Compiler plugin.Without it the Selenium test automation build will fail. This plugin is used to identify the specific location of the compiler. You can just add the below plugin in your pom.xml and refresh the project before executing Maven install.
<build> | |
<plugins> | |
<plugin> | |
<groupId>org.apache.Maven.plugins</groupId> | |
<artifactId>Maven-compiler-plugin</artifactId> | |
<configuration> | |
<compilerVersion>1.5</compilerVersion> | |
<source>1.5</source> | |
<target>1.5</target> | |
</configuration> | |
</plugin> | |
</plugins> | |
</build> |
Upon adding this piece, your pom.xml would look like below:
Now, go to Maven Install just like you did for Maven Clean in this Selenium Maven tutorial and observe the console output for the build installation:
You can see in the console output that the Maven installer executed the tests as well. To see the installed directories in your local system you refresh your project and see the directories generated. In the below snapshot you can see all the files generated(a jar file as well since this is a simple JAVA program) as a result of Maven install. You can share this jar file directly for others to execute.
Maven Test
Similarly, we can do Maven Test in this Selenium Maven tutorial and see the build results in the console:
The above steps show how execution can be done through Eclipse IDE, but in real-time project execution is done mostly through the command line So now we would use these goals from the command line. Before proceeding onto these tasks make sure that your current working directory in cmd points to your local workspace location. If we do not do so our Maven command would not be able to find the desired pom.xml and hence our Selenium test automation build would fail.
Let us first do the Maven Clean using the command prompt in this Selenium Maven tutorial. We simply need to write ‘mvn clean’:
You can see the target folder getting deleted. The next step in this Selenium Maven tutorial is to do Maven install which is done by typing ‘mvn install’ in the command prompt.
You can see the console output is almost similar to what was there through Eclipse execution. Let us now perform a Maven test by typing ‘mvn test’ which would result in the test being executed without building jar files.
Now that you know the basic goals for executing our automation test you’re good to go to run your automation scripts through Maven!
How do I compile my application sources?
Change to the directory where pom.xml is created by archetype:generate and execute the following command to compile your application sources:
- mvn compile
he compiled classes were placed in ${basedir}/target/classes
Maven Surefire Plugin
By now you have read this term in this Selenium Maven tutorial quite some time in your console output logs, so I’ll shed some light on it. The surefire plugin helps Maven to identify the tests and is used with whichever framework your project is built on. To add Surefire plugin to your pom.xml use below code snippet:
<properties> | |
<suiteXmlFile>src/main/resources/testng.xml</suiteXmlFile> | |
</properties> | |
<build> | |
<plugin> | |
<groupId>org.apache.Maven.plugins</groupId> | |
<artifactId>Maven-surefire-plugin</artifactId> | |
<version>2.17</version> | |
<configuration> | |
<suiteXmlFiles> | |
<suiteXmlFile>${suiteXmlFile}</suiteXmlFile> | |
</suiteXmlFiles> | |
</configuration> | |
</plugin> | |
</build> |
Your pom.xml should look like below:
Here I’ve put in testng.xml in src/main/resources and thereby giving its path in properties.
Now I’ll run Maven test from eclipse in this Selenium Maven tutorial and see the results:
You can now check the reports that have been generated by default and in a similar way by typing ‘mvn test’ can execute this suite from the command prompt.