How to take ScreenShot of failed test case using ITestResult interface in @AfterMethod annotation?

Taking Screen shot of all the test cases may create a memory issue, because we will be running the automation suite multiple times. Moreover, we can see the status of test cases (pass / failed / skipped) in the testing report. 
So, what is better is, to take the screen shot of only failed test cases.
To achieve this, we can use ITestResult interface in @AfterMethod annotation of testng.
ITestResult is an interface which keeps all information about test case which is being executed. It captures some information like Test case execution status, test case name etc
What we can do here is, in @AfterMethod annotation, get the status of the test using ITestResult Interface & check if it is failed. If failed then capture the screen shot. As simple as that.
Below Example Explains this.

package screenshotOfFailedTest;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;

public class CaptureScreenShotUsingITestResult {

WebDriver driver;

@Test
public void Testcase1(){

driver = new FirefoxDriver();
driver.navigate().to("https://learnaboutsoftwaretesting.blogspot.in");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();

driver.findElement(By.xpath("ajhjhj")).click(); //take incorrect xpath, so that test will fail becuase element does not exist.

}

@AfterMethod
public void TearDown(ITestResult result) throws IOException{

if (result.getStatus() == ITestResult.FAILURE ){
TakesScreenshot ts = (TakesScreenshot)driver;
File srcFile = ts.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(srcFile, new File("./ScreenShots/"+result.getName()+".jpg"));
//result.getname() method will give you current test case name.
//./ScreenShots/ tell you that, in your current directory, create folder ScreenShots. dot represents current directory
}
}

}

To know about how the same can be achieved using ITestListener, refer below post –
Hope This Helps!!!

 

How to take ScreenShot of failed test case using ITestListener Interface?

Taking Screen shot of all the test cases may create a memory issue, because we will be running the automation suite multiple times. Moreover, we can see the status of test cases (pass / failed / skipped) in the testing report. 
So, what is better is, to take the screen shot of only failed test cases.
To achieve this, we need to trigger an event (of capturing screen shot) whenever any test case fails. We can use testng listener for this purpose.
Let us see how can we use ITestListener
 
Basically, ITestListener is an interface, which we need to implement in our class.
Follow these steps to take screen shot of failed test cases only.
          1.      Create sample class & implement ITestListener
          2.      Write code to capture screen shot in OnTestFailure Method.
v        3.      Define listener either in testng.xml (if you want to apply this for all the classes which are going to execute from testng.xml) or in the class which contains testng tests( if you want to apply this to only some specific class)
          4.      That’s it, all done. Refer below code & screenshots.
Create a sample class and implements ITestListener in it.
When you import ITestListener in the class, you will see option to implement unimplemented methods.
Refer below screen shot.

Once clicked on “Add unimplemented methods”, all methods will be implemented, code will look like –

package screenshotOfFailedTest;

import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;

public class ListenerClass implements ITestListener {

@Override
public void onFinish(ITestContext arg0) {
// TODO Auto-generated method stub

}

@Override
public void onStart(ITestContext arg0) {
// TODO Auto-generated method stub

}

@Override
public void onTestFailedButWithinSuccessPercentage(ITestResult arg0) {
// TODO Auto-generated method stub

}

@Override
public void onTestFailure(ITestResult arg0) {
// TODO Auto-generated method stub

}

@Override
public void onTestSkipped(ITestResult arg0) {
// TODO Auto-generated method stub

}

@Override
public void onTestStart(ITestResult arg0) {
// TODO Auto-generated method stub

}

@Override
public void onTestSuccess(ITestResult arg0) {
// TODO Auto-generated method stub

}

}
In the above code, ITestResult is an interface which keeps all information about test case which is being executed. It captures some information like Test case execution status, test case name etc.
Now, if you want to capture screen shot of only failed test cases, then you need to write the code to capture screen shot in OnTestFailure method.

