Running TestNG Suite programitically

Hi everyone,

We are working on an automation project and are using Selenium and TestNG.

At the moment we are using Jenkins to run our tests.

However as a personal project of mine I want to create a alternative way of running our tests.

I have created a seperate JavaFX project to create a GUI, with this GUI it enables you to chose the XML using Java File Chooser, when you choose the TestNG XML, it executes.

However when it executes it cannot find it test classes that are specified within it.

Can anyone shed some light on this? Or know of ways you can get a TestNG XML Suite to run from Java code, for example you give the code the path to the XML and then the code will run the XML.

Thanks in advance.

Add Comment
  • 2 Answer(s)

    Hey Collin,

    What you are looking for is the TestNG Class. This is the class which can be invoked to run the tests programatically.

    TestNG  runner = new TestNG();
    List<String> tempSuite = new ArrayList<String>();

    tempSuite  is a list of paths to your suite.xml.

    TestNG class also contains lot of other methods that can help you control your test run. One such important set of methods are
    TestNG.addListener which will help you add your listners.


    Professor Answered on July 9, 2016.

    Hi Virender Singh,

    I am using code quite similar to yours at the moment.

    In the GUI the user inputs the path to the XML and then once the path is given, my code is executed.

    I will have a look at your code and my own to see if there is anything I am missing, or anything I need to change.

    I want this to be dynamic, that the path to any testNG XML suite can be giving and it can be executed.

    on July 14, 2016.

    Here is my code

    String xmlFileName = selectedFile.getAbsolutePath();
    TestNG testng = new TestNG();
    List<String> testFilesList = new ArrayList<String>();

    xmlFileName contains the path to the XML.

    Here is the error code when I try to run a suite:

    Exception in thread “JavaFX Application Thread” org.testng.TestNGException:
    Cannot find class in classpath: com.emc.qe.u360.tests.LogoutTests

    on July 14, 2016.
    Add Comment

    I can suggest: “Compatibility Testing Using BrowserStack & Selenium – TestNG”

    Train Answered on October 13, 2016.
    Add Comment
  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.