Learn Maven, TestNG, and Selenium

This post guides how to create a maven project from the scratch and integrate it with TestNG. If you are a beginner at test automation, this post may help you.

Setting up Eclipse

  1. Download Eclipse from here, make sure you select Eclipse for Java Developers.
  2. Make sure you install testng plugin for Eclipse from Eclipse Market space. Help -> Eclipse Marketspace and search for testng, and install it

Creating a Maven Project

  1. Let’s first create a maven project. Go to File -> New -> Project -> Maven Project.
  2. Let’s create a project from an archetype. An archetype is nothing but a template. In the filter section type org.apache.maven.archetypes, and select maven-archetypes-quickstart.

3. Provide a name for the Group Id and the Artifact Id as below, and click on Finish. Your maven project will be created.

4. Let’s clean the pom because we will be adding dependencies and plugins later in this post. Just have only the following at your pom, and delete the rest.

5. If your project shows an error, just right click on the project -> Maven -> Update Project.

6. Now right click on the project again, select Run AS -> Maven clean

7. Your build should be successful.

Adding dependencies and plugins

What are dependencies?

In this test project, we will use many libraries, jar files. If we are going to download them manually, it would be a tedious task. Therefore we will be adding them as a maven dependency. All these jars are available in the maven central repository. Once we add a dependency, they will get downloaded from the maven central. If we run this for the first time, it will download and put them to the .m2 folder on our local computer. (~/.m2). Therefore we do not need to download anything manually.

Since we hope to use selenium and testng in this project, I’m going to add the selenium and testng dependency as below.

How to get these dependencies?

You can go to the maven central repository, search for selenium java, and select the version. Select it and can copy the maven value. Similarly, you can get the testng dependency as well.

After saving the pom file, it will download these jars automatically and you can see them in the Maven Dependencies folder in the project structure.

What are plugins?

Using Maven plugins we can get done the tasks that we want, such as

  • maven-compiler-plugin: The Compiler Plugin is used to compile the sources of your project.
  • maven-source-plugin: The Source Plugin creates a jar archive of the source files of the current project. The jar file is, by default, created in the project’s target directory.
  • maven-surefire-plugin: The surefire plugin runs the tests in the project.

Let’s add the required plugins to the pom file.

We need to add plugins, inside build component. Therefore, the complete pom file will be as below.

Now let’s create some tests.

  1. Let’s create a class in src/test/java folder. Name the class as ArithmaticOperations. Add the following method. Make sure to add the annotation test.

2. Right-click on the project and run the test as a TestNG test. You will get the below output.

[RemoteTestNG] detected TestNG version 7.3.0PASSED: TesMultiplicationPASSED: TestAdditionPASSED: TestSubstraction===============================================Default testTests run: 3, Failures: 0, Skips: 0===============================================

3. But we want to run them as a maven build. And we need to run several tests classes in the same project.

Create another class called StringOperations.java as below.

4. If we want to run both test classes as a mvn project, we need to include them in the testng.xml file. Let’s create testng.xml file.

Right-click on the project folder and create a source folder as below.

5. Provide the folder name as src/main/resources. In that folder create testng.xml file.

6. The testng.xml file should contain the following. Give the whole package name to run all the classes inside it.

7. Add the below configuration to maven-surefire-plugin as below.

8. Now go to the project location in the terminal, and run the mvn clean install command.

It will run along with the tests.

You can find the full source from here.

Senior Software Engineer — QA at WSO2

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store