I have researched lot, how to execute the TestNG xml from a Java class because I wanted to make my whole project in to a Runnable JAR file and execute it anywhere outside eclipse. This was already in the TestNG docs, but I couldn't understand this first time.
Advantages of this method:
Now Export your whole project to runnable JAR. Double click the JAR, will execute the xml as testNG. You may create a user interface to select XML so that it adds flexibility to select which test you need to run.
Advantages of this method:
- You can execute your tests outside eclipse, any machine provided JRE is installed on the machine. You don't need to place the libraries too, The JAR will have all these.
- Without a Selenium grid, this enables you to run the tests on any previous versions of browsers running on another machine.
- This can be executed by anyone without any knowledge of Eclipse, scripting etc.
How to do this:
Assuming you are running your test cases by executing testNG.xml as TestNG.
Steps:
Create a Java class with main method with below code.
public class MainTest {
public static void main(String[] args){
List<String> file = new ArrayList<String>();
file.add("D:/Selenium_workspace/Framework/src/Framework.xml");
TestNG testNG = new TestNG();
testNG.setTestSuites(file);
testNG.run();}
}
Now Export your whole project to runnable JAR. Double click the JAR, will execute the xml as testNG. You may create a user interface to select XML so that it adds flexibility to select which test you need to run.