_readFile is used to read text files.
_readFile(filePath)
Returns the contents of the file as a single String.
The parameters are:
This is the simplest way of reading a file’s contents. This can be used to read data from a csv file or a tab separated file to perform data driven testing.
var $fileContents = _readFile("C:\\abc.txt");
var $SEPARATOR = ","; // or "\t" for tab
var $data = new Array();
var $lines = $fileContents.replace(/\r/g, '').split("\n");
for (var $i=0; $i<$lines.length; $i++){
$data[$i] = $lines[$i].split($SEPARATOR);
}
$data is now a 2 dimensional array of values.
For eg. if you had a file with these lines:
red, apple
green, banana
$data would be
$data[0][0] "red"
$data[0][1] "apple"
$data[1][0] "green"
$data[1][1] "banana"
To access values from $data through a loop:
for (var $i=0; $i < $data.length; $i++){
var $row = $data[$i];
_alert($row[0]+" "+$row[1]);
}