Maven | Running multiple TestNG.xml files using jar files. (Part-3)

As we read that a jar file should have a main method to start the program execution. But what if we have multiple TestNG.xml files in a single project?

In this part, we will learn:-

  1. Managing multiple TestNG.xml files via main().
  2. Creating jar file using Maven’s install feature.
  3. Create a package with required folders and files along with Jar.
  4. Create a separate batch file to pass the TestNG XML file name while executing the jar files.

Maven Project Creation

  1. Create a new Maven Project from the File menu.
  2. Now create TestNG test files under the src/test/java folder.
  3. Create multiple TestNG.xml files based on the requirement.
  4. Add a class in the src/main/java folder which should contain the main() method.
  5. Here please ensure that, while adding the dependency in the pom.xml file. The <scope> should be set as “compile” instead of test, for TestNG libraries. This will allow us to keep the test and main classes in separate folders.
Setting scope of the library file as compile instead of test.

Access multiple TestNG.xml file in main()

  1. Write code to access TestNG XML files in the main().
  2. Here we will use the argument of the main() to pass the name of the required TestNG XML file.
  3. We will pass arguments while running the jar file and main() will invoke the required XML file.
public static void main(String[] args) {
		//Scope of testng library file should be "compile". 		
		TestNG test=new TestNG();
		List<String> suite=new ArrayList<String>();
		
		String targetXMLSuite=".\\TestNGXML\\"+args[0]+".xml";
		System.out.println("Current TestNG File= "+targetXMLSuite);
		suite.add(targetXMLSuite);
		test.setTestSuites(suite);
		test.run();
	}

Code Example

pom.xml in Maven project

Ensure to keep <scope> as compile instead of test for library files. And manifest is point to the class having main() method.

<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>com.allinoneblogs</groupId>
	<artifactId>MavenDemo</artifactId>
	<version>0.0.1-SNAPSHOT</version>


	<name>MavenDemo</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<maven.compiler.source>1.6</maven.compiler.source>
		<maven.compiler.target>1.6</maven.compiler.target>
	</properties>

	<build>

		<!-- Default path of the source files -->
		<sourceDirectory>src</sourceDirectory>
		<plugins>

			<!-- Contains Maven compiler plugin information -->
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.7.0</version>
				<configuration>
					<release>10</release>
				</configuration>
			</plugin>

			<!-- Contains Maven Assembly plugin information -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-assembly-plugin</artifactId>
				<configuration>

					<!-- This will combine all the supporting library files defined in dependencies 
						tag. -->
					<descriptorRefs>
						<descriptorRef>jar-with-dependencies</descriptorRef>
					</descriptorRefs>

					<!-- This section contains the exact path till Class file haing the 
						main() in it. -->
					<archive>
						<manifest>
							<addClasspath>true</addClasspath>
							<mainClass>com.allinoneblogs.MavenDemo1.MainClass</mainClass>
						</manifest>
					</archive>
				</configuration>

				<!-- Setting execution methods and packing -->
				<executions>
					<execution>
						<id>make-assembly</id> <!-- this is used for inheritance merges -->
						<phase>package</phase> <!-- bind to the packaging phase -->
						<goals>
							<goal>single</goal>
						</goals>
					</execution>
				</executions>

			</plugin>
		</plugins>

	</build>

	<dependencies>
		<!-- https://mvnrepository.com/artifact/org.testng/testng -->
		<dependency>
			<!-- https://mvnrepository.com/artifact/org.testng/testng -->
				<groupId>org.testng</groupId>
				<artifactId>testng</artifactId>
				<version>7.3.0</version>
				<scope>compile</scope>
			</dependency>

		<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
		<dependency>
			<groupId>org.seleniumhq.selenium</groupId>
			<artifactId>selenium-java</artifactId>
			<version>3.141.59</version>
		</dependency>

		<dependency>
			<groupId>com.google.inject</groupId>
			<artifactId>guice</artifactId>
			<version>4.1.0</version>
			<classifier>no_aop</classifier>
			<scope>compile</scope>
		</dependency>

		<!-- https://mvnrepository.com/artifact/com.beust/jcommander -->
		<dependency>
			<groupId>com.beust</groupId>
			<artifactId>jcommander</artifactId>
			<version>1.72</version>
		</dependency>
	</dependencies>
</project>

TestA.java under Test folder

We have to create a drivers folder to keep all the exe files like chromedriver.exe file in the project.

package com.allinoneblogs.MavenDemo1;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;

/**
 * @author ashok.kumar
 */
public class TestA 
{
	static WebDriver driver;
	
	@Test
	public void launchBrowser()
	{
		System.setProperty("webdriver.chrome.driver", "drivers\\chromedriver.exe");
		driver=new ChromeDriver();
	}
	
