Execute TestNg Project using batch file

Below description would help you to execute JAVA + TestNg Project using batch file.

Here, we have one sample class called SampleTest.java in JAVA Project which would open Google in chrome browser and verify the page title and then closes the browser at the end.

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class SampleTest {
	WebDriver driver;

	@BeforeMethod
	public void setup() {
		String baseUrl = "http://www.google.com";
		// Initialize driver object
		System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "/lib/chromedriver.exe");
		driver = new ChromeDriver();
		// Launch Application on browser
		driver.get(baseUrl);
	}

	@Test
	public void test01() {
		String expectedPageTitle = "Google";
		String actualPageTitle = "";

		// Fetch page title and store it in a variable
		actualPageTitle = driver.getTitle();
		// Print title
		System.out.println(actualPageTitle);

		if (actualPageTitle.equals(expectedPageTitle)) {
			System.out.println("Test case passed");
		} else {
			System.out.println("Test case Failed");
		}

	}

	@AfterMethod
	public void tearDonw() {
		// close browser
		driver.close();
	}
}

Create TestNg file in Project
We will invoke below TestNg.xml file using batch file (.bat)

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Selenium Tests" verbose="3" parallel="tests"
	thread-count="1">


	<test name="Test1">
		<classes>
			<class name="SampleTest" />
		</classes>
	</test>
</suite>

Create LIB folder in project and put all the external Jars into it
As we new below 2 external Jars for this sample Project :
a)Selenium standalone Jar
b)TestNg Jar

The Sample Project would look like :

TestNg-Batch

Create batch(.bat) file

set projectPath=C:\Users\workspace\TestProject
cd\
cd %projectPath%
set classpath=%projectPath%\bin;%projectPath%\lib\*;
java org.testng.TestNG testng.xml

As you notice in above commands, you have to set path of project bin folder and external libraries.

Save above commands in .bat file and now you can simply execute the project by just double click on batch file.

If you really like the information provided above, please don’t forget to like us on Facebook, you can also leave the comment.

Leave a Reply

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