Execute Testng.xml from command line

We can execute testng.xml file in different ways.

1. Eclipse
2. InteliJ
3. Ant / Maven
4. Command Line
5. Batch File.

In IDE you can just right click on testng.xml file and click on Run as 'TestNG' will invoke your tests. And we can also invoke TestNG.xml with Ant build.xml file and we also by doing some simple configuration to maven pom.xml which we have see examples earlier

Now let us see how to invoke testng.xml from command line step by step:

Step 1: Create new project, In example we have created project 'SampleTestNG'

Step 2: Create new package. 'com.test' as example package created here.

Step 3: Create sample classes which has @Test methods. In the example below we have created four classes as below :

testng project structure

Class - Browser.java, which has getBrowser(String browserType) method which accepts browser Type and returns driver based on the string passed by the user.

package com.test;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

public class Browser {

	static String driverPath = "F:/Selenium/SampleTestNG/lib/";
	
	public static WebDriver driver;
	
	public static WebDriver getBrowser(String browserType) {
		switch (browserType) {
		case "firefox":
			return	driver = new FirefoxDriver();
		case "chrome":
			System.setProperty("webdriver.chrome.driver", driverPath+"chromedriver.exe");
			return	driver = new ChromeDriver();
		case "IE":
			System.setProperty("webdriver.ie.driver", driverPath+"IEDriverServer.exe");
			return	driver = new InternetExplorerDriver();
		default:
			System.out.println("browser : " + browserType + " is invalid, Launching Firefox as browser of choice..");
			return driver = new FirefoxDriver();
		}
	}
}

We will now try to create three different classes each passing different browser type (Firefox, chrome and InternetExplorer) and create test method in each class.

First create a class and get browser firefox :

package com.test;

import org.openqa.selenium.WebDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class TestA {
	
	WebDriver driver;
	
	@BeforeClass
	public void setUp() {
		System.out.println("*******************");
		System.out.println("launching firefox browser");
		driver = Browser.getBrowser("firefox");
		driver.manage().window().maximize();
	}
	
	@Test
	public void testGooglePageTitleInFirefox() {
		driver.navigate().to("http://www.google.com");
		String strPageTitle = driver.getTitle();
		Assert.assertTrue(strPageTitle.equalsIgnoreCase("Google"), "Page title doesn't match");
	}

	@AfterClass
	public void tearDown() {
		if(driver!=null) {
			System.out.println("Closing firefox browser");
			driver.quit();
		}
	}
	
}

Create another class 'TestB.java' and get browser chrome driver to execute tests in chrome browser :

package com.test;

import org.openqa.selenium.WebDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class TestB {
		
	WebDriver driver;
	
	@BeforeClass
	public void setUp() {
		System.out.println("*******************");
		System.out.println("launching chrome browser");
		driver = Browser.getBrowser("chrome");
		driver.manage().window().maximize();
	}
	
	@Test
	public void testGooglePageTitleInChrome() {
		driver.navigate().to("http://www.google.com");
		String strPageTitle = driver.getTitle();
		Assert.assertTrue(strPageTitle.equalsIgnoreCase("Google"), "Page title doesn't match");
	}

	@AfterClass
	public void tearDown() {
		if(driver!=null) {
			System.out.println("Closing chrome browser");
			driver.quit();
		}
	}
}

Create another class 'TestC.java' and get browser chrome driver to execute tests in IE browser:

package com.test;

import org.openqa.selenium.WebDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class TestC {
	
	WebDriver driver;
	
	@BeforeClass
	public void setUp() {
		System.out.println("*******************");
		System.out.println("launching IE browser");
		driver = Browser.getBrowser("IE");
		driver.manage().window().maximize();
	}
	
	@Test
	public void testGooglePageTitleInIEBrowser() {
		driver.navigate().to("http://www.google.com");
		String strPageTitle = driver.getTitle();
		System.out.println("Page title: - "+strPageTitle);
		Assert.assertTrue(strPageTitle.equalsIgnoreCase("Google"), "Page title doesn't match");
	}

	@AfterClass
	public void tearDown() {
		if(driver!=null) {
			System.out.println("Closing IE browser");
			driver.quit();
		}
	}
}

The below is the testng.xml file, which will execute all the tests that are available under 'com.test' package.

In the above classes, each class has a test method with @Before and @After class which will open browser, validate Google Home page title and close browser. In IDE, it is very simple to execute to click on Run as TestNG.

The below is the testng.xml file

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Main Test Suite" verbose="2">

    <test name="TestNG Test Group">
        <!-- <classes>
        <class name="com.test.TestA"/>
        </classes> -->
        <packages>
        <package name="com.test"/>
        </packages>
    </test>
    
</suite>

Here is below commands to execute 'testng.xml' file from command line

cd F:\Selenium\SampleTestNG
java -cp F:\Selenium\SampleTestNG\lib\*;F:\Selenium\SampleTestNG\bin org.testng.TestNG testng.xml

After executing above command, it should execute the tests that we have specified in testng.xml file. And the below is the screen shot after executing the tests.

testng command line statement

In the above command, for 'java -cp' argument, we provide the classpath i.e. path to classes / libraries that our program requires to run. You can check out Oracle Docs for more information on class path.

Test Frameworks: 

Comments

I'm getting following error on executing testng.xml from command prompt - Error - Couldn't find or load main class.

If you are still facing that issue try this, JVM should know where the testng jar file is, the classpath that you are giving may not contains testng jar file, add testng jar file to libs, if you have added testng as a plugin in eclipse you will find it in the plugins of eclipse(in users). It will be less than 1mb. Add it and run the batch file(not from jenkins) if it works proceed to jenkins.

Thank you sir i was getting this issue that couldnot load org.testng.TestNG class ..after your suggestion i combine jars of testng to eclipse plugin and the problem got solve

Try to keep your "lib" folder which contains all the jars outside the src folder. This might solve your problem

C:\Program Files\MohanWorkSpace\CM_Desktop>java -cp bin;lib/* org.testng.TestNG\
testng.xml
Error: Could not find or load main class org.testng.TestNG\testng.xml

C:\Program Files\MohanWorkSpace\CM_Desktop>

There is some path issue coming on console, lib path that i am using is D:\C drive back up\workspace1\Jcb\lib\*
Getting error could not find or load main class drive

Hello,
I get this error. Any idea why? class files are all in bin folder. Thanks!

c:\eclipse2\Workspace\NDM3a>java -cp org.testng.TestNG testng.xml
Error: Could not find or load main class testng.xml

c:\eclipse2\Workspace\NDM3a>java org.testng.TestNG testng.xml
[TestNG] [ERROR]
Cannot find class in classpath: PageObject.NDM_TestSuite

I tried to run using above method but getting Cannot find class in classpath , I have included jar files in my lib folder also.
Do we need to create bin folder and add here:
F:\Selenium\SampleTestNG\bin

I want to dynamically decide which group of test cases to get executed by passing the group name from cmd line, but the
<groups>
<run> <!-- group name --></run>
</groups>
is present in testng.xml. how should i pass cmd line property value to testng.xml, please help.

After adding -cp, it worked.

Thanks

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.