TestNG Tutorial

How to execute testng.xml files using Java Program

execute xml using java

In this post, we are going to discuss how to execute testng.xml files using Java Program.

As we all know in Selenium webdriver can execute multiple test cases using testng.xml file and we can handle all test case execution via testng.xml file.

If you are new in testng and not sure about testng.xml file then please check the below youtube video.

How to create testng.xml file in Eclipse 

 

What is need of this in Selenium?

Let me explain this with a help of an example.

Consider you have 10 test cases in Selenium and  you have created a testng.xml to execute all test cases.

Now you create another 20 test cases which belong to the same project but they belong to different module so again we created testng1.xml for the same.

If the same process goes on then at last you must be having couple of xml files which will  be having all the testcases.

Now the real problem is if you want to execute all xml in one shot then you can not do because Eclipse will allow to execute only one xml.

In this case we can take help of an TestNG class which allow us to execute multiple xml in one shot.

In below example I am having one testng.xml file which I will execute using Java Program.

 

How to implement in Selenium Webdriver

I have covered the therory part in program itself.Lets have a look.

import java.util.ArrayList;
import java.util.List;

import org.testng.TestNG;

public class RunTestNG {

public static void main(String[] args) {

// Create object of TestNG Class
TestNG runner=new TestNG();

// Create a list of String 
List<String> suitefiles=new ArrayList<String>();

// Add xml file which you have to execute
suitefiles.add("C:\\Users\\Documents\\Blog6March\\dummy16june\\testng.xml");

// now set xml file for execution
runner.setTestSuites(suitefiles);

// finally execute the runner using run method
runner.run();
}

}

I above program I have taken one xml file only but if you have mutiple files then repeat the same procedure and add files.

Hope you will implement the same in your project.

For More updates Learn Automation page

For any query join Selenium group- Selenium Group

author-avatar

About Mukesh Otwani

I am Mukesh Otwani working professional in a beautiful city Bangalore India. I completed by BE from RGPV university Bhopal. I have passion towards automation testing since couple of years I started with Selenium then I got chance to work with other tools like Maven, Ant, Git, GitHub, Jenkins, Sikuli, Selenium Builder etc.

29 thoughts on “How to execute testng.xml files using Java Program

  1. pankhuri says:

    Hey, I am getting below error in Intellij:
    Exception in thread “main” org.testng.TestNGException:
    Cannot find class in classpath:

  2. paras23 says:

    What is the benefit of running testng this way? We just run as a “TestNG” test?

    1. Hi Paras, when you will run your test from Jenkins or any other CI Server then you will be running pom.xml file. Instead of having multiple xml and maintaining in pom could be tidius. This is one of the way to run your all xml at once.

  3. Saif Ali Manihar says:

    Hi Mukesh,

    I follow your blogs and follow your youtube channel also. You are doing fabulous job.

    I need a help. I have created runnable jar file of my maven testng project. In testng.xml, there are 3 classes to test. But when I am running jar file, it is only running first class and generating the output. But it is not running the rest of the classes. When I am running the same code in eclipse, everything worked fine. All the classes got run.

    Any suggestion or idea.

    1. Hi Saif, can you show me the main file which gets executed when you execute jar file.

  4. narms says:

    HI,

    How do I define a class in Testng which has only main method and no other methods in it?

    And how do I specify annotation for this java class if I have only main method? Any suggestions pls

    1. Hi Narms,

      Why do you want to use main method when TestNg itself can take care of everything? There is no need of main method over here.

  5. Gagandeep Singh says:

    This only works if you change mention the absolute path but thats not what you would want to do in actual development environment where you have to execute code on bamboo or other machines. If you change the path to root then it breaks. Any suggestions?

    1. Hi Gagandeep,

      In that case, you should use relative file path with respect to your project root folder

  6. Harsh says:

    Hi sir, thanks for the article,

    I am getting the following exception on running main method

    The processing instruction target matching “[xX][mM][lL]”

    1. Hi Harsh,

      There is no need of have main method if you are using TestNG

  7. Pankaj says:

    I am getting below error:
    Exception in thread “main” org.testng.TestNGException:
    Cannot find class in classpath: org.com.apitestRestAssured.RestAssured.RentalOnlineReqTest

    1. Hi Pankaj,

      This is usual issue with TestNG Eclipse plugin. It would good if you clean your project and restart your eclipse. If it doesn’t work after this then uninstall and reinstall TestNG plugin.

      1. Jubayer Shamshed says:

        I uninstalled and reinstall testNG, clean the project and restart eclipse several times, But No luck.

        1. Hi Jubayer,
          Try with installing previous version of TestNG plugin for Eclipse. Steps Uninstall existing TestNG plugin -> Use this link for installing plugin and proceed

  8. Svitlana says:

    Hi,
    I hava a problem with my testng.xml file – in “Run as” I have only the option “Run Configuration” ….I have written the class name, all class methods have the annotation @Test. Webdriver driver I created as the class variable, the methods I invoke via my class variable in the “public static void main”. Could you please tell me what I’m doing wrong?

    1. Hi Svitlana,

      If you are using testng annotations then you don’t need to mention main method over there.

  9. Ram Narayan says:

    Hi Sir,
    Instead of writing a java file for the testng.xml files we have, can’t we just create a batch file and run the testng.xml files at one go. which is nothing but running from command prompt.

    Thanks for this article !

    1. Yes Ram that also we can do. I have also covered the same in another article.

  10. laksha says:

    Hi Mukesh,
    Thanks for your help in solving problems..I will be very thankful to you if u give me an example for the concept how to include and exclude test cases in xml file in testNG for example if you want only 50 test cases out of 100 .

    1. Sure will do for sure.

  11. manohar kamarushi says:

    I thank you so much

  12. Nikhil says:

    Mukesh its so nice. I tried and it worked well. Super again.

  13. Azgar khan Abdul says:

    Mukesh you are simply brilliant..they way you explanation is very nice.

    1. Hi Azgar,

      Thanks Mate 🙂 let me know if any help from my side.

  14. Jananga says:

    good one 🙂

    1. Hi Jananga,

      Thank you. Keep visiting other section as well.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.