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. Imagine a scenario where a user downloads 100 Jars and after a few days, 50 of them require to be updated. In the traditional Java project, it will be a very tedious task 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 operates within distinct lifecycles, each comprising various phases tailored to different project requirements:

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

2) Clean LifeCycle: Clean LifeCycle includes PreClean, Clean and PostClean phases.

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 follow a structured format, comprising “mvn” followed by either a phase name or a plugin and its associated goal.

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.

Essential Maven Terminology

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 offers 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: Refer to the below-mentioned example for the mvn compiler plugin, which is a 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: Refer to the syntax of the Maven surefire plugin, which is a reporting plugin. 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.

<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>


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 the Maven multi-module approach.

Discover more from AutomationQaHub

Subscribe now to keep reading and get access to the full archive.

Continue reading