Automation Testing Framework using Selenium WebDriver and Java : Day 4

In the last article from our POM series, we were able to run framework tests from testng.xml file. Today, we will see how to run the same from command prompt.

Plugin requirement :

In our POM.xml file, we need to add Surefire plugin, which will enable Maven to read the testng.xml (or any test runner .xml) file and execute automated tests.

According to official documentation :

The Surefire Plugin is used during the test phase of the build lifecycle to execute the unit tests of an application. It generates reports in two different file formats:

  • Plain text files (*.txt)

  • XML files (*.xml)

<plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>3.2.5</version>
        <configuration>
          <suiteXmlFiles>
            <suiteXmlFile>testng.xml</suiteXmlFile> 
          </suiteXmlFiles>
        </configuration>
      </plugin>
</plugins>

We need to provide the test runner .xml file path within the <suiteXmlFile>. In case of our automation project, it will be :

<plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>3.2.5</version>
        <configuration>
          <suiteXmlFiles>
            <suiteXmlFile>src/test/resources/testrunners/testng.xml</suiteXmlFile> 
          </suiteXmlFiles>
        </configuration>
      </plugin>
</plugins>

Run the tests from Command Line:

Open command prompt and go to the project directory.

Run the command mvn clean test or mvn test:

In case of multiple test runner files :

Suppose we have multiple test runners i.e. sanitytest.xml, regresion_01_tests.xml, regresion_02_tests.xml etc. We cannot run all test runners at once and we don't want to do that. In order to run a specific runner file, we need to change in the POM.xml file under Surefire plugin :

<plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>3.2.5</version>
        <configuration>
          <suiteXmlFiles>
            <!-- <suiteXmlFile>src/test/resources/testrunners/testng.xml</suiteXmlFile> -->
            <suiteXmlFile>${suiteXmlFile}</suiteXmlFile>
          </suiteXmlFiles>
        </configuration>
      </plugin>
</plugins>

Now, go to project directory and open command line prompt.

Run the command :

mvn clean test -DsuiteXmlFile=src/test/resources/testrunners/testng.xml

Note
If the test runner files are not located as above file, and located directly under project root structure, we can just write mvn clean test -DsuiteXmlFile=testng.xml

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716008881052/6c826eec-c452-4227-bb2f-32958b4cba82.png align="center")

Surefire reports :

The Surefire plugin generates reports as well, under the default folder "target" :

Copy the emailable-report.html file path and open in any browser :

It is a very basic TestNG report which includes all details regarding our testsuite.

In the later articles, we will see how to generate better looking reports like the Allure Report and Extent Reports.

GitHub repo : https://github.com/debasmita-a/cura-herokuapp-automation-testing

Thank you ! :)

0
Subscribe to my newsletter

Read articles from Debasmita Adhikari directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Debasmita Adhikari
Debasmita Adhikari

I am a software engineer with interest in Automated testing. I am here to share my learning experiences.