• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • Programming Languages
    • Java Tutorials
    • Python Tutorials
    • JavaScript Tutorials
  • Automation Tools and Different Tools
    • Web Automation
      • Selenium with Java
        • Selenium Basic
        • Selenium Advance
        • Selenium Realtime
        • Framework
        • Selenium Interview
        • Selenium Videos
        • Selenium with Docker
      • Selenium with Python
      • WebdriverIO
        • Selenium Webdriver C# Tutorial
      • Cypress
      • Playwright
    • TestNG
    • Cucumber
    • Mobile Automation
      • Appium
    • API Testing
      • Postman
      • Rest Assured
      • SOAPUI
    • testRigor
    • Katalon
    • TestProject
    • Serenity BDD
    • Gradle- Build Tool
    • RPA-UiPath
    • Protractor
    • Windows Automation
  • Automation For Manual Testers
  • Services
  • Online Training
  • Contact us
  • About me
  • Follow us
    • Linkedin
    • Facebook Group
    • Facebook Page
    • Instagram

Automation

Selenium WebDriver tutorial Step by Step

You are here: Home / TestNG Tutorial / How to execute testng.xml files using Java Program

How to execute testng.xml files using Java Program

June 16, 2015 by Mukesh Otwani 29 Comments

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

Filed Under: TestNG Tutorial Tagged With: testng.xml execution

Reader Interactions

Comments

  1. pankhuri says

    July 31, 2023 at 9:17 AM

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

    Reply
  2. paras23 says

    February 2, 2023 at 11:25 AM

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

    Reply
    • Mukesh Otwani says

      July 24, 2023 at 1:18 AM

      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.

      Reply
  3. Saif Ali Manihar says

    November 1, 2020 at 9:28 AM

    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.

    Reply
    • Mukesh Otwani says

      November 2, 2020 at 12:12 PM

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

      Reply
  4. narms says

    May 20, 2020 at 5:58 PM

    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

    Reply
    • Mukesh Otwani says

      May 21, 2020 at 8:26 AM

      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.

      Reply
  5. Gagandeep Singh says

    May 8, 2020 at 5:02 AM

    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?

    Reply
    • Mukesh Otwani says

      May 8, 2020 at 11:08 AM

      Hi Gagandeep,

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

      Reply
  6. Harsh says

    July 10, 2019 at 9:31 PM

    Hi sir, thanks for the article,

    I am getting the following exception on running main method

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

    Reply
    • Mukesh Otwani says

      July 11, 2019 at 10:28 AM

      Hi Harsh,

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

      Reply
  7. Pankaj says

    June 25, 2019 at 8:29 PM

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

    Reply
    • Mukesh Otwani says

      June 25, 2019 at 9:53 PM

      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.

      Reply
      • Jubayer Shamshed says

        April 12, 2021 at 12:02 PM

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

        Reply
        • Mukesh Otwani says

          April 14, 2021 at 9:06 AM

          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

          Reply
  8. Svitlana says

    February 19, 2017 at 9:50 AM

    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?

    Reply
    • Mukesh Otwani says

      February 19, 2017 at 2:34 PM

      Hi Svitlana,

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

      Reply
  9. Ram Narayan says

    October 14, 2016 at 3:20 PM

    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 !

    Reply
    • Mukesh Otwani says

      October 14, 2016 at 6:24 PM

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

      Reply
  10. laksha says

    September 24, 2016 at 8:29 AM

    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 .

    Reply
    • Mukesh Otwani says

      September 29, 2016 at 2:19 PM

      Sure will do for sure.

      Reply
  11. manohar kamarushi says

    September 23, 2016 at 6:59 AM

    I thank you so much

    Reply
    • Mukesh Otwani says

      September 23, 2016 at 11:29 PM

      Welcome Manohar

      Reply
  12. Nikhil says

    September 19, 2016 at 4:46 PM

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

    Reply
    • Mukesh Otwani says

      September 29, 2016 at 2:36 PM

      Nice Nikhil .

      Reply
  13. Azgar khan Abdul says

    April 12, 2016 at 11:15 AM

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

    Reply
    • Mukesh Otwani says

      April 13, 2016 at 10:52 PM

      Hi Azgar,

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

      Reply
  14. Jananga says

    February 3, 2016 at 4:33 PM

    good one 🙂

    Reply
    • Mukesh Otwani says

      March 3, 2016 at 4:19 PM

      Hi Jananga,

      Thank you. Keep visiting other section as well.

      Reply

Leave a Reply Cancel 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.

Primary Sidebar

Free Selenium Videos

https://www.youtube.com/watch?v=w_iPCT1ETO4

Search topic

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?

Stay connected via Facebook

Stay connected via Facebook

Archives

Footer

Categories

Recent Post

  • API Testing Using Postman And RestAssured
  • Disable Personalise Your Web Experience Microsoft Edge Prompt In Selenium
  • How To Fix Error: No tests found In Playwright
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Best and easy way to Group test cases in selenium

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?