Skip to content

Get Started With Maven In Test Automation

Maven developed by Apache Software Foundation is a popular build automation and management tool. Its goal is to make the build process consistent and simple. Maven in test automation framework used to manage the framework’s build compilation, execution, and documentation.

Why Maven Project Is Required?

Maven has a pom.xml file that contains the project configuration information. We can simply add the required dependencies in pom.xml and those JARs will be downloaded from the central maven repository and saved locally under the .m2 repository in the local machine.

By Using Maven, versioning of software can be easily maintained, which can not be obtained easily in a java project. Suppose the user downloads 100 Jars and after a few days 50 of them have some update, then in the Java project, it will be very tedious to download all those updated Jars again however in Maven Project we can easily change the version number in Pom.xml and all updated Jars will be automatically updated.

What is Maven’s Life Cycle?

Maven has 3 different lifecycles and each lifecycle has different phases.

1)Build LifeCycle: Build lifecycle’s commonly used phases are Validate, Compile, Test, Install, and Packaging.

2)Clean LifeCycle: Clean LifeCycle include PreClean, Clean and PostClean phase.

3)Site LifeCycle: Site LifeCycle consists of Pre-Site, Site, and Post-Site, Site-Deploy Phases.

Each phase consists of different plugins and plugins associated with different goals.

Understanding The Maven Commands Structure

Maven commands consist of the term “mvn” + “phase name” Or “mvn” + “plugin: goal”.

So if we write mvn compile, it shows that compile is a phase of Build LifeCycle. The same goal can be achieved by using the Plugin and goal combination. Where Compiler is the plugin and compile is the goal.

mvn compiler:compile

let’s take another example, When we write mvn clean install then clean and install are 2 different phases. For the mvn test we can use the surefire plugin:

mvn surefire:test

Commonly Used Maven Commands

1)mvn test: This command tests the compiled source code using a suitable unit testing framework.

2)mvn install: This Command uses the mvn jar plugin to create the project Jar in the project location that can be shared.

3)mvn clean: Delete all the previous Jars and data from the previous run and create new Jars.

4)mvn test -Dtest = test case name: Executes a single test case.

General Phrases Used In Maven

Group Id: A unique name for the Project.

Artifact Id: It Refers to the name of Jar.

Version Id: Refers to the Version of the Project.

Maven Artifact: Refers to the Jar file that gets deployed to the maven repository.

Maven Plugins

Maven has two types of plugins that are used to reuse the commonly build logic across different projects.

1)Build Plugins

2)Reporting Plugins

By using maven plugins several goals can be accomplished like Create Jar File, Create War File, Unit Testing, Compilation, Project Report Creation, etc.

Syntax:

mvn [plugin-name]:[goal-name]
mvn compiler:compile

Example of Build Plugin:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
</build>

Build plugins execute during the build process and are defined under the “<build/>” element of POM.

Example of Reporting Plugin:

<project>
  <reporting>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-report-plugin</artifactId>
        <version>3.0.0-M7</version>
      </plugin>
    </plugins>
  </reporting>
 </project>

Report plugins are used to generate the report as part of project Reports. These plugins are configured in the  “<reporting/>” element of the pom.xml.
By Leveraging the power of Maven, the Automation framework can be more efficient and more powerful.
Parallel execution can be achieved using Maven, Multiple profiles can be built for security and robustness. A single framework can be developed to achieve web, mobile, and API automation using Maven multi-module approach.