Skip to content Skip to navigation
Check Our New Demo Website!

TestNG XML example to execute Multiple Classes

In testng.xml file we can specify multiple name (s) which needs to be executed.

In a project there may be many classes, but we want to execute only the selected classes.

We can pass class names of multiple packages also. If say suppose, we want to execute two classes in one package and other class from some other package.

The below is the example testng.xml which will execute the class names that are specified.

Example:

<?xml version="1.0" encoding="UTF-8"?>
<suite name="example suite 1" verbose="1" >
  <test name="Regression suite 1" >
    <classes>
      <class name="com.first.example.demoOne"/>
      <class name="com.first.example.demoTwo"/>
      <class name="com.second.example.demoThree"/>
    </classes>
 </test>
</suite>

We need to specify the class names along with packages in between the classes tags.

In the above xml, we have specified class name as “com.first.example.demoOne” and “com.first.example.demoOne” which are in “com.first.example” package. And class name demoThree is in package “com.second.example.”

All the classes specified in the xml will get executes which have TestNG annotations.

Below are the two example classes under “com.first.example” package which is executed.

Package: “com.first.example”
Classname: demoOne

package com.first.example;
import org.testng.annotations.Test;
public class demoOne {
	@Test
	public void firstTestCase()
	{
		System.out.println("im in first test case from demoOne Class");
	}
	
	@Test
	public void secondTestCase()
	{
		System.out.println("im in second test case from demoOne Class");
	}
}

Classname: demoTwo

package com.first.example;
import org.testng.annotations.Test;
public class demoTwo {
	@Test
	public void firstTestCase()
	{
		System.out.println("im in first test case from demoTwo Class");
	}
	
	@Test
	public void secondTestCase()
	{
		System.out.println("im in second test case from demoTwo Class");
	}
}

Package: “com.second.example”
Classname: demoThree

package com.second.example;
import org.testng.annotations.Test;
public class demoThree {
	@Test
	public void firstTestCase()
	{
		System.out.println("im in first test case from demoThree Class");
	}
	@Test
	public void secondTestCase()
	{
		System.out.println("im in second test case from demoThree Class");
	}
}

We need to run the testng.xml file. (Right click on testng.xml and select Run as ‘TestNG Suite”)
The below is the output for the above example code.

TestNG with multiple classes

Test Frameworks: 

Comments

Hi

when i executed below xml file then two browser windows are opened at a time. can you please suggest me on this

Note: in each class i have initiated the Firefox browser
WebDriver driver=new FirefoxDriver()

<?xml version="1.0" encoding="UTF-8"?>
<suite name="example suite 1" verbose="1" >
<test name="Regression suite 1" >
<classes>
<class name="com.first.example.demoOne"/>
<class name="com.first.example.demoTwo"/>
</classes>
</test>
</suite>

When you are instantiating the browser for each class, you should also class the browser after the class.

So that next class is opened freshly in new browser. Write a 'driver.close()' in After class.

in test suite when i execute its open multiple browser window and then execute one by one (suppose in test suite have 10 class its open 10 window)...please suggest me on this

Actually though i am instantiating the browser for each class, and closing after the class.. It opens several windows at once.. in the sense if testng.xml has 10 classes, it opens 10 browser windows at once! how to avoid this?

Even I faced similar issue
Solution:Please Write test cases by following annotations. Browser launching operations must be done in a method with annotation @BeforeClass and closing browser quit/close must be done in a method followed by @AfterClass Annotations
This Will solve your problem

multiple instance of browser is getting opened if i am running testng suite having multiple classes ,please provide solution for the same

Hi,
Me too facing this same issue as well. At finally I got a solution to solve the problem "Multiple Instance of browser is getting opened when we run the Test from XML"
So Wat we have to do know is - In the Base class you will be created a instance by using the below command
Public WebDriver driver = new FirefoxDriver(); Now you need to include a Static Keyword. This solves that multiple browser instance problem. Try this public static WebDriver driver = new FirefoxDriver();

for eg, In 100 testng class i have to run only required 40 testcases hw can i select tat cases..?

You can create multiple test
for eg
<?xml version="1.0" encoding="UTF-8"?>
<suite name="example suite 1" verbose="1" >
<test name="Regression suite 1" >
<classes>
<class name="com.first.example.demoTwo"/>
</classes>
</test>
<test name="Regression suite 2" >
<classes>
<class name="com.first.example.demoOne"/>
</classes>
</test>
</suite>

Here one window will open at a time

Second - if you want run 40 test out of 100 test cases , you can group those test and add here

Hi,

I want to try execute two classes at a time but when i declared as "public static WebDriver cd1" in one class
and i wrote some code and again i am trying to try code in another class while am write code with object(cd1) it is asking to declare object but i am already object declared as public also getting error why.

Pages

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.