Execute Testng.xml using batch file

In previous tutorial we have seen executing testng.xml tests from command line. Now here we will look into executing testng.xml using batch file (.bat) file. A batch file (.bat) is used in DOS and Windows, which is an unformatted text file that consists of a series of commands to be executed by the command line interpreter.

Let us jump into simple example by taking two classes each has three or more @Test methods.

First let us take a class or multiple classes which has tests so that we can execute them using batch file and view the output.

Here we will create Two class as 'TestA.class' and 'TestB.class' as show below:

Class - TestA , which has three @Test methods with @BeforeClass and @AfterClass methods

package com.test;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
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 = new FirefoxDriver();
		driver.manage().window().maximize();
	}
	
	@Test
	public void testPageTitleSampleA() {
		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");
	}
	
	@Test
	public void testSampleTwo() {
		System.out.println("Im in test sample two");
	}
	
	@Test
	public void testSampleThree() {
		System.out.println("Im in test sample three");
	}

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

Class - TestB : - which has four @Test methods with @BeforeClass and @AfterClass methods

package com.test;

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

public class TestB {
	
	public WebDriver driver;
	
	@BeforeClass
	public void setUp() {
		System.out.println("*******************");
		System.out.println("launching firefox browser");
		driver = new FirefoxDriver();
		driver.manage().window().maximize();
	}
	
	@Test
	public void testPageTitleSampleB() {
		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");
	}
	
	@Test
	public void testSampleOne() {
		System.out.println("Im in test sample one");
	}
	
	@Test
	public void testSampleTwo() {
		System.out.println("Im in test sample two");
	}
	
	@Test
	public void testSampleThree() {
		System.out.println("Im in test sample three");
	}

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

The below is the testng.xml file which has two classes that we have created above and we will be invoking this xml file using batch file (.bat).

<?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"/>
        <class name="com.test.TestB"/>
        </classes>
    </test>
</suite>

Done, Now we have created classes and testng.xml file. After doing this, the project structure should look like below image :

TestNg Batch File project structure

Now, let us create a batch file by adding the below commands in it. How to create a batch file?
[Note that batch files are specific to Windows. If you want to run tests on any platform that supports Java and TestNG then, it is recommended to run TestNG programmatically from Java instead of a batch file]

Step 1: Open notepad
Step 2: Paste the below lines of code - You may need to add your project location. In the example, project location is set as 'F:\Selenium\TestNGBatchExample'.
Step 3: Save the file as 'testNGBatchFile.bat' in location that you want to save.

set projectLocation=F:\Selenium\TestNGBatchExample
cd %projectLocation%
set classpath=%projectLocation%\bin;%projectLocation%\lib\*
java org.testng.TestNG %projectLocation%\testng.xml
pause

Now after doing above steps, now you should see a bat file as in the below image :

TestNg Batch File creation

In the above line of code, at the end of a batch file, we have added 'pause' statement to prevent auto-closing of console after the execution, which will print a nice message as 'Press any key to continue . . . ' so that we can view the output. Or, if we don't want "Press any key to continue . . ." message you can just ignore that.

And make sure, you don't add any spaces around the equal sign, if so the SET commands will not work. That's done it!. Now click on .bat file we have just created and see your tests executing.

Hope this article helps you to invoke your tests using .bat file. Feel free to comment below if you have any queries.

Test Frameworks: 

Comments

When i try to run testng.xml from command line or via jenkins using this bat file, getting same error in both cases Could not find or load main class. Can anyone help meout with this?

I converted my xml to bat file. But while running it, its not taking the data from different excel sheet.

How can we specify time in batch file to execute selenium scripts at the specified time?

Make change to java org.testng.TestNG %projectLocation%\testng.xml
to java org.testng.TestNG testng.xml.
You are already in the project location directory no need to add again

Hi, I have followed the above steps to Execute Testng.xml using batch file. I have just changed my package name and class names. apart from this everything is kept same.
After click ON .bat file, cmd window is getting open and I am getting the ERROR : could not find or load main class.

And I am using selenium-server-standalone-3.5.3.jar and testng-6.0.jar.

Any help will be appreciated.

While executing through Batch file,I'm getting the following error:
java.lang.NoSuchMethodError: org.openqa.selenium.support.ui.WebDriverWait.until(Ljava/util/function/Function;)Ljava/lang/Object;

But it is executed successfully when ran through Eclipse IDE.

Can anyone please tell, what could be the reason for this issue?

Here is my batch file content,

set projectLocation=D:\TestNGWorkspace_V1\DelmiaAutomation_TestNG
cd\
cd %projectLocation%
set classpath=%projectLocation%\lib\*;%projectLocation%\bin
java org.testng.TestNG testng.xml
pause

after running the batch file getting "could not find or load main class" this error , please help me on this.

do we need to install java and eclipse with TestNG on system to run batch file?

Add new comment

Image CAPTCHA