21

I am currently working on the selenium web driver and testng on Eclipse IDE. I usually run the test from the XML file that i have created which runs all the methods in the eclipse.

Now i want to create a simple executable jar which should do the same i.e its running point should be the XML file so that each test is executed .

I am trying hard on this. Please give me some advice on how to go further with it

Dropout
  • 13,653
  • 10
  • 56
  • 109
mannu singh
  • 879
  • 3
  • 13
  • 22

4 Answers4

39

Here is the better way to do it. But thanks anyways sanbhat.

You can just create a main method which will have list of all test classes to be executed as follows:

public static void main(String[] args) {
TestListenerAdapter tla = new TestListenerAdapter();
TestNG testng = new TestNG();
testng.setTestClasses(new Class[] { test_start.class });
testng.addListener(tla);
testng.run();
}

Here is the reference URL from the official testng website.

http://testng.org/doc/documentation-main.html#running-testng-programmatically

Cheers!

mannu singh
  • 879
  • 3
  • 13
  • 22
  • 5
    To use an xml configuration instead of directly choosing classes to run, see this answer: http://stackoverflow.com/a/23287848/116810 – Kimball Robinson Jun 26 '15 at 13:26
  • I did this to start testNG when Javafx button is clicked. It works in eclipse but doesnt work when application is deployed as Jar file. – Bits Please Sep 28 '18 at 13:48
4

You can create a main method like below and can execute it

public static void main(String[] args) {
    TestListenerAdapter tla = new TestListenerAdapter();
    TestNG testng = new TestNG();
    List<String> suites = Lists.newArrayList();
    suites.add("c:/tests/testng1.xml");//path to xml..
    suites.add("c:/tests/testng2.xml");
    testng.setTestSuites(suites);
    testng.run();
}
smali
  • 4,687
  • 7
  • 38
  • 60
3

Use Eclipse Export Wizard. While exporting, select "Create Runnable Jar" and select the class which is entry point (which contains main method) of your project.

This class will have main method which will read XML and execute the testcases

sanbhat
  • 17,522
  • 6
  • 48
  • 64
  • Getting unable to find testng.xml when run from command prompt. XML is in my runnable jar. Any help?? – saravana Apr 18 '17 at 12:23
-4

Creating a jar File in Command Prompt

Start Command Prompt.
Navigate to the folder that holds your class files:

C:\>cd \lalit

Set path to include JDK’s bin. For example:

C:\lalit> path c:\Program Files\Java\jdk1.7.0_25\bin;%path%

Compile your class(es):

C:\lalit> javac *.java

Create a manifest file and your jar file:

C:\lalit> echo Main-Class: hitech >manifest.txt
C:\lalit> jar cvfm hitech.jar manifest.txt *.class

or

C:\lalit> jar cvfe hitech.jar hitech *.class

Test your jar:

C:\lalit> hitech.jar

or

C:\lalit> java -jar hitech.jar
Radim Köhler
  • 122,561
  • 47
  • 239
  • 335