Top Rounds
[Hide Navigation]

Data Driven Testing ·

Sahi has some nice inbuilt features for data-driven testing.

The core concept is that data is present as a grid of rows and columns, and tests need to run with each row of the grid as input.

Suppose we have a 2-dimensional array of data, on which we perform an add operation:

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

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

A simple way to do this is to iterate over the array and invoke the doAdd function with values from each row.

for (var $i=0; $i<$data.length; $i++){
	var $row = $data[$i];
	doAdd($row[0], $row[1], $row[2]);
}

But this loop will fail in the second row, since 1+2=3 and not 4, as given in the second row. In case of failure we need to continue to the next row and not stop there. To do this, we use a try catch block.

for (var $i=0; $i<$data.length; $i++){
    var $row = $data[$i];
    try{    
        doAdd($row[0], $row[1], $row[2]);
    } catch (e) {
        // The try catch block helps in continuing with the next
        //  step of data inspite of failures.
        // _logException so that the logs will indicate the failing line.
        _logException(e);        
    }
}

All this looping and try catch have been neatly wrapped into a utility function _dataDrive. So instead of the following steps, you will just have:

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

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

_dataDrive(doAdd, $data);

Note that the function itself is passed to _dataDrive. (No open close brackets after doAdd).

Instead of $data being hard coded in the test, you can read it from a CSV file/database or excel sheet. For example, the above code could become

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

var $data = _readCSVFile("path/to/data_file.csv");

_dataDrive(doAdd, $data);

Sahi can read and write to CSV (Comma Separated Value) files, databases and Excel sheets.


Reading and writing to CSV files

_readCSVFile(csvFilePath) reads a CSV file and returns a two dimensional array.
_writeCSVFile(array2D, filePath) writes the two dimensional array into a file in CSV format.


Working with databases

Sahi uses JDBC to connect to various databases. You will need to get the corresponding JDBC driver for your database and add it to Sahi’s classpath.

All communication to databases happens through the API _getDB.
Have a look at _getDB


Working with Excel sheets

Have a look at Working with Excel sheets

Click to View a detailed example on data driven testing




---


Top Rounds