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
pankhuri says
Hey, I am getting below error in Intellij:
Exception in thread “main” org.testng.TestNGException:
Cannot find class in classpath:
paras23 says
What is the benefit of running testng this way? We just run as a “TestNG” test?
Mukesh Otwani says
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.
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.
Mukesh Otwani says
Hi Saif, can you show me the main file which gets executed when you execute jar file.
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
Mukesh Otwani says
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.
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?
Mukesh Otwani says
Hi Gagandeep,
In that case, you should use relative file path with respect to your project root folder
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]”
Mukesh Otwani says
Hi Harsh,
There is no need of have main method if you are using TestNG
Pankaj says
I am getting below error:
Exception in thread “main” org.testng.TestNGException:
Cannot find class in classpath: org.com.apitestRestAssured.RestAssured.RentalOnlineReqTest
Mukesh Otwani says
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.
Jubayer Shamshed says
I uninstalled and reinstall testNG, clean the project and restart eclipse several times, But No luck.
Mukesh Otwani says
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
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?
Mukesh Otwani says
Hi Svitlana,
If you are using testng annotations then you don’t need to mention main method over there.
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 !
Mukesh Otwani says
Yes Ram that also we can do. I have also covered the same in another article.
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 .
Mukesh Otwani says
Sure will do for sure.
manohar kamarushi says
I thank you so much
Mukesh Otwani says
Welcome Manohar
Nikhil says
Mukesh its so nice. I tried and it worked well. Super again.
Mukesh Otwani says
Nice Nikhil .
Azgar khan Abdul says
Mukesh you are simply brilliant..they way you explanation is very nice.
Mukesh Otwani says
Hi Azgar,
Thanks Mate 🙂 let me know if any help from my side.
Jananga says
good one 🙂
Mukesh Otwani says
Hi Jananga,
Thank you. Keep visiting other section as well.