Note:
To take screen shot in selenium, we have one predefined interface known as TakesScreenshot, we can not create object of an interface in java. So we need to typecast it to Webdriver object.
                TakesScreenshot ts = (TakesScreenshot)driver;       (can’t create object directly so typecast and then create ).
                File srcFile = ts.getScreenShotAs(OutPutType.FILE);              (use the object created, then get screenshot as file & assign it to the File variable).
           FileUtils.copyFile(srcFile, new File(“./ScreenShots/image1.png”);        (now you need to copy the srcFile, so use FileUtils class, then give source file & destination file. In destination file “.” (dot) refers to the current working directory)     
                            
Refer below code now –
First Create one sample test case,
package screenshotOfFailedTest;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;


@Listeners (screenshotOfFailedTest.ListenerClass.class)

public class SampleTest {

public static WebDriver driver;

@Test
public void TestCase(){
driver = new FirefoxDriver();
driver.navigate().to("https://learnaboutsoftwaretesting.blogspot.in/");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();

driver.findElement(By.xpath("invalidXpath")).click(); //using invalid xpath so that test will fail & we can invoke ITestListener.
}

}
Note: We need to mention somewhere about listenerclass in our test case. There are 2 ways to do so. You may like to do this at class level (this will be applicable to all test cases in that class) or you may like to do this at suite level (which will be applicable to whole suite. You need to define this in testng.xml file).
In above example. I have done it for class level by adding below line of code for listener.
                              @Listeners(screenshotOfFailedTest.ListenerClass.class)
Now, Update the above created listener class for onTestFailure method. Add the code to take screen shot in this method. The code will look like –

package screenshotOfFailedTest;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;

public class ListenerClass implements ITestListener {
CaptureScreenShot c = new CaptureScreenShot();

@Override
public void onFinish(ITestContext arg0) { }

@Override
public void onStart(ITestContext arg0) { }

@Override
public void onTestFailedButWithinSuccessPercentage(ITestResult arg0) { }

@Override
public void onTestSkipped(ITestResult arg0) { }

@Override
public void onTestStart(ITestResult arg0) { }

@Override
public void onTestSuccess(ITestResult arg0) { }

@Override
public void onTestFailure(ITestResult arg0) {

//in below code, "SampleTest.driver" is used to get same driver from sample test class.
TakesScreenshot ts = (TakesScreenshot)SampleTest.driver;

File srcFile = ts.getScreenshotAs(OutputType.FILE);

try {
FileUtils.copyFile(srcFile, new File("./ScreenShots/"+arg0.getName()+".jpg"));
} catch (IOException e) {
e.printStackTrace();
}

}
}
This is all done. Now whenever you run the test case & if it fails, the screenshot will be captured at given location.

Hope this helps !!!!

How to take screen shot of failed test case only?

Taking Screen shot of all the test cases may create a memory issue, because we will be running the automation suite multiple times. Moreover, we can see the status of test cases (pass / failed / skipped) in the testing report. 
So, what is better is, to take the screen shot of only failed test cases.
Why to take screenshot is required in automation? 
              1. Screenshot helps us to understand the flow of application whether application is behaving correctly or not.
                 2. It helps as while doing cross browser testing.
                3. Another use is, while doing headless testing, you will not see actual GUI screen, but it will run on backend (ex – by using HTMLUnitBrowser ), in that case, we can track failed tests.
Now, there are two ways how we can take screen shot of failed test case only.
             
             1.      By using ITestListener Interface
             2.      By using ITestResult interface in @AfterMethod
In the below links, both the ways of taking screen shot on test failure are covered.

Data Driven test suite using TestNG framework

What is Data Driven Testing? 
Consider an example of account open to the bank. Now there are various possible scenarios for account open itself. But notice that the flow remains same, you will pass through the same screens but only you will enter different sets of data. 
Now, writing a test case for each set of data for same type of functionality it not a good idea, so what we can do here is, we can write one generic test case and pass the all sets of data to this test case. Here the role of data driven testing comes in to picture.
Data driven testing is where the test input and the expected output results are stored in a separate data file (normally in a tabular format) so that a single driver script can execute all the test cases with multiple sets of data.
The driver script contains navigation through the program, reading of the data files and logging of the test status information.
Test data (data file) can be stored in one or more central data sources like excel file, xml file or can be text file.

Below snap explains this – 

Advantages:
• This framework reduces the number of overall test scripts needed to implement all the test cases.
• Less amount of code is required to generate all the test cases.
• Offers greater flexibility when it comes to maintenance and fixing of bugs.
• The test data can be created before test implementation is ready or even before the system to be tested is ready.
Let us see now. How data driven testing can be achieved using TestNG framework.
Below steps explains how to create data driven testing framework.
              
              1.      Create a script with a set of constant test data
              2.      Replace constant test data with some variables.
              3.      Create multiple sets of test data in data storage like excel or xml file.
              4.      Assign the values to variable.
Consider a very simple example of emi calculator. We will write one generic test case & will pass different sets of data to the test case.
My Input Excel file, containing test data, looks like –

Below piece of code explains how can we pass multiple sets of test data to a single test case.
We need to use @DataProvider annotation of testing. This will help to provide the data to test case.
Note that, @DataProvider should always return an array of object.
Give some meaningful name to @DataProvider annotation.
Use the same name in @Test annotation.
Note: we need to add Apache POI jar files in project build path
Step1: Write test case with variables in it.
Step2: Use apache POI to read excel file, and return an array of object (because @DataProvider annotation should always return an array of Object).
Step3: Give some meaningful name to @DataProvider annotation.  Ex.    @DataProvider(name = “data”)
Step4: Use the same name in @Test annotation.   Ex.       @Test(dataProvider= “data”)
Refer Below code –

package dataDrivenTestingExample;

import java.io.File;
import java.io.FileInputStream;
import java.util.concurrent.TimeUnit;

import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class DataDrivenTest {


@Test(dataProvider = "data")
public void DataDrivenTest1(String LoanAmount, String InterestRate, String Tenure){

WebDriver driver = new FirefoxDriver();

driver.navigate().to("https://emicalculator.site/");

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();

driver.findElement(By.xpath("//*[@id='frm-emi']/p[1]/input")).sendKeys(LoanAmount);
driver.findElement(By.xpath("//*[@id='frm-emi']/p[2]/input")).sendKeys(InterestRate);
driver.findElement(By.xpath("//*[@id='frm-emi']/div/div/input")).sendKeys(Tenure);
driver.findElement(By.xpath("//*[@id='frm-emi']/p[3]/input")).click();

driver.close();

}


@DataProvider(name = "data")
public Object[][] testDataSupplier() throws Exception{
//file path where excel file placed, containing test data.
String filePath = "C:UsersPrakashworkspaceLearnTestNGTestDataSheet.xlsx";

//read excel file using file input stream, using Apache POI
FileInputStream fis = new FileInputStream(new File (filePath));
XSSFWorkbook wb = new XSSFWorkbook(fis);
XSSFSheet sheet = wb.getSheet("TestData");

//calculate total number of rows and columns so that we can iterate over it.
int totalNumberOfRows = sheet.getLastRowNum()+1;
int totalNumberOfCols = sheet.getRow(0).getLastCellNum();

//create an object array. which will store the test data from excel file
Object[][] testdata1 = new Object[totalNumberOfRows][totalNumberOfCols];


for (int i = 0; i <totalNumberOfRows; i++ ){
for (int j = 0; j < totalNumberOfCols; j++){

testdata1[i][j] = sheet.getRow(i).getCell(j).toString();
}
}
return testdata1;
}

}
Hope this helps !!!!

How to Execute TestNG.xml from command prompt?

There are various ways we can execute testing.xml file. We can run this from Eclipse itself. We can run it from command prompt, we can run it by creating .bat (batch) file, we can run it from any continuous integration tool.
Let us see here how can we run this command prompt.
Create sample java project.
 
Write some testing test in the project.
Create testing.xml file.
Sample test: (we will run this test from command prompt)

package runTestNGxmlFromCMD;

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

public class RunTestNGTest {

@Test
public void test() throws InterruptedException{

WebDriver driver = new FirefoxDriver();

driver.navigate().to("https://learnaboutsoftwaretesting.blogspot.in");

System.out.println("navigate to website");

Thread.sleep(3000); //sleep for 3 second so that you can observe that site is opened, before it closes quickly

driver.close();

}

}

Testng.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Default suite">
<test verbose="2" name="Default test">
<classes>
<class name="runTestNGxmlFromCMD.RunTestNGTest"/>
</classes>
</test> <!-- Default test -->
</suite> <!-- Default suite -->
Now, my project structure looks like – 

Here is below commands to execute ‘testng.xml’ file from command line
Now,   my project location is:    C:UsersPrakashworkspaceRunTestNGXML
           Location of my jar files:  C:UsersPrakashworkspaceRunTestNGXMLlib*
      By default, class files will be created in project location (here – C:UsersPrakashworkspaceRunTestNGXMLbin)
So, based on above information, we need to run below piece of code in you command prompt.
cd C:UsersPrakashworkspaceRunTestNGXML

java -cp C:UsersPrakashworkspaceRunTestNGXMLlib*;C:UsersPrakashworkspaceRunTestNGXMLbin org.testng.TestNG testng.xml

Hope this helps !!!

Cross browser testing using TestNG framework?

Let us understand first what is cross browser testing.
 
Whenever the application is developed, it is intended to use on most of the web browser. So, while testing we need to make sure that the application works all and all for major browsers like IE, firefox, Chrome, Safari etc. To test the application over multiple browsers is known as cross browser testing.
In an automation framework, there are two ways to pass browser name which will decide on which browser our test should run.
            1.      To pass the browser name / value from external file like properties file or excel file
            2.      To pass the browser name / value from testing.xml file.
Let us understand how can we pass the browser value from testing.xml here.
Below piece of code accepts the parameter form testing.xml
In Testng.xml, browser values are given.

package crossBrowserTests;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Parameters;

public class CrossBrowserTestFromTestNG {

WebDriver driver;

@Parameters("browser")
@BeforeMethod
public void lanuchBrowser(String browser){

if (browser.equalsIgnoreCase("firefox")){

driver = new FirefoxDriver();
System.out.println("Browser selected for testing is : Firefox");

}
else if (browser.equalsIgnoreCase("chrome")){
System.setProperty("webdriver.chrome.driver", "C:chromedriver.exe");

driver = new ChromeDriver();
System.out.println("Browser selected for testing is : Google Chrome");

}
else if (browser.equalsIgnoreCase("ie")){
System.setProperty("webdriver.ie.driver", "C:IEDriverServer.exe");
driver = new InternetExplorerDriver();
System.out.println("Browser selected for testing is : IE");
}
}

@org.testng.annotations.Test
public void Test(){

driver.navigate().to("https://learnaboutsoftwaretesting.blogspot.in");
System.out.println("Title of the webpage is: "+driver.getTitle());
}

@AfterMethod
public void closeBrowser(){
driver.close();
}


}

Sample Testng.xml (where browser value is supplied)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Default suite">
<test verbose="2" name="FireFox Test">
<parameter name="browser" value="firefox"></parameter>
<classes>
<class name="crossBrowserTests.CrossBrowserTestFromTestNG"/>
</classes>
</test> <!-- Default test -->
<test verbose="2" name="Chrome Test">
<parameter name="browser" value="chrome"></parameter>
<classes>
<class name="crossBrowserTests.CrossBrowserTestFromTestNG"/>
</classes>
</test> <!-- Default test -->
<test verbose="2" name="Internet Explorer Test">
<parameter name="browser" value="ie"></parameter>
<classes>
<class name="crossBrowserTests.CrossBrowserTestFromTestNG"/>
</classes>
</test> <!-- Default test -->
</suite> <!-- Default suite -->
When you run testing.xml, it will run 3 tests. A similar test in 3 different browsers (firefox, ie, chrome).
Hope this helps !!!

How to pass parameter through testng.xml file to a test case?

We could define the parameters in the testng.xml file and then reference those parameters in the source files.
Create a java test class, say, ParameterizedTest.java and add a test method say parameterizedTest() to the test class. This method takes a string as input parameter. Add the annotation @Parameters(“browser”) to this method

// TestNG Interview Questions
public class ParameterizedTest {
@Test
@Parameters("browser")
public void parameterizedTest(String browser){
if(browser.equals("firefox")){
System.out.println("Open Firefox Driver");
}else if(browser.equals("chrome")){
System.out.println("Open Chrome Driver");
}
}
}
The parameter would be passed a value from testng.xml, which we will see in the next step.
We could set the parameter using the below syntax in the testng.xml file. 
<parameter name="browser" value="firefox"/>
Here, name attribute represents the parameter name and value represents the value of that parameter.

.
Hope this helps !!!

Difference between @BeforeTest and @BeforeMethod annotations in TestNG

This is most confusing point in testng. Let us understand the difference between them.
@BeforeTest Will be called before any Test only..
@BeforeMethod will be called before every method

Note that, not every method in a file is Test, but every Test in a file is a method.

In the above case the helper() method is only a method not a Test. Whereas toBeforeTest() and toBeforeMethod() are Test, since they are preceded with “@Test” annotation.

@BeforeTest will be executed only for the blocks of toBeforeTest() and toBeforeMethod().
Whereas @BeforeMethod will be executed for all the methods of toBeforeTest(), toBeforeMethod() as well as the helper() method

@BeforeTest is used to do setup related to bunch of tests written inside the tag as the author has mentioned.

@BeforeTest
@BeforeMethod
To execute a set-up method before any of the test methods included in the < test > tag in the testng.xml file.
To execute a set-up method before any of the test methods annotated as @Test.

Example Testng.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="none">


<test name="test">
<classes>
<class name=".BeforetestandBeforemethod">
<methods>
<include name="toBeforeTest"></include>
<include name="toBeforeMethod"></include>
</methods>
</class>
</classes>
</test>

<methods>
</suite> <!-- Suite -->


Example Java code such as:

import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;


public class BeforetestandBeforemethod {

@BeforeTest
public void beforeTest() {
System.out.println("@BeforeTest");
}
@BeforeMethod
public void beforemethod(){
System.out.print("@BeforeMethod");
}

public void helper() {
System.out.println("Flow");
}

@Test
public void toBeforeMethod() {
helper();
System.out.println("BeforeMethod");
}

@Test
public void toBeforeTest() {
helper();
System.out.println("BeforeTest");
}

}
Execute code by TestNG, then the Output would be:
When run  <include name=”toBeforeTest”></include>

 

Run  <include name=”toBeforeMethod”></include>

Hope this helps !!!!


What are TestNG annotations?

Here is a quick overview of the annotations available in TestNG along with their attributes.
@BeforeSuite
@AfterSuite
@BeforeTest
@AfterTest
@BeforeGroups
@AfterGroups
@BeforeClass
@AfterClass
@BeforeMethod
@AfterMethod
Configuration information for a TestNG class:
@BeforeSuite: The annotated method will be run before all tests in this suite have run.
@AfterSuite: The annotated method will be run after all tests in this suite have run.
@BeforeTest: The annotated method will be run before any test method belonging to the classes inside the <test> tag is run.
@AfterTest: The annotated method will be run after all the test methods belonging to the classes inside the <test> tag have run.
@BeforeGroups: The list of groups that this configuration method will run before. This method is guaranteed to run shortly before the first test method that belongs to any of these groups is invoked.
@AfterGroups: The list of groups that this configuration method will run after. This method is guaranteed to run shortly after the last test method that belongs to any of these groups is invoked.
@BeforeClass: The annotated method will be run before the first test method in the current class is invoked.
@AfterClass: The annotated method will be run after all the test methods in the current class have been run.
@BeforeMethod: The annotated method will be run before each test method.
@AfterMethod: The annotated method will be run after each test method.
For before methods (beforeSuite, beforeTest, beforeTestClass and beforeTestMethod, but not beforeGroups): If set to true, this configuration method will be run regardless of what groups it belongs to.
For after methods (afterSuite, afterClass, …): If set to true, this configuration method will be run even if one or more methods invoked previously failed or was skipped.
@DataProvider
Marks a method as supplying data for a test method. The annotated method must return an Object[][] where each Object[] can be assigned the parameter list of the test method. The @Test method that wants to receive data from this DataProvider needs to use a dataProvider name equals to the name of this annotation.
Parameters for dataprovider –
a.       name
The name of this data provider. If it’s not supplied, the name of this data provider will automatically be set to the name of the method.
b.      parallel
If set to true, tests generated using this data provider are run in parallel. Default value is false.
@Factory
Marks a method as a factory that returns objects that will be used by TestNG as Test classes. The method must return Object[].
@Listeners
Defines listeners on a test class.
.
@Parameters
Describes how to pass parameters to a @Test method.
@Test  -> AnnotationMarks a class or a method as part of the test.
—  @Test (name=“example“,
                              description=“example test“,
                              expectedExceptions=IOException.class,
                              dataProvider=“dataProv“,
                              groups={“functional“,“stress“},
                              dependsOnGroups=“database“,
                              dependsOnMethods={“init“,“setup“},
                              enabled=true,
                              successPercentage=80,
                              timeout=10000,
                              invocationCount=10,
                              threadPoolSize=10,
                              sequential=true)

  Hope this helps !!!!

What is TestNG.xml ?

When you want to run the TestNG test, which you have created, there are two ways to do so.
If you want to run single test case, you can right click on the test, and Run it as TestNG Test. (form Eclipse).
But, if you want to run a set of test cases from various classes, then you need to specify them somewhere, because, you can’t click on each class and run it. Moreover, when running our test cases from the continuous integration tool ( like Jenkins), we need some configuration file which will tell us which test cases to run, if you need to pass some parameters at runtime, then to do that, like wise to achieve something more.
So, In TestNG, all these things are written in a xml file, called as TestNG.xml.
We can configure our test suite utilizing test methods, classes, packages, groups, and so on in single testng.xml file.
TestNG bolster parallel testing, Parameterization (Passing parameters values through XML 
configuration), Data Driven Testing(executing same test strategy numerous times utilizing diverse data) .
The testng.xml file allows lots of flexibility in selecting the test classes and test methods to be executed. You can exclude or include test methods or exclude and include groups.
You can specify the listener to be used by all test classes in the testng.xml file
How to create testing.xml file? 
Create testng suite file manually as below:
  1. Run once your project
  2. Refresh your project
  3. Now, “test-output” folder will get created under your project – unfold the folder
  4. Double click the ‘index.html’ file; it will get open in the Eclipse Browser window
  5. Click on the ‘.xml’ link given on the left panel
  6. The test suite XML will get opened on the right-side panel
  7. Take a copy of the XML content
  8. Right click your project >> New >> File
  9. Give name of your test suite file (with.xml extension e.g. testng.xml) and click finish
  10. Paste the content that you copied from the ‘index.html’ and amend your suite file as per your needs
Sample TestNG.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Aayush Corp Test suite">

<listeners>
<listener class-name="com.aayushCorp.utilities.ITestListenerClass" />
</listeners>

<test verbose="2" name="UserTests" >
<classes>
<class name="com.aayushCorp.appData.tests.UserTests"/>
</classes>
</test> <!-- Default test -->
</suite> <!-- Default suite -->
TestNG.xml is –
•          Controls test runs
•          Optional
•          File can have any name (.xml suffix advised
             TestNG creates “testng-failed.xml”, for easy re-run.
•          Root: <suite> (then <test>/<classes>/<method>)
•          Suite can also point to a list of additional xml suite files.
•          Some of the available specifications are described next.
Hope This helps !!!!