1

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 want to use my test, he has to import the project in his eclipse, relink the java 8 library, install TestNG on his eclipse, add the selenium jar to the project and finaly create the testNG configuration and finaly run it with the choosen 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 ? (wich are the selenium jar, the java 8 library, the testNG jar and running the firefox browser)

Omegaspard
  • 1,828
  • 2
  • 24
  • 52
  • This is why maven is popular. Consider migrating your source to a maven project. – Thorbjørn Ravn Andersen Aug 03 '16 at 14:04
  • hello thorbjorn from overwatch. How could maven could help me more ? I know it could manage the dependencies of project and i will look forward to it on the internet but if i could have a little more information on how i could use maven for my problem from you, it would be great. – Omegaspard Aug 03 '16 at 14:09
  • @BobReynolds Maven has a manual, you can't just get it going with a paragraph of information in a stackoverflow answer. But since your question is not about using Maven, currently creating any kind of answer using Maven would make the answer off-topic. – Gimby Aug 03 '16 at 14:31
  • 1
    Maven allows you to specify the individual steps needed to do your build in the maven `pom.xml` file. This includes using the appropriate JVM, pulling in the jars you need (as maven dependencies), and invoking selenium on your configuration file which you provide with your sources. You may have to learn and/or relearn a lot - Maven has _opinions_ - but for me just the ability to develop without being tied to a specific IDE is worth a lot just on its own. – Thorbjørn Ravn Andersen Aug 03 '16 at 15:59
  • 1
    i think Bob is wanting to know, is it possible to package up the tests to a jar file, so it can be distributable and executable without dependencies. See this: http://stackoverflow.com/questions/16393223/how-to-create-a-executable-jar-file-for-testng-and-the-runnnig-point-should-be-t. – Greg Aug 08 '16 at 13:51

1 Answers1

1

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.

Omegaspard
  • 1,828
  • 2
  • 24
  • 52