There are so many articles about running TestNG from Command Prompt but very few explains it in an easy manner.I am trying to put some efforts to make the same thing more simpler to beginners.
When it comes to running a TestNG file from Command Prompt we may have two situations
- Project containing both Selenium jar and TestNG jar files
- Project containing only TestNG jar files.
First Step is to download the TestNG Jar file from the link below if you havent downloaded it.
Download the Zip file, Extract it and save it to any fixed Location and set the same path of the Testng jar file in CLASSPATH of environment variables.
Lets first run the Project containing only TestNG jar file
Open the Eclipse and create one new project 'FirstProject' and add the same TestNG jar file to the Project whose path is set in the CLASSPATH.
Right-Click on the Project, Create one new file name and name the file as 'testng.xml'
Add all the neccessary information in the testng.xml file such that if we right click on the testng.xml and run it as testng then we are able to execute the file successfully.
Now Open the Command Prompt and go to the project folder created above. It should contain the complete path where your Eclipse stores the Project.
Verify that we are under the same project folder which we are trying to execute from the command prompt and most important it contains 'testng.xml'.
Now the first step is to set the ProjectPath in the command Prompt in below manner
set ProjectPath=D:\TestNG_Workspace\FirstProject
(Note: The Project Path will differ in your case)
Verify the Path by typing below command
echo %ProjectPath%
Now the second Step is to set the classpath from the command prompt.
set classpath=%ProjectPath%\bin;C:\Program Files\testng-6.8\testng-6.8.jar
(Note: The testng jar location path may differ in your case)
Verify the Path by typing below command
echo %classpath%
Now the final Step is the run the testng.xml file in below manner
java org.testng.TestNG %ProjectPath%\testng.xml
(Note: The name of the testng.xml may differ in your case)
And yes you're now successfully runnnig testng script from command prompt.
Now Run the Project containing both Selenium Jar and TestNG jar file.
First Step is to locate the path of the Selenium jar files, Its Simply the path where from you're adding the Selenium jar files in your Eclipse project. Set the same path of Selenium jar files in CLASSPATH of environment variables.
The Process of running the project will remain same except ClassPath.
set classpath=%ProjectPath%\bin;C:\Program Files\testng-6.8\testng-6.8.jar;S:\selenium-java-2.44.0;S:\selenium-java-2.44.0\*
(Note: The testng jar and Selenium jar file path may differ in your case)
Verfiy the Path by typing below command
echo %classpath%
Run the testng.xml file in below manner
java org.testng.TestNG %ProjectPath%\testng.xml
I hope you're able to run the xml file successfully.