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.
_dataDrive
Section titled “_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
| Name | Type | Description |
|---|---|---|
$fn | function | Javascript function to be executed repeatedly |
$data | 2D Array | Data on which fn should be called iteratively |
$preFn | functionoptional | Javascript function to be executed at each iteration BEFORE fn |
$postFn | functionoptional | Javascript 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 calledfunction doAdd($first, $second, $total){ _setValue(_textbox("first"), $first); _setValue(_textbox("second"), $second); _click(_button("Add")); _assertEqual($total, _textbox("total").value);}
// datavar $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 postFnfunction logBefore() { _log("Before");}function logAfter() { _log("After");}_dataDrive(doAdd, $data, logBefore, logAfter);Using with Data Source Functions
Section titled “Using with Data Source Functions”Simple 2D array
Section titled “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
Section titled “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
Section titled “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);
