javaeclipsemavenseleniumtestng

Running my testNG project from a jar using Maven


I'm using testNG and Selenium to run multiple functionals test of my application.

To have more stable test I decided to include the Firefox binaries inside the project folder and call this specific binary and not the default browser.

The problem I encounter now is that if someone wants to use my test, they have to import the project in their eclipse, relink the java 8 library, install TestNG on their eclipse, add the selenium jar to the project and finally create the testNG configuration and finally run it with the chosen xml. I wanted to know if this possible to simple transform project into a jar file that I will call by command line, something like:

java SeleniumTestNGTest myXmlFile.xml

Would this way avoid the different setting wrote above? Can we call a testNG test from jar using java? Would Firefox be runnable from the jar file?

EDIT 1: A good suggestion in the comment is to migrate my source to a maven project. But still the same questions remains. Would maven be able to manage the setting that need my test project? (which are the selenium jar, the java 8 library, the testNG jar and running the Firefox browser)


Solution

  • Finally i did not use maven, i just created a main in my project. The main will call the testNG suite that is specified in the XML file passed in the argument of the programm. This method will allow me to execute the test from a jar without caring about extern library (the library testNG and selenium are already inside).

    Here the code of the main if it can help anyone :

     public class Runner {
    
    public static void main(String[] args) throws IOException {
    
        if(args.length != 2) {
            System.out.println("Usage : <xmlFile> <OutputFileName>");
            System.exit(-1);
        }
    
        Path path = Paths.get(args[0]);//Path to the xml
    
        new PrintWriter(args[1]).close();
    
        Charset charset = StandardCharsets.UTF_8;
    
        String content = new String(Files.readAllBytes(path), charset);
        content = content.replaceAll("theOutputFileNamePattern", args[1]);
        Files.write(path, content.getBytes(charset));
    
        //We create a testNG object to run the test specified in the xml file
        TestListenerAdapter tla = new TestListenerAdapter();
        TestNG testng = new TestNG();
    
        List<String> suites = Lists.newArrayList();
        suites.add(args[0]);
    
        testng.setTestSuites(suites);
    
        testng.run();
    
    }
    
    }
    

    Then you just have to go in eclipse, then clic on File -> Export -> Runnable Jar file -> Select the configuration that you want to export -> Finish.