Friday 10 January 2014

Data Driven Testing using Selenium RC with JUnit



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.
We are  using Eclipse, selenium RC, Junit and Excel sheet for this script.

·         Create a Java project in eclipse.
·         Create a new class DatadrivenJUnit.
·         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 please do not forget to change the excel file to .xls if you are using MS-office2007.
Below is the code
import java.io.FileInputStream;
import jxl.Sheet;
import jxl.Workbook;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.server.RemoteControlConfiguration;
import org.openqa.selenium.server.SeleniumServer;
import com.thoughtworks.selenium.*;

public class DatadrivenJUnit extends SeleneseTestCase
{
public Selenium selenium;
public SeleniumServer seleniumserver;
@BeforeClass
public void setUp() throws Exception
{
RemoteControlConfiguration rc = new RemoteControlConfiguration();
rc.setSingleWindow(true);
seleniumserver = new SeleniumServer(rc);
selenium = new DefaultSelenium("localhost", 4444, "*iexplore", "http://");
seleniumserver.start();
selenium.start();
}
@Test
public void testDatadrivenJUnit() 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();
}
 }

No comments:

Post a Comment