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.
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++)
{
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); }
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();
@AfterClass
public void tearDown() throws InterruptedException{
selenium.stop();
seleniumserver.stop();
}
}
Please let me know if you need more information regarding Data driven testing....
This example does not use a spreadsheet.
ReplyDeleteSorry for that i forgot to change the code....now it will work....pls go through it...
ReplyDeletethanks for your examples,very helpful !
ReplyDeleteRSK
could u please state the tools required for this
ReplyDeleteBelow post will explain you what to do
ReplyDeletehttp://testerinyou.blogspot.com/search/label/Setting%20up%20Selenium%20RC%20and%20TestNG
Hi naga,
ReplyDeleteYour posts are very helpful.
May i know how construct a framework in selenium.
I am using tesNG for coding.
-chandu
Hi Naga,
ReplyDeleteYour 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 :-)
Hi Naga,
ReplyDeleteI 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());
Sachin,
ReplyDeleteMaybe 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
Hi Naga,
ReplyDeleteAbove script is only works for TestNG? Any modification for Junit4? Please help me. i am new to selenium so.
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 ?
ReplyDeleteYou 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.
not fetching the data from excel....its open up google...but not able to read data from excel....should i name for sheet also..
ReplyDeleteHi,
ReplyDeleteI 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.......
off white shoes
ReplyDeletea bathing ape
kobe basketball shoes
nike off white
nike shox for men
air jordan
moncler jackets
pandora jewelry
yeezy shoes
curry 7