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.

Sample script for checking the html table values against DB values

navanithanavanitha Members
edited November -1 in Sahi - Open Source
Can anybody post a sample script for comparing the values in a html table with the values in the DB (DB being used is mysql)
And also how to fetch the table element name using sahi..

Comments

  • narayannarayan Administrators
    Hi Navanitha,

    Here is some sample code:
    <browser>
    // Fetches table contents. contents will be accessible via indexes. 
    function getTableContents(table){
        var array2d = new Array();
        var rows = table.rows;
        for (var i=0; i<rows.length; i++){
            var row = rows[i];
            array2d[i] = new Array();
            var cells = rows[i].cells;
            for (var j=0; j<cells.length; j++){
                array2d[i][j] = _getText(cells[j]);
            }
        }    
        return array2d;
    }
    </browser>
    
    // Read data from database
    var $db = _getDB("com.mysql.jdbc.Driver", "jdbc:mysql://localhost/sahitest?user=user&password=password", "user", "password");
    var $dbData = $db.select("select id, name, age from user order by age asc limit 10");
    
    // Assert that the database contents are same as contents of table.
    _assertEqual($dbData, getTableContents(_table("tableId")));
    

    To fetch table element, just ctrl-mouseover the edge of the table till Sahi identifies the table. Or you could use _parentTable(_cell("cellId")).


    Make sure you replace the query and the required values in the above script.

    Regards,
    Narayan
  • navanithanavanitha Members
    edited March 2010
    At the time of playback is there any way to check the results..
    I wanted to display the values i am getting from the table..How can i check it at the time of playback..
    like some alert for displaying the values..
  • narayannarayan Administrators
    You could use _alert(msg) to alert or use _debug(msg) to print to Sahi's console.
  • navanithanavanitha Members
    edited March 2010
    can i use it in the function that is being called..
    can u just show me an example..
    i want to see the values returned by the function gettablecontents..
  • narayannarayan Administrators
    Sure.
    _alert(getTableContents(_table("tableId")));
    _debug(getTableContents(_table("tableId")));
    _assertEqual($dbData, getTableContents(_table("tableId")));
    
Sign In or Register to comment.