Showing posts with label Data Driven Testing. Show all posts
Showing posts with label Data Driven Testing. Show all posts

Monday, 5 September 2011

Data Driven Testing using Spreadsheets


Consider spreadsheet holds multiple combinations of inputs to be fed to test the application. The best practice here is to keep the data sheet in a common place, preferably in the source of current directory.
Following script designed using Jxl.jar file
public class DDT {

public Selenium selenium;

@Test
public void DataDrivenTesting()throws Exception {

FileInputStream fi=new FileInputStream("F:\PATH.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); }
}
}

Note:Below are should import to run above script
import java.io.FileInputStream;
import jxl.Sheet;
import jxl.Workbook;
import com.thoughtworks.selenium.*;
import org.testng.annotations.Test;

Create connection with database and fetch the values

Following script will explain how to create connection with database and fetch the values
public class DB_Connection_SQL {

public static String url = "jdbc:sqlserver://000.000.0.000:0000;DatabaseName=TEST_DB" ;
public static void main(String [] args){

Connection con = DriverManager.getConnection(url,"UN","PWD");

// Create statement object which should use SQL statement.
Statement stmt = con.createStatement();

// Send SQL SELECT statements to the database which returns the //requested information as rows of data in a Result Set object.
ResultSet res = stmt.executeQuery( "select top 1 name from Emp" );

// Fetch value of "name" from "result" object.
String Ename = res.getString( "name" );

// Use the email Address value to login to application.
selenium.type( "userID" , Ename);
}
}
Note:Below are should import to run above script
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;