In previous post, we saw how can we run TestNG.xml file fromcommand prompt.
Refer post: How to Execute TestNG.xml from command prompt?
TestNG tests can be directly run from batch file (.bat file). Let us see below how this can be done.
Create sample java project.
Write some testing test in the project.
Create testing.xml file.
Create batch file (.bat file)
Sample test: (we will run this test from batch file)
package runTestNGxmlFromCMD;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
public class RunTestNGTest {
@Test
public void test() throws InterruptedException{
WebDriver driver = new FirefoxDriver();
driver.navigate().to("https://learnaboutsoftwaretesting.blogspot.in");
System.out.println("navigate to website");
Thread.sleep(3000); //sleep for 3 second so that you can observe that site is opened, before it closes quickly
driver.close();
}
}
Testng.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Default suite">
<test verbose="2" name="Default test">
<classes>
<class name="runTestNGxmlFromCMD.RunTestNGTest"/>
</classes>
</test> <!-- Default test -->
</suite> <!-- Default suite -->
Now, in this case, my project location is: C:UsersPrakashworkspaceRunTestNGXML
My project folder structure looks like –
Go to your project (or anywhere, where you want to keep your batch file)
Then create new file. Name the file with some name & with extension .bat
(if you are unable to create with .bat extension, use double quote, example – “testing.bat” while saving file.
Add below piece of lines in to the .bat file just created.
cd C:UsersPrakashworkspaceRunTestNGXML
java -cp C:UsersPrakashworkspaceRunTestNGXMLlib*;C:UsersPrakashworkspaceRunTestNGXMLbin org.testng.TestNG testng.xml
and it is done. Double click on your .bat file, your test running should start.
Hope this helps !!!!