	@Test (dependsOnMethods = "launchBrowser")
	public void launchURL()
	{
		driver.get("https://google.com");
		System.out.println("URL lanuched successfully.");
	}
	
	@Test(dependsOnMethods = "launchURL")
	public void searchKeyword()
	{
driver.findElement(By.name("q")).sendKeys("Computer"+Keys.ENTER);
	}	
}

input.properties under UserInputs folder

We can create as many as folders to take inputs from excel, text files or properties files.

demoURL=https://www.demourl.com

TestB.java under Test folder

package com.allinoneblogs.MavenDemo1;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import org.testng.annotations.Test;

/**
 * @author ashok.kumar
 */
public class TestB 
{
	@Test
	public void demoURL() throws IOException
	{
		File f=new File("UserInputs\\input.properties");
		FileInputStream fis=new FileInputStream(f);
		Properties prop=new Properties();
		prop.load(fis);
		
		System.out.println("Demo URL= "+prop.getProperty("demoURL"));
		
		System.out.println("Demo URL lanuched.");
	}
	
	@Test
	public void demoTest()
	{
		System.out.println("Demo Test completed.");
	}
}

TestA.xml file under TestNGXML folder

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
<suite parallel="false" name="Live Test Suite">
<test name="Alpha_Test1">
	<classes>
        <class name="com.allinoneblogs.MavenDemo1.TestA"></class>
    </classes>
</test>  
</suite> <!-- Suite -->

TestB.xml file under TestNGXML folder

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
<suite parallel="false" name="Demo Test Suite">
<test name="Alpha_Test1">
	<classes>
    <class name="com.allinoneblogs.MavenDemo1.TestB"></class>
    </classes>
</test>
</suite> <!-- Suite -->

MainClass.java under Main folder

package com.allinoneblogs.MavenDemo1;
import java.util.ArrayList;
import java.util.List;
import org.testng.TestNG;

/**
 * @author ashok.kumar
 */
public class MainClass {
	public static void main(String[] args) {
		//Scope of testng file should be "compile". This will allow us to keep files in Test and Main folders.
		
		TestNG test=new TestNG();
		List<String> suite=new ArrayList<String>();
		
		String targetXMLSuite=".\\TestNGXML\\"+args[0]+".xml";
		System.out.println("Current TestNG File= "+targetXMLSuite);
		suite.add(targetXMLSuite);
		test.setTestSuites(suite);
		test.run();
	}
}

Generating Jar file in Maven

Now we will generate the jar file and package all the required folders and files in it.

  1. Right-click on the pom.xml file and click on Test from the context menu. It will run all the TestNG.xml files in the project and give output.
  2. Now right-click on the project folder and click on Install under the RunAs menu. Again all the TestNG.xml files will execute and output will generate along with the jar files.
  3. The jar files would be generated under the targets folder.

Packaging the Distributable folder

Now, create a package by adding the required folders. In this example we are adding below:

  1. UserInputs folder: contains properties files(input.properties) to be used in the program.
  2. drivers folder: Contains required .exe files like chromedriver.exe
  3. TestNGXML folder: Contains all TestNG XML files (TestA.xml and TestB.xml) which would be invoked by the main() during execution.
  4. Report folders: The default test-output folder will be created automatically via TestNG. But if we are using ExtentReports we have to add a reports folder as well.
  5. Jar file: Add the generated jar with the dependency file.
  6. Batch file: Now we have to create a separate batch file for each TestNG.xml. While creating these batch files we have to pass the XML file name as an argument. As below:
    • Example: java -jar <demo.jar> <testng_xml_filename>
    • To invoke TestA.xml file: java -jar demo.jar TestA
    • To invoke TestB.xml file: java -jar demo.jar TestB
Final Maven distributable package with jar file.

Maven:


Leave a Comment

Your email address will not be published. Required fields are marked *

   YouTube ChannelQuora

            Ashok Kumar is working in an IT Company as a QA Consultant. He has started his career as a Test Trainee in manual testing in August 2010. Then he moves towards the automation testing after 4 years. He started learning JAVA and Selenium by self to get the knowledge of automation.

       While learning these tools and working on multiple projects, he found that sometimes people get stuck in live scenarios in their project and they have to do lots of RnD to get out of it. So he decided to start blogging only for such scenarios, where anyone facing any problem in their project, can ask any question or give a solution or you can say an alternate solution to achieve the goal successfully.

Later on, he observed that some people want to learn Java but they have few questions in their mind like how to start Java, whether we should go for the online or offline course. So he started writing tutorials on Java, Jira, Selenium, Excel etc.