How To Create Batch File For Maven Project
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 :
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 :
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.
How To Create Batch File For Maven Project
Source: https://www.seleniumeasy.com/testng-tutorials/how-to-run-testng-xml-via-batch-file-example
Posted by: manninosumanducke.blogspot.com

0 Response to "How To Create Batch File For Maven Project"
Post a Comment