Search⌘ K
AI Features

How to Execute Automated Tests With Maven

Explore how to execute Selenium automated tests locally using Maven commands. Learn to clean the environment, run tests, generate execution reports, and selectively run test groups to streamline test management.

It has already been mentioned in the Project Information section (of the Selenium Automation Project chapter) that the project uses Maven for managing dependencies.

The dependencies are described in the pom.xml file:

XML
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>selenium</groupId>
<artifactId>selenium</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>selenium</name>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.9.10</version>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
</project>

Maven can do much more than managing dependencies. It also allows you to compile the automation code and execute the automated tests.

Two Maven commands are needed for these purposes.

mvn clean

mvn clean cleans up the target folder where temporary files are saved by previous code executions. Cleaning up these temporary files makes sure that the next execution happens in a clean environment.

mvn test

mvn test does quite a few things. It downloads any missing Maven or dependency files. In addition, it compiles the automation code, executes the automated tests, and generates the test execution results.

mvn clean test

Instead of running the 2 commands separately, first, mvn clean and then mvn test, the two Maven commands can be combined into one:

mvn clean test

Outcome of executing tests

It is important to remember that when running the project’s code, the listener runs at the same time with the tests.

So, after the code finishes running, we will have the following:

  • Execution status for each test
  • Screenshots taken for each passing test: They can be found in the target/screenshots folder.
  • Screenshots taken for each failing test: They can be found in the target/screenshots folder.
  • Trace log showing the test name and the names of all executed methods for each test: It can be found in the target/logs folder.
  • Exception log showing the exception info and the stack trace for each failed test: It can be found in the target/logs folder.

    Hints

    1. Execute tests that belong to a group

    If, instead of running all tests, we need to only run the tests that belong to a certain group, let’s say the "High" group, then the command uses a parameter for the group name:

    mvn clean test -Dgroups=High
    
    1. Execute tests that belong to multiple groups

    If we need to run tests that are part of multiple groups, the group names should be separated by a comma:

    mvn clean test -Dgroups=High,Low