How To Create a Jar for Selenium TestNg tests using Maven

Recently there was a request from the deployment team to run some GUI and API test in the production environment, and the deployment team wanted to make it as simple as possible. Since it was supposed to be deployed in multiple customer locations, and they don't want to configure the remote connections and accessibility for multiple locations across the globe. Their proposal was to give them some automated standalone tool and they run the selenium or API test and report us the test results.

In this post we'll see how to generate a jar and run TestNg tests from command line, using Maven.

1. Maven Plugin
The Maven plugin which we are interested is maven-assembly-plugin, with descriptorRef as jar-with-dependencies.
The entire pom.xml as below.
 <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>AutoTestFramework</groupId>  
      <artifactId>AutoTestFramework</artifactId>  
      <version>0.0.1-SNAPSHOT</version>  
      <dependencies>  
           <dependency>  
                <groupId>org.seleniumhq.selenium</groupId>  
                <artifactId>selenium-java</artifactId>  
                <version>3.0.1</version>  
           </dependency>  
           <dependency>  
                <groupId>org.testng</groupId>  
                <artifactId>testng</artifactId>  
                <version>6.10</version>  
                <scope>compile</scope>  
           </dependency>  
           <dependency>  
                <groupId>junit</groupId>  
                <artifactId>junit</artifactId>  
                <version>4.12</version>  
           </dependency>  
           <dependency>  
                <groupId>io.github.bonigarcia</groupId>  
                <artifactId>webdrivermanager</artifactId>  
                <version>1.5.0</version>  
           </dependency>  
           <dependency>  
                <groupId>com.sun.jersey</groupId>  
                <artifactId>jersey-client</artifactId>  
                <version>1.19.3</version>  
           </dependency>  
           <dependency>  
                <groupId>log4j</groupId>  
                <artifactId>log4j</artifactId>  
                <version>1.2.17</version>  
           </dependency>  
           <dependency>  
                <groupId>org.json</groupId>  
                <artifactId>json</artifactId>  
                <version>20160810</version>  
           </dependency>            
      </dependencies>  
      <build>  
           <pluginManagement>  
                <plugins>  
                     <plugin>  
                          <artifactId>maven-compiler-plugin</artifactId>  
                          <version>3.1</version>  
                          <configuration>  
                               <source>1.8</source>  
                               <target>1.8</target>  
                          </configuration>  
                     </plugin>  
                     <plugin>  
                          <artifactId>maven-assembly-plugin</artifactId>  
                          <version>3.0.0</version>  
                          <configuration>  
                               <descriptorRefs>  
                                    <descriptorRef>jar-with-dependencies</descriptorRef>  
                               </descriptorRefs>  
                          </configuration>  
                          <executions>  
                               <execution>  
                                    <id>make-assembly</id>  
                                    <phase>package</phase>  
                                    <goals>  
                                         <goal>single</goal>  
                                    </goals>  
                               </execution>  
                          </executions>  
                     </plugin>  
                     <plugin>  
                          <groupId>org.apache.maven.plugins</groupId>  
                          <artifactId>maven-surefire-plugin</artifactId>  
                          <version>2.16</version>  
                          <configuration>  
                               <suiteXmlFiles>  
                                    <!-- suiteXmlFile>testng.xml</suiteXmlFile-->  
                                    <!-- suiteXmlFile>suitestestng.xml</suiteXmlFile -->  
                               </suiteXmlFiles>  
                               <includes>  
                                    <include>**/Test*.java</include>  
                                    <include>**/*Tests.java</include>  
                                    <include>**/*Test.java</include>  
                                    <include>**/*TestCase.java</include>  
                               </includes>  
                          </configuration>  
                     </plugin>  
                </plugins>  
           </pluginManagement>  
           <plugins> <!-- jar with dependencies will NOT be generated without this, for #mvn package or install -->  
       <plugin>  
         <groupId>org.apache.maven.plugins</groupId>  
         <artifactId>maven-assembly-plugin</artifactId>  
       </plugin>  
     </plugins>  
      </build>  
 </project>  

2. The project structure:
The screen-shot of my automation project as below.
Note that my test classes are in src/main/java folder, and not in src/test/java folder.

3. Generating the jar:
Right click the project in eclipse and do Maven Install or Package goal. You should see 2 jars created - one without dependencies and another with dependencies, under target folder.


4. Running the test classes:
Take the jar with dependencies to a separate location or system, and run as below.
 #java -cp AutoTestFramework-0.0.1-SNAPSHOT-jar-with-dependencies.jar org.testng.TestNG testng.xml  

The testng.xml should be in the same folder, and the running system should have same jdk version installed with which the jar was built.

1 comment:

  1. Do this hack to drop 2lb of fat in 8 hours

    At least 160,000 women and men are losing weight with a easy and secret "liquid hack" to burn 1-2lbs each night as they sleep.

    It is scientific and works with anybody.

    This is how to do it yourself:

    1) Go grab a glass and fill it half the way

    2) And now follow this strange HACK

    and be 1-2lbs skinnier when you wake up!

    ReplyDelete