Maven Tutorial For Selenium Testing

In this post, we will learn about Maven build tool and we need to use Maven. What are the different advantages of Maven?

Maven is a build/project management tool provided by Apache Foundation and it is an open source tool. It is based on POM (Project Object Model) which contains all project information such as dependencies, plugins, source directory, test source directory etc.

In this post, we will learn about Maven build tool and we need to use Maven. What are the different advantages of Maven?

Maven is a build/project management tool provided by Apache Foundation and it is an open source tool. It is based on POM (Project Object Model) which contains all project information such as dependencies, plugins, source directory, test source directory etc.

In this post, we will learn about Maven build tool and we need to use Maven. What are the different advantages of Maven?

Maven is a build/project management tool provided by Apache Foundation and it is an open source tool. It is based on POM (Project Object Model) which contains all project information such as dependencies, plugins, source directory, test source directory etc.

In this post, we will learn about Maven build tool and why we need to use Maven. What are the different purposes of Maven, what is POM.xml file etc. ?

What is Maven in Selenium?

Maven is a build/project management tool provided by Apache software Foundation and it is an open source tool. It is based on POM (Project Object Model) which contains all project information such as dependencies, plugins, source directory, test source directory etc.

Why Maven:

We know for every coding or test scripting we need to create a project. So in any project we need some dependencies and 3rd party libraries (Ex. If I am working with selenium then I need to download and import jar files of selenium into project).  If I am not using Maven, we have to do all these process (downloading and importing jars) manually.

Also if in future I want to upgrade the jar files with latest version then again I have to download and import all jars into my project manually. It is very tedious task to do these things.

Now Maven comes into picture for removing the manual process of all above activities.

There are different purposes of using Maven.

  • Project Management (Provide common folder structure)
  • Use of POM.xml to manage dependencies and Plugins
  • Reporting
  • Generate documentation
  • Packaging

How Maven Project works internally?

The main part of Maven is POM.xml file.

POM.xml contains all information about project details like Project name, version, group Id, artifact Id and dependencies, plugins etc.

Sample structure of POM.xml file:

<project xmlns="http://maven.apache.org/POM/4.0.0"    
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
   http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.automationTestings.App1</groupId>
    <artifactId>My-First-App</artifactId>
    <version>1.0</version>
</project>

In above structure, we can add dependencies tag if we are using any dependencies in our Project. For example if we are using maven for Selenium then we need to add selenium related dependency in pom.xml file.

Sample structure of POM.xml file with dependencies tag  [pom.xml for Selenium]

<project xmlns="http://maven.apache.org/POM/4.0.0"    
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
   http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.automationTestings.App1</groupId>
    <artifactId>My-First-App</artifactId>
    <version>1.0</version>

<dependencies>  
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>3.141.599</version>
    </dependency>  
</dependencies>  

</project>



Maven Repository

As soon as when we create Maven project, By default it will create Maven local repository into our local drive in .m2 folder.

When we add dependencies in pom.xml file, it will download all 3rd party jars into maven local repository from maven remote repository.

Here internet connection is required to download the libraries from remote repository.

In future if we want to upgrade the version of selenium jars then just change the version of jar in pom.xml dependency tag. It will automatically download the updated jars from remote repository to local repository.

So this is how maven internally works.


Maven Plugins

In pom.xml, all project related configuration is done into plugins. For example to compile the source code, run the source code we need to add plugins.

Sample structure of POM.xml file with Plugins tag

<project xmlns="http://maven.apache.org/POM/4.0.0"    
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
   http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.automationTestings.App1</groupId>
    <artifactId>My-First-App</artifactId>
    <version>1.0</version>


<dependencies>  
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>3.141.599</version>
    </dependency>  
</dependencies>  

<build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>

</project>

So far, we have learnt basics of maven like what is Maven, what is dependency, plugins. How maven internally works, what is pom.xml file and what maven repository is.

Now we will see practically to create Maven project into eclipse, folder structure and Project object Model (pom.xml).


How To Install Maven in eclipse


How to create maven project in eclipse for Selenium

Steps:

  1. Open eclipse.
  2. Go to File –> New–> Click on Maven Project

Create maven project for selenium testing

3. Select checkbox of ‘Create a simple project’

Maven tutorial for Selenium Testing

4. Click on Next

5. After click on next button below screen will show where we will enter group Id, artifact Id etc.

Maven group id and artifact Id

 Note: In above screen Group Id basically represents your company name and Artifact id representing the name of project name. Version is by default – 0.0.1-SNAPSHOT and which format we want to package the source code that we can select from Packaging dropdown.  

 

 

After click on Finish, maven project is created. You can see in below image.

Maven Project in eclipse


Maven Folder Structure

Now we will discuss about folder structure of Maven Project.

Maven Folder Structure

src/main/java : This folder contains the source code. If a developer is working on any project then he will develop the source code in this folder.

src/main/resources: While developing the source code if developer needs any resources that will store in this folder.

src/test/java: After developed any code by developer we know this they need to be perform unit testing so this folder contains the all unit tests which is developed by the developers.

src/test/resources: While developing unit testing if they need any resources then they will store resources into this folder.

Apart from these above folders, we have also two separate folder src and target folder which is used by maven at run time to store temporary files.

Apart from this there is a main file called POM.xml. This is most important file which controls the whole maven project. By using this we will control and organize whole maven project.


Let’s understand the POM.xml file

Maven POM.xml

Maven pom.xml

Project: This is the root tag of pom.xml file.

modelVersion: This shows the modelVersion of the project. By default it is set 4.0.0.

