Friday, June 17, 2011

Selenium IDE - How to Read table content & store as a Array


It is simple.  Try the below code:

1. This function locates the table in a html page.
2. Finds no. of rows in a table.
3. Reads each row data & stores in an array.
4. Returns the data in an array.

Selenium.prototype.getSaraTable = function(tableName) {
try
{
    var table = this.browserbot.findElement(tableName);
            var rows = table.rows.length;
            var TblArr = [];
                for (var i = 0; i < rows ; i++)
                {
                        var celltxt  = getText(table.rows[i].cells[0]);
                        TblArr.push(celltxt);
            }
                return TblArr;
}
catch (e)
{
        throw new SeleniumError("Threw an exception!" + e.message);
}

};


 Then call this function in Selenium IDE as,


<tr>
            <td>storeSaraTable</td>
            <td>css=table</td>
            <td>Result</td>
</tr>


Ex:

  Table is as below :

123
456
789
Abc
efg






           Results contains data as: 123,456,789,Abc,efg



No comments:

Post a Comment