Create A Simple Maven Project Using TestNG

Prerequisites

What Is Maven Project

Maven is an automation and project management tool that is based on Project Object Model (POM). Building a software project is kind of hectic job and often consists of tasks like downloading dependencies, adding JAR files on a classpath, packaging compiled codes into deployable artifacts such as JAR , WAR , ZIP files and deploying all these to an Application Server.

Apache Maven comes into the action by automating these tasks, which also reduces the risk of human errors while building software manually. In this tutorial we are going to explore Apache Maven by creating a simple maven project with complete build life cycle.

Installing Maven

We will have to download Maven from its official site. Once downloaded some setup is required to utilize the power of Maven.

Assuming that you already downloaded and finished the setup required for Java Development Kit (JDK) please proceed to the Maven setup. If not, then please see our other article Getting Started With TestNG And Selenium Web Driver for a quick start guide.

Firstly, we need to extract the apache-maven-x.x.x.zip file to our system directory, for example if you are using Windows System, you need to paste the extracted files to the C:\Program Files\Maven

Now, we need to update our system’s path variable with the following value –

C:\Program Files\apache-maven-3.6.3\bin

Next, create one new system variable with the following variable name and value –

Variable Name: M2_HOME
Variable Value: C:\Program Files\apache-maven-3.6.3

You may test your installation by typing the following command in your Terminal / Console.

λ mvn --version
Apache Maven 3.6.3
Maven home: C:\Program Files\apache-maven-3.6.3
Java version: 14.0.1, vendor: Oracle Corporation
Default_locale: en_US
OS name: Windows 10

Creating A Maven Project

To start working with Maven, we need to create one project in eclipse with proper Group ID and Artifact ID like shown below.

After clicking Finish, one new Maven project will be created. On your project files tab you will see one pom.xml file which holds all the configuration information for maven to build the project such as dependencies, build directory, plugins etc.

Your default project directory will look something like this -

You can create packages and classes according to your requirement. Each time you save the project new JAR versions will be downloaded from the network automatically, this is the most interesting and advantageous side of creating Maven projects. You don’t need to add the updated JAR files each time.

Building Test Cases for Maven Project

Now it’s time to build test cases to execute with Maven. In order to do that first you need to create packages under src/main/java as per your requirement.

 

I have created two packages and two classes under those packages namely LoginPage.java and LoginPageAction.java

This loginPage.java file, will be used for Login Page Locator, and the other one i.e LoginPageAction.java will store some actions like Click , Send Usage Data , Submit etc.

LoginPage.java

LoginPageAction.java

Now for this project I have created another package with name Test under src/test/java folder and two classes selTest.java , sanityTest.java inside of that “Test” package.

selTest.java

Here selTest.java will be treated as base class, where you will find the logic behind working mechanism of the browser and url parsing.

sanityTest.java

sanityTest.java is the main test class, which I have written using TestNG annotation.

As you can see, I have developed a whole framework so I don’t need to write the test cases again in the SanityTest.java. I just called all the methods which are needed and extended the base class otherwise the browser action will not happen.

It's a good practice to use excel sheet, data provider, properties file, xml file to maintain all user data.

.properties file

So, guys here I explained my framework and its structure, but its not the only way to develop test cases using Maven. You can just create a package under src/test/java and accordingly create classes as per your requirements.

Execute As Maven Project

Now we are all set to execute our project for that right click on the project, and select the Run As >> Maven Clean

You will get Build Success after a successful execution. We need to do Maven Clean to delete all previously compiled java sources and resources.

Now to run the project you have to right click on Run As >> Maven Test

On successful execution, it will open a new browser window and will login to the specific site using the specified username / email, password provided in the .properties file.

Running Existing Project As Maven

If you don’t have any Maven project but want to execute your existing project in Maven, then it can be done by following these simple steps –

Just right click on project Configure >> Convert to Maven Project that will open Create new POM window. Fill in the details with Group Id, Artifact Id and click on Finish to create a pom.xml file in your project directory.

Once created you will be able to add all of your dependencies there.

After doing all these, you can follow the above mentioned steps under Execute As Maven section.

You can download this complete tutorial from my GitHub Repository

Copy link
Powered by Social Snap