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
===============================================

7 comments:

  1. Thanks Smita for visiting the blog. Glad to hear it helped to solve the problem

    ReplyDelete
  2. Hi,
    This was really useful. Thank you :)

    Cheers!
    Kalidas

    ReplyDelete
  3. hi i tried with all ur above steps but when executed getting the below error
    Error: Could not find or load main class org.testng.TestNG

    please help

    ReplyDelete
  4. Hi...........
    This is not working on my side ...after executing steps 3 It is showing an error "java.lang.NoClassDefFoundError"


    In My eclipse project "lib" folder was not there so i created it on my own including the
    1)selenium-server-standalone-2.25.0.jar
    2) testng.jar
    3) testng-sources.jar
    4) junit-4.8.2.jar
    5) guava-base-r03.jar
    6) guava-collections-r03.jar

    Is there something missing ? please help !

    ReplyDelete
  5. thankyou so much for the solution

    ReplyDelete

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

Related Posts Plugin for WordPress, Blogger...