groupId: What we have given groupId at the time of maven project creation that shows here in this tag. It is the unique ID of the project group. By using this we can uniquely identifies our project across all project.

artifactId: What we have given artifactId at the time of maven project creation that shows here in this tag. It shows the Id of project. It is actually name of your project. See in below image.

artifact ID

version: It shows the version of artifactId in the particular group.


I will create a demo project and try to explain how maven commands work.

Let’s create a Package and inside this create a class.

Here I have written some piece of code for login, not for testing purpose, this is actually login functionality code. Here we will use one config file which we need to store into src/main/resources folder.

maven-project

To develop the login application we will maintain the data into config.properties file.

Here login functionality Code:

package com.automationtestings;

import java.util.ResourceBundle;

public class App {
    
    public int userLogin(String in_user, String in_pwd) {
        
        ResourceBundle rb = ResourceBundle.getBundle("config");
        
        String username = rb.getString("username");
        String password = rb.getString("password");
        
        if(in_user.equals(username) && in_pwd.equals(password)) {
            
            return 1;
            
        }else
            return 0;
        
        
    }

}

Explanation:

Here we have created on method called userLogin which will take two arguments username and password. Internally it will username and password from config.properties file. In config.properties file we have maintained the username and password. See in below image.

So in this method we are loading the username and password from config file by using ResourceBundle class.

Store that username and password into a String.

In if conditions we are checking if both username and password equal then it will return 1 (login successfully) otherwise it will return 0 (login unsuccessful).

So this is basically development code.

Once the developer developed this code in src/main/java, he has to test this functionality. So for testing this code he will write the code under src/test/java folder using testNG.

For using the testNG we need to add dependency of testNG into pom.xml file. See in below image.

TestNG Dependency in pom.xml

As soon as when we saved this dependency in pom.xml file we can a folder called Maven dependencies with testng jar will be created. These jars file downloaded from remote repository to local maven repository.

See in below Image.

Maven Dependencies Folder

Here we will write code for unit testing into src/test/java.

package com.automationtestings;

import org.testng.Assert;
import org.testng.annotations.Test;

public class AppTest {
    
    @Test
    public void testLogin1() {
        
        App myapp = new App();
        Assert.assertEquals(0, myapp.userLogin("abc", "abc123"));
        
    }
    
    @Test
    public void testLogin2() {
        
        App myapp = new App();
        Assert.assertEquals(1, myapp.userLogin("abc", "[email protected]"));
        
    }

}

When we run it by right click and click on textNG then output will be like below.

So far we have seen How to create Maven project, Folder structure of Maven, How to add dependencies in pom.xml, where developer writes the source code for develop the any application, where store the resources and where he writes the unit testing code.


What is Maven build lifecycle?

Now we will execute the above test using pom.xml. When we execute any code using pom.xml in maven, it follows the maven build life cycle process which contains the some phases.

Below are the names of phases which always follow by maven during execution.

Maven Build Life Cycle Phases:

  1. Validate
  2. Compile
  3. Test
  4. Package
  5. Verify
  6. Install
  7. Deploy

Validate: First it will validate the code. Whatever we have written, all project information is available or not, first we need to validate it.

Compile: In this phase it will compile the code.

Test: Test the source code which we have complied in above phase.

Package: Whatever we have tested, it will package it into distributable format such as JAR/WAR format as mentioned into POM.xml file.

Install: In this phase, it will install the package into maven local repository.

Deploy: In this phase, copy the final package into the remote repository which is use by other project.

 

Here we are going to run our code using pom.xml.

Right Click on POM.xml –> Run as –> Click on maven test

run-pom.xml

 Note: 1. When we click on Run As  maven test, it will also execute the phases which we have before the Test phase (It will execute Validate, Compile and Test phase). 2. When we will execute maven install then it will also execute the phases which we have before the Install phase (It will execute Validate, Compile, Test, package then Install phase)  

Maven Build Command:

When we Run as–> Maven build then we need to specify the Goal.

In Goal, we can specify what is our goal like clean, test, install etc.

Maven Build Command

goal-clean-command

As you can see in goal section I have written à clean, means it will clean the project.

Click on run button.

As soon as click on run button we can see result in below image.

It will clean the target folder.

clean maven project


Maven Compile Command

Same like if we want to execute compile command then follow below steps –

  1. Right click on pom.xml
  2. Mouse hover on Run As –> Click on Maven build..

3. Enter goal –> compile –> click on Run

Maven Goal Compile

It will compile the source code and gives output.

 Note: Sometimes it gives error while compilation like – No compiler is provided in this environment. Perhaps you are running on a JRE rather than Jdk. To resolve this issue you have to set JDK in Windows –> Preferences–> Java–> Installed JRE –> Select JDK from C driver:Java folder. You can see in below image I have already selected JDK.  

 


Maven Install Command:

We will follow same steps as above mentioned.

  1. Right click on pom.xml
  2. Run As à Click on Maven install

(Here we can select maven build.. and put install in goal, same like above)

Maven Install Command

We can see in the output it is executed all phases which come before install phase.

Output:

So this is how we can execute some basic commands in maven.

In the next Maven tutorial we will execute these maven commands without eclipse.

Thanks for reading. Happy learning 🙂

Summary

In this tutorial, we have learnt “How to create Maven Project in eclipse”  , ‘Why used Maven’, ‘What is POM.xml file‘ and ‘What is are different elements used in POM.xml file‘ , ‘Maven Build life cycle‘ etc.

Hopefully, this post was helpful to you.

Bookmark this Post “Maven Tutorial for Selenium” for your future reference.

If you have any other questions, the comment section is yours. Don’t forgot to leave a comment below!

 

 

Leave a Comment