18 January 2026:


This forum is now archived and is in read-only mode. Please continue discussions on our improved new Sahi Pro Community forum.



Sahi Pro is an enterprise grade test automation platform which can automate web, mobile, API, windows and java based applications and SAP.

readCSVFile doesn't stop at end of file

wgoriswgoris Members
edited November -1 in Sahi - Open Source
Hello,

We are doing a POC to see if we can use sahi for our web tests. However we are having a problem with the function readCSVFile. In the .csv file are 3 lines. But the script doesn't stop after executing the 3 lines. It starts a 4th time and uses data null. There is no newline after the 3rd line in the csv file.

I'm wondering if I do something wrong in the code. Or that there is a problem with the function readCSVFile.

These are the lines in the csv file:

Wijzig adres,België (BE),2235,Hulshout,Geersbroekstraat,BlokB,21,A
Wijzig adres,België (BE),2250,Olen,Noorderwijkseweg,BlokC,92,E
Wijzig adres,België (BE),2000,Antwerpen,Meir,Links,5,C

I have writen the following code:

//read data from my csv file
var $data = _readCSVFile("../scripts/IMJV/01.LoketIdentificatie/DataAdres.csv");

//fill in screen to update adress
_dataDrive(WijzigAdres, $data);

//function WijzigAdres
function WijzigAdres($OpenKnop, $Land, $Postcode, $Gemeente, $Straat, $Aanvulling, $Nummer, $Bus){

_click(_submit($OpenKnop));
_setSelected(_select("adresForm:land"), $Land);
_setValue(_textbox("adresForm:postcode"), $Postcode);
_setValue(_textbox("adresForm:gemeente"), $Gemeente);
_setValue(_textbox("adresForm:straat"), $Straat);
_setValue(_textbox("adresForm:aanvulling"), $Aanvulling);
_setValue(_textbox("adresForm:nummer"), $Nummer);
_setValue(_textbox("adresForm:bus"), $Bus);
_click(_submit("Bewaren"));

}

I expect the function WijzigAdres is executed 3 times, because there are 3 lines in the CSV file. But the script tries to execute it 4 times with values null. Here the script failes when it has to select a country in a dropdownlist (_setSelected(_select("adresForm:land"), $Land);).

Thank you in advance for your help.
Best regards,
Werner

Comments

  • narayannarayan Administrators
    Hi Werner,

    What OS and version of Sahi are you on?

    You can do this to fix the problem:

    Open sahi/htdocs/spr/lib.js

    and replace the function Sahi.prototype._readCSVFile with the code below.
    Sahi.prototype._readCSVFile = function(filePath, wordSeparator){
        if (!wordSeparator) wordSeparator = ",";
        var contents = this._readFile(filePath);
        var lines = contents.replace(/\r\n/g, '\n').replace(/\r/g, '\n').split("\n");
        var data = []; // new Array();
        for (var i=0; i<lines.length; i++){
            if (lines[i].replace(/^\s*|\s*$/g, '') == "") continue;
            var words = this.splitUnQuoted(lines[i], wordSeparator); //lines[i].split(",");
            for (var j=0; j<words.length; j++){
                var w = words[j];
                w = w.replace(/^\s*|\s*$/g, '');
                if (w.match(/^".*"$/)){
                    words[j] = eval(w); 
                }
            }
            data[data.length] = words;
        }
        return data;
    }; 
    

    It will ignore any blank lines in the csv file.

    Please restart Sahi before you retry.

    Regards,
    Narayan
  • Hello narayan,

    It works now. Thank you very much for the help and quick support!

    My OS is Ubuntu 10.04 with Firefox 3.6.13 and Sahi build 2010-11-03.

    Best regards,
    Werner
This discussion has been closed.