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.

array return by java could use _writeCSVFile?

beesmanbeesman Members
edited August 2010 in Sahi - Open Source
var $c = Packages.MyCsvManager.getList();
_debug($c);
_writeCSVFile($c, "c:/aa.csv") ;

MyCsvManager.java
public static String getList(){
    List addList = new ArrayList();
    addList.add("00000000001");
    ......
    addList.add("00000000005");
    List rowList = new ArrayList();
    for(int i=0; i<addList.size(); i++){
        rowList.add("A");
        rowList.add(addList.get(i));
        addList.set(i, rowList);
        rowList = new ArrayList();
    }
    
    return getJSObject(addList);
}

    public static String getJSObject(final List<ArrayList<String>> list) {
        StringBuffer sb = new StringBuffer();
        sb.append("[[");
        boolean isFirst1 = true;
        for (Iterator<ArrayList<String>> iterator = list.iterator(); iterator.hasNext();) {
            if (isFirst1)
                isFirst1 = false;
            else
                sb.append(",[");
            ArrayList<String> record = iterator.next();  
            for (int i = 0; i < record.size(); i++ ) {
                if(i!=0){
                    sb.append(",");
                }
                String value = record.get(i);
                sb.append("\"" + Utils.makeString(value) + "\"");
                //sb.append("\"" + Utils.escapeDoubleQuotesAndBackSlashes(value) + "\"");
            }
            sb.append("]");
        }        
        sb.append("]");
        return sb.toString();
        
    }



console
Debug: "A","00000000001"],["A","00000000002"],["A","00000000003"],["A","00000000004"],["A","00000000005"

aa.csv is generated but empty.


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

_writeCSVFile($c, "c:/bb.csv") ;

bb.csv is generated and have contents.


why aa.csv is empy

Comments

  • narayannarayan Administrators
    Is getList returning a String or a 2D array?
  • there is a string like return by net.sf.sahi.plugin.DBClient.getJSObject(ArrayList<ArrayList<String>>)
  • narayannarayan Administrators
    You will have to first make the string a 2D array by evaling it.
    var $c = Packages.MyCsvManager.getList();
    
    var $data = eval("(" + $c + ")");
    
    _debug($data);
    _writeCSVFile($data, "c:/aa.csv") ;
    

    What we are doing is taking a JSON string and converting it to a javascript object; then passing it to _writeCSVFile.

    Regards,
    Narayan
  • var $data = _eval("(" + $c + ")");
    _debug($data);
    _writeCSVFile($data, $fileName) ;

    Debug: A,00000000001,A,00000000002,A,00000000003,A,00000000004,A,00000000005

    the file is still empty


    are there something wrong in the getJSObject I written ,
    not the net.sf.sahi.plugin.DBClient#getJSObject
  • should i change

    public static String getList(){} to
    public static String[][] getList(){}
    ?
  • var $c = Packages.MyCsvManager.getList();
    var $data;
    _set($data, _eval($c));
    _writeCSVFile($data, $fileName) ;

    i rewrite the code , and it works
  • I also have the same problem, and haven't solved *+*
  • narayannarayan Administrators
    beesman,

    You were using _eval instead of eval, which is not correct.

    liprise,

    Could you please elaborate on your problem?

    Regards,
    Narayan
Sign In or Register to comment.