Monday 23 December 2013

How to do Data Driven Testing using Selenium RC


How to do Data Driven Testing using Selenium RC withJUnit
There are many ways to do Data driven tests. Excel sheet for reading data and FileInputStream method.
Below script explains 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.
Data Driven Testing refers to using the same test (or tests) multiple times with varying data. These data sets are often from external files i.e. .csv file, text file, or perhaps loaded from a database. Data driven testing is a commonly used test automation technique used to validate an application against many varying input. When the test is designed for varying data, the input data can expand, essentially creating additional tests, without requiring changes to the test code.
·         Create a Java project in eclipse.
·         Create a new class DatadrivenJUnit.
·         Paste the below code in Eclipse.
·         Change the path of excel file according to your requirement.
NOTE:
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
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;
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();
}
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); } }
public void tearDown() throws InterruptedException{
selenium.stop();
 seleniumserver.stop ();
}
 }
   

No comments:

Post a Comment