Execute TestNg Using Command Prompt: In our previous post, we have understood how we can execute our selenium TestNG class using any editor like Eclipse. But running the TestNG classes using IDE is not only one way, but we can also able to execute our TestNG Class by below methods like:
- Ant / Maven – Using any continuous integration tool.
- Command Line
- Batch File – run it by creating .bat (batch) file
Previously we have seen for running a TestNG class using IDE, we need to right-click on the testng.xml file and select run as TestNG which invoke the test class. Now lets us see how we can invoke the testng.xml file from the command line to run the test class in step by step:
- Open the IDE and Create a Java class inside the package.
- Create a directory inside the project folder and store all the jar files in that folder. In our case, we have created a folder named that “lib” and place all the jar files required to any Java Selenium TestNG Class.
- Write a Java Program and Save that.
- Convert that program to TesNG by right click on the class file and select testNG then click on Convert to TestNG. In this way, we can prepare the testng.xml. Because when we call the testng.xml file, it will execute the test methods which are present inside the test class.
- Open Command Prompt and go to the directory where your testng.xml file is present.
The syntax for TestNG using command prompt Like
Tell you about the syntax you have known a few more things, which will help you to understand how we prepare the syntax:
In the above Example:
- My Project Location is: C:\Users\Admin\Desktop\ SeleniumTutorial\ workspace\SoftwareTestingo
- Location Of All Jar Files: C:\Users\Admin\Desktop\ SeleniumTutorial\workspace\ SoftwareTestingo\lib\*
When we compile our Java program, By Default the .class file will be stored inside the project directory, in this case: C:\Users\Admin\Desktop\ SeleniumTutorial\workspace\ SoftwareTestingo\bin
So Based on the Above Information, we need to run below the piece of code in your command prompt.
Got to the project directory using Command Prompt like this:
CD C:\Users\Admin\Desktop\ SeleniumTutorial\workspace\SoftwareTestingo and Hit Enter
Then The Final Command Looks something like below:
java -cp C:\Users\Admin\Desktop\SeleniumTutorial\workspace\SoftwareTestingo\lib\*; C:\Users\Admin\Desktop\SeleniumTutorial\workspace\SoftwareTestingo\bin org.testng.TestNG testng.xml
In the above command, for ‘java -cp’ argument, we provide the classpath, i.e. the path to classes/libraries that our program requires to run. This way, you can run the TestNG class without using any IDE. Similarly, we can run our TestNG class with the help of Batch FIle.
How to Run TestNG.xml File Using Batch File
Let’s See how we can execute the same Testng.XML File with the help of a batch file. A batch file (.bat) used in the DOS and Windows operating system, which is an unformatted text that consists of a series of commands to be executed by the command-line interpreter.
Let us take the above Java File and TestNG.XMl File, and then trey to execute using a batch file.
How to Create a Batch File?
Note: Batch files are specific to windows, if you want to run the test on test platform which supports Java and TestNG, then we recommend to run your TestNG through Java instead of a batch file.
Steps to Create a Batch File
- Open notepad
- Mentioned the Project location like C:\Users\Admin\Desktop\SeleniumTutorial
- Paste the below code:
set projectLocation=C:\Users\Admin\Desktop\SeleniumTutorial cd %projectLocation% set classpath=%projectLocation%\bin;%projectLocation%\lib\* java org.testng.TestNG %projectLocation%\testng.xml pause
Or you can use the method below also:
cd C:\Users\Admin\Desktop\SeleniumTutorial java -cp C:\Users\Admin\Desktop\SeleniumTutorial\workspace\SoftwareTestingo\lib\*; C:\Users\Admin\Desktop\SeleniumTutorial\workspace\SoftwareTestingo\bin org.testng.TestNG testng.xml pause
- Save the file as ‘testNGBatchFile.bat’ in a place that you want to save.
We have seen in the end we have mentioned “Pause,” which will prevent the auto-closing of the console after the execution completed. To run the batch file, double click on that so your test methods will get executed.
Note: Don’t add spaces; otherwise, it will not work.
Leave a Reply