Skip to content
Get Free Trial

Data Drive Functions

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

Since
Sahi Pro 3.5
Desktop Engine 7.0.0
Mobile Engine 7.5.0
SAP Engine 9.0.0

Modes BrowserWindowsJavaAndroidiOSSAP

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

Arguments

NameTypeDescription
$fnfunctionJavascript function to be executed repeatedly
$data2D ArrayData on which fn should be called iteratively
$preFnfunctionoptionalJavascript function to be executed at each iteration BEFORE fn
$postFnfunctionoptionalJavascript function to be executed at each iteration AFTER fn

Returns

null

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);
var $data = [
[2, 3, 5],
[1, 2, 4],
[4, 3, 7]
]
_dataDrive(doAdd, $data)
var $data = _readCSVFile("data.csv");
_dataDrive(doAdd, $data)
var $data = _readExcelFile("data.xls", "Sheet1");
_dataDrive(doAdd, $data);

or

var $excel = _getExcel("data.xls", "Sheet1");
$data = $excel.getDataForDataDrive();
_dataDrive(doAdd, $data);
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);