Sahi Pro - Data Drive APIs

abstract Data driver refers to running a series of steps repeated with multiple sets of data
This can be accomplished using the api _dataDrive.

_dataDrive

Since Sahi Pro: 3.5
Since Sahi OS: 3

_dataDrive($fn, $data[, $preFn[, $postFn]])

Arguments
$fnfunction Javascript function to be executed repeatedly
$data2D Array Data on which fn should be called iteratively
$preFnfunction optional Javascript function to be executed at each iteration BEFORE fn
$postFnfunction optional Javascript function to be executed at each iteration AFTER fn

Details

Iterates over the data and calls fn for each row of data.
preFn and postFn if specified, are called before and after fn for each row.
preFn and postFn are guaranteed to be called even if fn fails or returns midway.

Example:
We will create a function doAdd and call it over multiple sets of data.

// function to be called
function doAdd($first, $second, $total){
  _setValue(_textbox("first"), $first);
  _setValue(_textbox("second"), $second);
  _click(_button("Add"));
  _assertEqual($total, _textbox("total").value);
}

// data
var $data = [
[2, 3, 5],
[1, 2, 4],
[4, 3, 7]
]

// Pass doAdd and $data to _dataDrive.
_dataDrive(doAdd, $data); // will call doAdd for each row

// Note how doAdd is passed as a function (without quotes or brackets)
// _dataDrive(doAdd, $data); // CORRECT
// _dataDrive(doAdd(), $data); // WRONG to call with brackets
// _dataDrive("doAdd", $data); // WRONG to use a string

// Call with preFn and postFn
function logBefore() {
  _log("Before");
}
function logAfter() {
  _log("After");
}
_dataDrive(doAdd, $data, logBefore, logAfter);

Using with various data source APIs

Simple 2D array

var $data = [
[2, 3, 5],
[1, 2, 4],
[4, 3, 7]
]
_dataDrive(doAdd, $data)

CSV

var $data = _readCSVFile("data.csv");
_dataDrive(doAdd, $data)

Excel Sheet

var $data = _readExcelFile("data.xls", "Sheet1");
_dataDrive(doAdd, $data);

or

var $excel = _getExcel("data.xls", "Sheet1");
$data = $excel.getDataForDataDrive();
_dataDrive(doAdd, $data);

Database

var $db = _getDB("com.mysql.jdbc.Driver",
          "jdbc:mysql://localhost/sahitest?user=user&password=password",
          "user", "password");
var $data = $db.select("select num1, num2 from addData");
_dataDrive(doAdd, $data);