October 26, 2010

How to do Data Driven Testing using TestNG

There are many ways to do Data driven tests.I used Excel sheet for reading data and FileInputStream method.

Below script explains you how to Read data from excel sheet and use the data to search google.

I am using Eclipse, selenium RC, TestNG and Excel sheet for this script.


--->Create a Java project in eclipse.
--->Create a new class DatadriventestNG.
--->Paste the below code in Eclipse.
--->Chnage the path of excel file according to your requirement.








Please note that selenium will support only .xls format plese do not forget to change the excel file to .xls if you are using MS-office2007.

Below is the code

package test;

import com.thoughtworks.selenium.*;

import org.openqa.selenium.server.*;
import org.testng.annotations.*;


public class DefaultTNG {
public Selenium selenium;
public SeleniumServer seleniumserver;

  @BeforeClass
  public void setUp() throws Exception {
RemoteControlConfiguration rc = new RemoteControlConfiguration();
seleniumserver = new SeleniumServer(rc);
selenium = new DefaultSelenium("localhost", 4444, "*iexplore", "http://");
seleniumserver.start();
selenium.start();
}

@Test
public void testDefaultTNG()throws Exception {
FileInputStream fi=new FileInputStream("F:\\Framework\\testdata\\search.xls");
Workbook w=Workbook.getWorkbook(fi);
Sheet s=w.getSheet(0);
selenium.open("http://www.google.com");
selenium.windowMaximize();
for (int i = 1; i < s.getRows(); i++)
 { 
//Read data from excel sheet
 selenium.type("name=q",s.getCell(0,i).getContents());
 selenium.click("btnG");
 Thread.sleep(1000); } 
}
@AfterClass
 public void tearDown() throws InterruptedException{
selenium.stop();
seleniumserver.stop();
 
 }
 }



 Please let me know if you need more information regarding Data driven testing....


14 comments:

  1. This example does not use a spreadsheet.

    ReplyDelete
  2. Sorry for that i forgot to change the code....now it will work....pls go through it...

    ReplyDelete
  3. thanks for your examples,very helpful !

    RSK

    ReplyDelete
  4. could u please state the tools required for this

    ReplyDelete
  5. Below post will explain you what to do

    http://testerinyou.blogspot.com/search/label/Setting%20up%20Selenium%20RC%20and%20TestNG

    ReplyDelete
  6. Hi naga,
    Your posts are very helpful.
    May i know how construct a framework in selenium.
    I am using tesNG for coding.

    -chandu

    ReplyDelete
  7. Hi Naga,

    Your Blog really helps me to learn selenium , i am new to selenium, just started learning, your examples helps me a lot, Very Very Very much thanks for posting all those informations :-)

    ReplyDelete
  8. Hi Naga,


    I am getting an error on s.getCell(0,i).getContents() unable to fetch contain from excel sheet

    //Read data from excel sheet
    selenium.type("name=q",s.getCell(0,i).getContents());

    ReplyDelete
  9. Sachin,

    Maybe you are missign JXL.jar in lib folder..u need to add JXl.jar file

    if you are still facign the issue pls send mail to nagaselenium@gmail.com

    ReplyDelete
  10. Hi Naga,
    Above script is only works for TestNG? Any modification for Junit4? Please help me. i am new to selenium so.

    ReplyDelete
  11. I dont think this leverages anything that TestNG provides for doing data driven approach. How is this different from a standalone public static void main() which also can be used to iterate through to read data from the excel sheet ?

    You might want to modify this post to include references to @DataProvider annotation, if you were to be branding this as "How to do data driven testing using TestNG" so that it is more apt and relevant.

    ReplyDelete
  12. not fetching the data from excel....its open up google...but not able to read data from excel....should i name for sheet also..

    ReplyDelete
  13. Hi,

    I was running data driven test script using Selenium Webdriver TestNG but below code is not working...

    Cell tableStart=sheet.findCell(tableName);

    Even though I have added latest JXL.jar file (ver : jxl 2.6.12) and imported as
    import jxl.*;

    Any suggestion.......

    ReplyDelete