-
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.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.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.
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.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.