Monday, October 24

Running Selenium TestNG test from command line

When I tried to run TestNG command from command line, I get following error. This is mainly because java cannot find class path and jar file of TestNG
C:\Selenium\Selenium_tests\DemoProject java org.testng.TestNg
Exception in thread "main" java.lang.NoClassDefFoundError: org/testng/TestNg
Caused by: java.lang.ClassNotFoundException: org.testng.TestNg
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: org.testng.TestNg.  Program will exit.
C:\Selenium\Selenium_tests\DemoProject 
So below are the steps to get it working



1. Navigate to directory of your project (if you are using Eclipse IDE).Create and set a path variable named “ProjectPath” by entering following command. Do echo command to check what is the value that we just set is right



C:\Selenium\Selenium_tests\DemoProject set ProjectPath=C:\Selenium\Seleni
um_tests\DemoProject 
C:\Selenium\Selenium_tests\DemoProject echo %ProjectPath%
"C:\Selenium\Selenium_tests\DemoProject"
2.  Now we after setting this project path, we need to create classpath that points to selenium/Testng class files and jar files along with testng jar file. My project setup is  like below and I keep jar files in folder called lib and eclipse creates complied classes in directory called bin automatically when you execute your test cases from eclipse
Below is command to set the classpath for jars in lib folder and binary classes/folders in bin folder and do a echo to check the path that is been set


C:\Selenium\Selenium_tests\DemoProject set classpath=%ProjectPath%
\bin;%ProjectPath%\Lib\*
C:\Selenium\Selenium_tests\DemoProject echo %classpath%
C:\Selenium\Selenium_tests\DemoProject\bin;C:\Selenium\Selenium_tes
ts\DemoProject\Lib\*
3. Now execute the java command for TestNG as shown below



C:\Selenium\Selenium_tests\DemoProject java org.testng.TestNG 
%ProjectPath%\IEandFirefoxTests.xml

[TestNG] Running:
  C:\Selenium\Selenium_tests\DemoProject\IEandFirefoxTests.xml
===============================================
IEandFirefox
Total tests run: 6, Failures: 0, Skips: 6
Configuration Failures: 4, Skips: 12
===============================================

---------------------------------------------

Related Posts Plugin for WordPress, Blogger...