Skip to content Skip to navigation
Check Our New Demo Website!

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.

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 tried to run the bat file, following error is coming on the command prompt.
"could not find or load main class"

Also, i do not see lib folder in my project.

Please help me here.

.bat file is running and its only reading my excel sheet first which is mentioned in my @beforemethod but after this its not opening my browser .Browser URL is mention in excel sheet in @dataprovider.I tried a lot but not able to identify the exact issue.could You help to solve the issue please.

my project contain property file ,when i am trying to execute my project testNg.xml it does not show any error but i am trying to execute with bat file it show error like " file not found exception"

Hi, iam running Testng test cases through cmd using batch file Can any one please suggest me how to rerun the failed test cases automatically after first run.

How to create bat file if there is an maven project? Because in maven we don't have library files.

What is "org.testng.TestNG" in the line#4(java org.testng.TestNG %projectLocation%\testng.xml) of the code?

java org.testng.TestNG %projectLocation%\testng.xml ->
Bat file execute the series of steps sequentially so it search the library file always before executing the required file. it will run testng.xml file in the command with java runtime and TestNG support. The same file can be run on different machine where this project is deployed. no change is required in bat file.

Highly Informative tutorial with apt examples and adequate coverage. Easy to learn for beginners.
Thanks

i am run testcase using .bat file. then all testcase skip.

in intellij bin folder will not get created for java project,could you please let me know how to create bat file for testng.xml

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.