Site Loader
Auckland, New Zealand
The main advantage of using TestNG framework in Selenium is its ease of running multiple tests from multiple classes using just one configuration (We can also have many configurations, which depends upon how we design our test). Testng.xml is an XML file that describes the runtime definition of a test suite. It describes complex test definition while still remain easy to edit. Using Testng.xml file we can run test available in one or more class file and make them as a single group, hence making test more meaningful, we call them as scenario based testing. Can I run test without using testng.xml file? Well answer is yes, you can run. But once your test become larger and requires configuration of multiple test at one place, the best option is testng.xml. The testng.xml file will have following hierarchy
  • The root tag of this file is <suite>.
  • <suite> tag can contain one or more <test> tags.
  • <test> tag can contain one or more <classes> tags.
  • <classes> tag can contain one or more <method> tags.

For more information on Tags and other tags that you can use in your XML file, check out the details here

Your xml file with just one test in a one class file will look something like this. image Before running TestNG test, first you need to configure the TestNG with Selenium, check out the article here for step by step procedure. As you could see above, the Class name has Test.NewTest, which is nothing but the “Fully qualified” name of the Class. It means NewTest is the class file which is available under package Test. Now your  Eclipse IDE should look something like this. image I have added the testng.xml file in our project, so that we can use it for running test.

Try Running Test using Testng.xml

Just right click on the testng.xml file in package explorer of Eclipse and select Run As Testng Suite, the test will run and show the report as shown below. image   Our NewTest.java file will have following code.
package Test;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import org.testng.annotations.DataProvider;

public class NewTest {
  @Test
  public void OpenBrowser() {
	  System.out.println("In Open Browser ....");
	  WebDriver driver = new InternetExplorerDriver();
	  driver.get("www.executeautomation.com");
	  driver.close();
  }

}
Now the testng report looks like this. image  

TestNG to run Multiple Classes and Methods using testng.xml file

Now let’s try adding some more test method and classes. The testng.xml file looks something like this. As you could see there are lot of new tags in testng.xml like “parameter”, “method” etc. image   Lets see how our NewTest.java file code looks like.
package Test;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import org.testng.annotations.DataProvider;

public class NewTest {
	  @Test
	  @Parameters({"SiteAddress"})
	  public void OpenBrowser(String value) {
		  System.out.println("In Open Browser ...." + value);
		  WebDriver driver = new InternetExplorerDriver();
		  driver.get(value);
		  driver.close();
	  }

	  @Test(dataProvider="DataFactory")
	  @Parameters({"SearchData"})
	  public void EnterSearchData(String value) {
		  System.out.println("Came to Search Data in IE ..." + value);
	  }

	  @DataProvider(name = "DataFactory")
	  public Object[][] CreateData()
	  {
		return new Object[][]
				{
					new Object[] {"TestData1"},
					new Object[] {"TestData2"}
				};
	  }
}
Our NewTest2.java file looks something like this.
package Test;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.annotations.Test;
import org.testng.annotations.Parameters;

public class NewTest2 {
  @Test
  @Parameters({"SiteAddress1"})
  public void OpenNewBrowser(String value) {
	  System.out.println("In Open New Browser" + value);
	  WebDriver driver = new InternetExplorerDriver();
	  driver.get(value);
  }
}
Now lets run the test using testng.xml file as done before. Now TestNg will genereate two report for us, since we have run two test as shown in the XML file. The first report looks something like this. image Second report looks something like this image That’s it !!! Karthik KK

Post Author: Karthik kk

12 Replies to “How to configure TestNG with testng.xml”

  1. Hi Karthik,

    The Reports are not getting displayed on the website. I can only see images for the code.
    Please fix this.

    Thanks
    Pankaj Sharma

    1. Oops !!!

      Thanks for bringing to my notice, please check now, its updated !!!

      Thanks,
      Karthik KK

Leave a Reply to Umesh Cancel reply

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