Sahi Documentation

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.


Since: Sahi ProSahi OSSahi Pro StarterDesktop Add-OnMobile Add-OnSAP Add-OnAI Assist Add-On

Available for modes: Browser | Windows | Java | Android | iOS | SAP | AI Assist

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

$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



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);
  _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() {
function logAfter() {
_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)


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

Excel Sheet

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


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


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