Run selenium testng tests from executable jar



How to run Selenium testng tests from Executable jar file.?


Have you ever thought of to run some set of tests and convert those test to executable jar and share that jar file to team members who are non -technical / business members so that with simple double clicks those test would run.? If yes, here is how to achieve the it.

pre-requisite:
 > Have a directory with all required libraries, say directory name as 'lib', then put the following jar files, 
a) chromedriver
b) jcommander-1.78.jar (latest version jar)
c) selenium-server-standalone
d) testng-6.8.8.jar (latest version compatible jar) 

1) A simple selenium testng test implemented in eclipse. (make sure test is running)
a) Create a java file with main method, for ex:
public class TestRunner {
static TestNG testng;
public static void main(String[] args) {
testng = new TestNG();
testng.setTestClasses(new Class[] {fayaz.google.searchtest.GoogleSearch.class});
testng.run();
}
}
b) From this java file, run the tests as java application to make sure tests are running
2) Right click on the project and click 'Export...'
3) Under 'Java' select 'Runnable JAR file'
4) Click Next, select the Launh file as, 'TestRunner' (java file name) and export to desktop
5) Open command prompt from the project directory ( NOT FROM desktop)
6) Now, double click the executable jar by providing the absolute path, with java -jar command, 
         for ex: D:\fayaz\workspace\TestRunnerDemo\> java -jar C:\Users\Fayaz\Desktop\testrunner.jar

Note: if the jar is running from the desktop directory then test doesn't run. 


=============================
Now, to make run the jar from any directory we need to have the chrome driver accessible.
To make chrome driver accessible, we need to set the driver path in environment variable, after which we need to restart the eclipse..  

Then set the chrome driver path as below..

String evn_var_chrome_driver_path = System.getenv("chrome_driver_path");
System.setProperty("webdriver.chrome.driver", evn_var_chrome_driver_path + "\\chromedriver");

Boommm.. now convert the project to executable java jar file and run it from any directory